Similar to the MD5 algorithm, the SHA-1 algorithm generates a message digest after receiving a message and can be used to verify data integrity. The difference is that sha1 generates a 160-bit Message Digest (MD5 generates 128-bit messages ).
For more information about other features, see the MD5 algorithm.
1. Makeup
First, we need to perform a makeup so that the length of the information after the completion is 512 and the remainder is 448. That is, data is extended
K * 512 + 448 (BIT), that is, K * 64 + 56 (byte), where K is a natural number. Specific bit filling operation: first fill in 1, followed by 0 to meet the above requirements. At least 1 bit is required, and a maximum of BIT is required.
2. Fill Length
Add 8 bytes based on K * 64 + 56 (byte), which is used to save the length of the original information.
3. constants used
A series of constant words K (0), K (1),..., K (79), if given in hexadecimal notation. They are as follows:
KT = 0x5a827999 (0 <= T <= 19)
KT = 0x6ed9eba1 (20 <= T <= 39)
KT = 0x8f1bbcdc (40 <= T <= 59)
KT = 0xca62c1d6 (60 <= T <= 79)
Initialize a buffer constant
H0. = 67452301
H1 = efcdab89
H2 = 98 badcfe
H3 = 10325476
H4 = c3d2e1f0
4. Auxiliary Functions and encryption functions
Define four auxiliary functions:
(0 <= T <20): F (B, c, d, t) = (B & C) | ((~ B) & D );
(20 <= T <40): F (B, c, d, t) = B ^ C ^ d;
(40 <= T <60): F (B, c, d, t) = (B & C) | (B & D) | (C & D ));
(60 <= T <80): F (B, c, d, t) = B ^ C ^ d;
The C ++ code is as follows:
# Include <iostream> # include <iomanip> # include <string> # include <sstream> using namespace STD; unsigned circleshift (const unsigned & Word, const Int & bits) {return (word <bits) | (Word)> (32-bits);} unsigned sha1fun (const unsigned & B, const unsigned & C, const unsigned & D, const unsigned & T) {Switch (T/20) {Case 0: Return (B & C) | ((~ B) & D); Case 2: Return (B & C) | (B & D) | (C & D); Case 1: Case 3: return B ^ C ^ d;} return t;} string sha1 (const string & strraw) {string STR (strraw); STR ++ = (unsigned char) (0x80 ); // each byte is 8 bits, so it must take 8 and move three bits to the left while (Str. size () <3% 512! = 448) {STR + = (char) 0 ;}// write the length of the original data for (int I (56); I >= 0; I-= 8) {STR + = (unsigned char) (unsigned _ int64) strraw. size () <3)> I);} const unsigned K [4] = {0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6}; unsigned A (0x67452301 ), B (0xefcdab89), C (0x98badcfe), D (0x10325476), E (0xc3d2e1f0), T (0); unsigned W [80] = {0 }; // 64 bytes are processed each time. A total of 512 bits for (unsigned I (0); I! = Str. Size (); I + = 64) {// the first 16 words are original data for (unsigned T (0); t! = 16; ++ t) {// put 4 8-bit data into a 32-bit variable W [T] = (unsigned) STR [I + 4 * t] & 0xff) <24 | (unsigned) STR [I + 4 * t + 1] & 0xff) <16 | (unsigned) STR [I + 4 * t + 2] & 0xff) <8 | (unsigned) STR [I + 4 * t + 3] & 0xff );} // fill in for (unsigned T (16); t! = 80; + + T) {W [T] = circleshift (W [T-3] ^ W [t-8] ^ W [t-14] ^ W [T-16], 1 );} for (unsigned T (0); t! = 80; ++ t) {T = circleshift (A, 5) + sha1fun (B, c, d, T) + E + W [T] + k [t/20]; E = D; D = C; C = circleshift (B, 30); B =; A = T;} A + = 0x67452301; B + = 0xefcdab89; C + = 0x98badcfe; D + = 0x10325476; e + = 0xc3d2e1f0;} stringstream SS; SS <SETW (8) <setfill ('0')
Download specific c ++ Implementation Algorithms
Hash encryption algorithm-SHA-1