SHA Encryption Algorithm

Source: Internet
Author: User

SHA1 algorithm principle
 
1 Introduction to SHA1 Algorithms
 
Secure Hash Algorithm (Secure Hash Algorithm) is mainly applicable to Digital Signature Algorithm DSA defined in Digital Signature Standard DSS ). For messages with a length less than 2 ^ 64-bit, SHA1 generates a 160-bit message digest. When a message is received, the message digest can be used to verify data integrity. Data may change during transmission, so different message summaries are generated at this time.
 
SHA1 has the following features: it cannot restore information from the message digest; two different messages do not produce the same message digest.
 
2 terms and concepts
 
2.1-Bit, Byte, and Word)
 
SHA1 always treats a message as a single-bit string. In this article, a Word is 32 bits, and a Byte is 8 bits. For example, the string "abc" can be converted into a single character string: 01100001 01100010 01100011. It can also be expressed as a hexadecimal string: 0x616263.
 
2.2 operators and symbols
 
The following logical operators are used in Word)
 
X ^ Y = X, Y logic and
 
X \/Y = X, Y logic or
 
X xor y = X, Y logic exclusive or
 
~ X = X logic inversion
 
X + Y is defined as follows:
 
The characters X and Y represent two integers x and y, where 0 <= x <2 ^ 32 and 0 <= y <2 ^ 32. make the integer z = (x + y) mod 2 ^ 32. at this time, 0 <= z <2 ^ 32. convert z to Z, which means Z = X + Y. www.2cto.com
 
The Left shift operator Sn (X) of the loop ). X is a word, n is an integer, 0 <= n <= 32. Sn (X) = (X <n) OR (X> 32-n)
 
X <n is defined as follows: discard the leftmost n-digit number and move each digit to the left one by one, then fill the n bits on the right with 0 (the final result is still 32 bits ). X> n is to discard the n digits on the right, move the n digits to the right in turn, and enter 0 in the n digits on the left. Therefore, it can be called the Sn (X) bitwise cyclic shift operation.
 
3. SHA1 Algorithm Description
 
In the SHA1 algorithm, we must convert the original message (string, file, etc.) into a bit string. The SHA1 algorithm only accepts bits as input. Suppose we generate a message digest for the string "abc. First, we convert it into a bit string as follows:
 
01100001 01100010 01100011
 
―――――――――――――
 
'A' = 97 'B' = 98 'c' = 99
 
The length of this character string is 24. Next, we need five steps to calculate MD5.
 
3.1 makeup
 
The message must be supplemented so that the remainder of its length after the modulo of 512 is 448. That is to say, (the message length after completion) % 512 = 448. Even if the remainder of the 512 modulo is 448, the padding is required.
 
The padding is done in this way: first fill in 1, then fill in 0, until the length of the 512 modulo is satisfied, and then the remainder is 448. All in all, a minimum of 512 bits can be supplemented. Or the previous "abc" example shows the completion process.
 
Original information: 01100001 01100010 01100011
 
Step 1: 01100001 01100010 01100011 1
 
First, add "1"
 
Step 2: 01100001 01100010 01100011 10 ..... 0
 
Then add 423 "0"
 
We can write the data after the final makeup in hexadecimal format as follows:
 
61626380 00000000 00000000 00000000
 
00000000 00000000 00000000 00000000
 
00000000 00000000 00000000 00000000
 
00000000 00000000
 
Now that the data length is 448, we can proceed with the next step.
 
3.2 fill Length
 
The so-called fill length is to fill the length of the original data after the message that has completed the fill operation. A 64-bit data is usually used to indicate the length of the original message. If the message length is not greater than 2 ^ 64, the first word is 0. After completing the length, the entire message is changed to the following (hexadecimal format)
 
61626380 00000000 00000000 00000000
 
00000000 00000000 00000000 00000000
 
00000000 00000000 00000000 00000000
 
00000000 00000000 00000000 00000018
 
If the length of the original message exceeds 512, we need to add it to a multiple of 512. Then, we divide the entire message into 512-bit data blocks and process each data block separately to obtain the message digest.
 
Constant used in 3.3
 
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 ).
 
3.4 functions to be used
 
In SHA1, we need a series of functions. Each function ft (0 <= t <= 79) operates on 32 characters B, C, and D and generates 32 characters as output. Ft (B, C, D) can be defined as follows
 
Ft (B, C, D) = (B and c) or (not B) AND D) (0 <= t <= 19)
 
Ft (B, C, D) = B XOR C XOR D (20 <= t <= 39)
 
Ft (B, C, D) = (B and c) or (B AND D) or (C AND D) (40 <= t <= 59)
 
Ft (B, C, D) = B XOR C XOR D (60 <= t <= 79 ).
 
3.5 calculate the message digest
 
The message digest must be calculated using messages that have been completed and completed. Computing requires two buffers, each consisting of five 32-bit characters, and a buffer with 80 32-bit characters. The buffer with the first five characters is identified as A, B, C, D, E. The buffer with the second five characters is marked as H0, H1, H2, H3, H4
 
. The buffer with 80 characters is identified as W0, W1,..., W79
 
In addition, a word TEMP buffer is also required.
 
To generate a message digest, the 16-character data block M1, M2,..., Mn defined in section 4th
 
Each data block Mi contains 80 steps.
 
Before processing each data block, the buffer {Hi} is initialized to the following value (hexadecimal)
 
H0 = 0x67452301
 
H1 = 0xEFCDAB89
 
H2 = 0x98BADCFE
 
H3 = 0x10325476
 
H4 = 0xC3D2E1F0.
 
Now we start to process M1, M2,..., Mn. To process Mi, follow these steps:
 
(1). Divide Mi into 16 words W0, W1,..., W15, W0 is the leftmost word
 
(2). For t = 16 to 79 make Wt = S1 (Wt-3 XOR Wt-8 XOR Wt-14 XOR Wt-16 ).
 
(3). Make A = H0, B = H1, C = H2, D = H3, E = H4.
 
(4) For t = 0 to 79, execute the following Loop
 
TEMP = S5 (A) + ft (B, C, D) + E + Wt + Kt;
 
E = D; D = C; C = S30 (B); B = A; A = TEMP;
 
(5). Make H0 = H0 + A, H1 = H1 + B, H2 = H2 + C, H3 = H3 + D, H4 = H4 + E.
 
After processing all Mn, the message digest is a 160-bit string, which is identified in the following sequence
 
H0 H1 H2 H3 H4.
 
For SHA256, SHA384, and SHA512. You can also calculate the message digest in a similar way. The algorithms for completing messages are the same.
 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.