SHA-1 is a one-way hash function that calculates a hash value of 160 bits based on a message with an upper limit of 2^64 bits, and its grouping and filling of data is the same as MD5, with 512 bits being 1 groups, filling the data with 1, filling 0, filling 448 bits, and the last 64 bits representing the original data length.
1. SHA-1: Fill
With Hello. This is populated with a 6-byte message length.
Hello. = 6C 6C 6F 2E, written in binary form as follows:
01001000 01100101 01101100 01101100 01101111 00101110
Raw data length is 6 bytes (48 bits)
①. Fill the data, always fill 448 bits
01001000 01100101 01101100 01101100 01101111 00101110 1 (first filled with 1 1)
01001000 01100101 01101100 01101100 01101111 00101110 10000000000000000000000 ..... (Refill 0, fill 448 bits)
②. Add raw data length at the bottom (6 bytes)
01001000 01100101 01101100 01101100 01101111 00101110 10000000000000000000000 ..... 00000000 (assuming that this place is filled with 448 bits)
000000000000000000000000000000000000000000000000000000000110000 (Increase the original data length by 48 bits at the end, add in binary form)
In this respect, the total data is 512 bits.
2. SHA-1: Calculation w0~w79
Once the data has been populated, we'll do the following in the input grouping, which calculates 80 32-bit values (w0~w79) for each input grouping, first dividing the input groupings of 512 bits into 32-bit *16 groups, and naming them W0~W15, and then the remaining w16~ The W79 method is calculated as follows:
W16 = (W0 ^ W2 ^ W8 ^ W13) loop left shift 1 bit
That
Wt = (Wt-16 ^ Wt-14 ^ Wt-8 ^ Wt-3) loop left shift 1 bit
Upon completion, the W0~W79 data is completed.
3. SHA-1: Packet processing
Next, the input grouping is processed in 80 steps to change the internal state based on the input grouping information.
In the case of packet processing, the SHA-1 constant KT and HT:
SHA-1 uses F0, F1, .... f79 a sequence of logical functions, each of which operates on 3 32-bit double-word b,c,d, producing a 32-bit double-word output.
Treatment of W0~W19:
Treatment of w20~w39:
Treatment of w40~w59:
Treatment of w60~w79:
After a step is completed, the contents of the buffer a,b,c,d are copied into the B,c,d,e (where B is to loop 30 bits to the left and then copied), and the contents of the buffer e are added to the buffer a after the contents of the other buffers and WT, KT.
Finally, the value of the loop 80 steps is added to the original, that is, the original 5 H constants are updated to get the final 160-bit message digest.
Principle of SHA-1 encryption algorithm