MCPcopy Create free account
hub / github.com/subbarayudu-j/TheAlgorithms-Python / final_hash

Method final_hash

hashes/sha1.py:78–112  ·  view source on GitHub ↗

Calls all the other methods to process the input. Pads the data, then splits into blocks and then does a series of operations for each block (including expansion). For each block, the variable h that was initialized is copied to a,b,c,d,e and these 5 variables a,b,c,

(self)

Source from the content-addressed store, hash-verified

76 return w
77
78 def final_hash(self):
79 """
80 Calls all the other methods to process the input. Pads the data, then splits into
81 blocks and then does a series of operations for each block (including expansion).
82 For each block, the variable h that was initialized is copied to a,b,c,d,e
83 and these 5 variables a,b,c,d,e undergo several changes. After all the blocks are
84 processed, these 5 variables are pairwise added to h ie a to h[0], b to h[1] and so on.
85 This h becomes our final hash which is returned.
86 """
87 self.padded_data = self.padding()
88 self.blocks = self.split_blocks()
89 for block in self.blocks:
90 expanded_block = self.expand_block(block)
91 a, b, c, d, e = self.h
92 for i in range(0, 80):
93 if 0 <= i < 20:
94 f = (b & c) | ((~b) & d)
95 k = 0x5A827999
96 elif 20 <= i < 40:
97 f = b ^ c ^ d
98 k = 0x6ED9EBA1
99 elif 40 <= i < 60:
100 f = (b & c) | (b & d) | (c & d)
101 k = 0x8F1BBCDC
102 elif 60 <= i < 80:
103 f = b ^ c ^ d
104 k = 0xCA62C1D6
105 a, b, c, d, e = self.rotate(a, 5) + f + e + k + expanded_block[i] & 0xffffffff,\
106 a, self.rotate(b, 30), c, d
107 self.h = self.h[0] + a & 0xffffffff,\
108 self.h[1] + b & 0xffffffff,\
109 self.h[2] + c & 0xffffffff,\
110 self.h[3] + d & 0xffffffff,\
111 self.h[4] + e & 0xffffffff
112 return '%08x%08x%08x%08x%08x' %tuple(self.h)
113
114
115class SHA1HashTest(unittest.TestCase):

Callers 2

testMatchHashesMethod · 0.80
mainFunction · 0.80

Calls 4

paddingMethod · 0.95
split_blocksMethod · 0.95
expand_blockMethod · 0.95
rotateMethod · 0.95

Tested by

no test coverage detected