1
1
import argparse
2
2
from unittest import TextTestRunner
3
3
from sys import argv
4
+
4
5
from constants import INITIAL_HASH_VALUES , CONSTANTS
5
6
from helpers import bytes_to_words , words_to_bytes , circular_shift
6
7
from test_project import make_test_suite
@@ -14,7 +15,7 @@ def pad_message(byte_message: bytes):
14
15
and then 0 value bytes
15
16
At the end of the message adds 64b/8B containing length(in bits) of original message
16
17
"""
17
- number_of_zeros = 64 - ((len (byte_message ) + 8 ) & 0x3F ) - 1
18
+ number_of_zeros = 64 - ((len (byte_message ) + 8 ) % 64 ) - 1
18
19
byte_message += (
19
20
b"\x80 "
20
21
+ b"\x00 " * (number_of_zeros )
@@ -49,9 +50,9 @@ def sha256_bytes(message: bytes, in_hex=True):
49
50
Ch = lambda x , y , z : z ^ (x & (y ^ z )) # Choose function
50
51
Maj = lambda x , y , z : ((x | y ) & z ) | (x & y ) # Majority function
51
52
52
- def _round (hash_values : tuple , word : int , constant : int ):
53
+ def _round (working_variables : tuple , word : int , constant : int ):
53
54
# Function performing one round of the compression function
54
- a , b , c , d , e , f , g , h = hash_values
55
+ a , b , c , d , e , f , g , h = working_variables
55
56
temp1 = h + sigma1 (e ) + Ch (e , f , g ) + constant + word
56
57
temp2 = sigma0 (a ) + Maj (a , b , c )
57
58
return (
@@ -75,20 +76,20 @@ def _round(hash_values: tuple, word: int, constant: int):
75
76
for chunk in chunks :
76
77
# Initializing values for the current loop
77
78
W = chunk [:]
78
- current_hash_values = hash_values
79
+ working_variables = hash_values
79
80
80
81
# Extend chunks onto the whole range
81
82
for i in range (16 , 64 ):
82
83
W .append (
83
84
(W [i - 16 ] + sum0 (W [i - 15 ]) + W [i - 7 ] + sum1 (W [i - 2 ])) & 0xFFFFFFFF
84
85
)
85
-
86
+
86
87
# Compression loop
87
88
for i in range (64 ):
88
- current_hash_values = _round (current_hash_values , W [i ], CONSTANTS [i ])
89
+ working_variables = _round (working_variables , W [i ], CONSTANTS [i ])
89
90
90
91
hash_values = tuple (
91
- [(current_hash_values [i ] + hash_values [i ]) & 0xFFFFFFFF for i in range (8 )]
92
+ [(working_variables [i ] + hash_values [i ]) & 0xFFFFFFFF for i in range (8 )]
92
93
)
93
94
result = words_to_bytes (hash_values )
94
95
if in_hex :
@@ -97,7 +98,7 @@ def _round(hash_values: tuple, word: int, constant: int):
97
98
return result
98
99
99
100
100
- def sha256_from_file (filename ):
101
+ def sha256_from_file (filename : str ):
101
102
"""
102
103
Read file bytes and hashes them
103
104
"""
0 commit comments