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)
| 54 | return w |
| 55 | |
| 56 | def final_hash(self): |
| 57 | """ |
| 58 | Calls all the other methods to process the input. Pads the data, then splits into |
| 59 | blocks and then does a series of operations for each block (including expansion). |
| 60 | For each block, the variable h that was initialized is copied to a,b,c,d,e |
| 61 | and these 5 variables a,b,c,d,e undergo several changes. After all the blocks are |
| 62 | processed, these 5 variables are pairwise added to h ie a to h[0], b to h[1] and so on. |
| 63 | This h becomes our final hash which is returned. |
| 64 | """ |
| 65 | self.padded_data = self.padding() |
| 66 | self.blocks = self.split_blocks() |
| 67 | for block in self.blocks: |
| 68 | expanded_block = self.expand_block(block) |
| 69 | a, b, c, d, e = self.h |
| 70 | for i in range(0, 80): |
| 71 | if 0 <= i < 20: |
| 72 | f = (b & c) | ((~b) & d) |
| 73 | k = 0x5A827999 |
| 74 | elif 20 <= i < 40: |
| 75 | f = b ^ c ^ d |
| 76 | k = 0x6ED9EBA1 |
| 77 | elif 40 <= i < 60: |
| 78 | f = (b & c) | (b & d) | (c & d) |
| 79 | k = 0x8F1BBCDC |
| 80 | elif 60 <= i < 80: |
| 81 | f = b ^ c ^ d |
| 82 | k = 0xCA62C1D6 |
| 83 | a, b, c, d, e = ( |
| 84 | self.rotate(a, 5) + f + e + k + expanded_block[i] & 0xFFFFFFFF, |
| 85 | a, |
| 86 | self.rotate(b, 30), |
| 87 | c, |
| 88 | d, |
| 89 | ) |
| 90 | self.h = ( |
| 91 | self.h[0] + a & 0xFFFFFFFF, |
| 92 | self.h[1] + b & 0xFFFFFFFF, |
| 93 | self.h[2] + c & 0xFFFFFFFF, |
| 94 | self.h[3] + d & 0xFFFFFFFF, |
| 95 | self.h[4] + e & 0xFFFFFFFF, |
| 96 | ) |
| 97 | return "%08x%08x%08x%08x%08x" % tuple(self.h) |
| 98 | |
| 99 | |
| 100 | class SHA1HashTest(unittest.TestCase): |
no test coverage detected