原创不易,转载请注明出处。
目录
1. 算法简介
2. 符号
3. 加密算法流程
3.1 概述
3.2 填充
3.3 加密处理
附录A 运算示例
SHA英文全称Secure Hash Algorithm,即安全散列算法。散列算法又称杂凑算法或哈希算法,能将一定长度的消息计算出固定长度的字符串(又称消息摘要)。SHA包含5个算法,分别是SHA-1、SHA-224、SHA-256、SHA-384和SHA-512,后四者并称为SHA-2。
SHA-256最大计算明文长度为2^64bit,属于分组算法,分组长度为512bit,产生的信息摘要长度为256bit。SHA-256算法属于密码杂凑算法,原则上不能通过密文推出明文。
本文将通过实例,详细介绍SHA-256加密算法的加密流程。
下列符号适用于本文本。
h0~h7:8个32bit字,初始哈希值,也存放每组计算的当前哈希值
abcdefgh:8个32bit字,存放中间变量
m :消息
m':填充后的消息
mod:模运算
s0,s1:字节扩展时中间变量
S0,S1,ch,maj,temp1,temp2:循环迭代时中间变量
w[i]:第i个扩展字
∧:32bit与运算
∨:32bit或运算
⊕:异或运算
>>>k:循环右移k比特运算
>>k:右移k比特运算
←:左向赋值运算符
~:32bit字按位取反
对长度为l (l<2^64bit )比特的消息 ,SHA-256杂凑算法经过填充和迭代压缩,生成杂凑值,杂凑值长度为256比特。
假设消息m 的长度为 l比特。首先将比特“1”添加到消息的末尾,再添加 k个“0”,k 是满足l+1+k=448mod512 的最小的非负整数。然后再添加一个64位比特串,该比特串是长度l 的二进制表示。填充后的消息 m'的比特长度为512的倍数。
例如:对消息01100001 01100010 01100011,其长度 =24,经填充得到比特串:
事实上,在编写程序时,应该先进行分组,对满足512bit的分组先进行计算。当分到最后一组不足512bit时,再进行填充。
将填充后的消息m' 按512比特进行分组:m'=B0B1……Bn-1,其中n=(l+k+65)/512 。
3.3.1 常量及初始值
初始哈希值:
h0= 0x6a09e667
h1= 0xbb67ae85
h2= 0x3c6ef372
h3= 0xa54ff53a
h4= 0x510e527f
h5= 0x9b05688c
h6= 0x1f83d9ab
h7= 0x5be0cd19
初始常量矩阵:
k[0..63]=
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
3.3.2 加密流程
每组数据按照如下流程进行加密操作:
1)分组数据按照32bit字存放在w[0]~w[15]中,然后按照如下规则扩展成64个字。注意 s0, s1 最后是右移, 不是循环右移。
FOR i from 16 to 63
s0= (w[i-15] ) >>> 7)⊕(w[i-15] >>> 18)⊕(w[i-15] >> 3)
s1= (w[i-2] >>> 17)⊕(w[i-2] >>> 19)⊕(w[i-2] >> 10)
w[i]= w[i-16] + s0 + w[i-7] + s1
ENDFOR
2)赋值初始哈希值
a= h0
b= h1
c= h2
d= h3
e= h4
f= h5
g= h6
h= h7
3)迭代计算64次
FOR i from 0 to 63
S1= (e >>> 6) ⊕ (e >>> 11) ⊕ (e >>> 25)
ch= (e∧ f) ⊕ ((~ e) ∧ g)
temp1= h + S1 + ch + k[i] + w[i]
S0= (a >>> 2) ⊕ (a >>> 13) ⊕ (a >>> 22)
maj= (a∧b) ⊕ (a∧c)⊕(b∧c)
temp2= S0 + maj
h= g
g= f
f= e
e= d + temp1
d= c
c= b
b= a
a= temp1 + temp2
ENDFOR
4)获取当前哈希值
h0= h0 + a
h1= h1 + b
h2= h2 + c
h3= h3 + d
h4= h4 + e
h5= h5 + f
h6= h6 + g
h7= h7 + h
注意前一组得到的哈希值h0~h7作为下一分组的初始值,最终得到的h0~h7就是256bit杂凑值。
原始输入消息:
abc
填充后消息:
0x61 0x62 0x63 0x80 0x00 0x00 0x00 0x00
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x18
64个扩展字
w[0]~w[3]: 0x61626380 0x00000000 0x00000000 0x00000000
w[4]~w[7]: 0x00000000 0x00000000 0x00000000 0x00000000
w[8]~w[11]: 0x00000000 0x00000000 0x00000000 0x00000000
w[12]~w[15]:0x00000000 0x00000000 0x00000000 0x00000018
w[16]~w[19]:0x61626380 0x000f0000 0x7da86405 0x600003c6
w[20]~w[23]:0x3e9d7b78 0x0183fc00 0x12dcbfdb 0xe2e2c38e
w[24]~w[27]:0xc8215c1a 0xb73679a2 0xe5bc3909 0x32663c5b
w[28]~w[31]:0x9d209d67 0xec8726cb 0x702138a4 0xd3b7973b
w[32]~w[35]:0x93f5997f 0x3b68ba73 0xaff4ffc1 0xf10a5c62
w[36]~w[39]:0x0a8b3996 0x72af830a 0x9409e33e 0x24641522
w[40]~w[43]:0x9f47bf94 0xf0a64f5a 0x3e246a79 0x27333ba3
w[44]~w[47]:0x0c4763f2 0x840abf27 0x7a290d5d 0x065c43da
w[48]~w[51]:0xfb3e89cb 0xcc7617db 0xb9e66c34 0xa9993667
w[52]~w[55]:0x84badedd 0xc21462bc 0x1487472c 0xb20f7a99
w[56]~w[59]:0xef57b9cd 0xebe6b238 0x9fe3095e 0x78bc8d4b
w[60]~w[63]:0xa43fcf15 0x668b2ff8 0xeeaba2cc 0x12b1edeb
64轮迭代(十六进制表示):
轮 |
a |
b |
c |
d |
e |
f |
g |
h |
0 |
6a09e667 |
bb67ae85 |
3c6ef372 |
a54ff53a |
510e527f |
9b05688c |
1f83d9ab |
5be0cd19 |
1 |
5d6aebcd |
6a09e667 |
bb67ae85 |
3c6ef372 |
fa2a4622 |
510e527f |
9b05688c |
1f83d9ab |
2 |
5a6ad9ad |
5d6aebcd |
6a09e667 |
bb67ae85 |
78ce7989 |
fa2a4622 |
510e527f |
9b05688c |
3 |
c8c347a7 |
5a6ad9ad |
5d6aebcd |
6a09e667 |
f92939eb |
78ce7989 |
fa2a4622 |
510e527f |
4 |
d550f666 |
c8c347a7 |
5a6ad9ad |
5d6aebcd |
24e00850 |
f92939eb |
78ce7989 |
fa2a4622 |
5 |
04409a6a |
d550f666 |
c8c347a7 |
5a6ad9ad |
43ada245 |
24e00850 |
f92939eb |
78ce7989 |
6 |
2b4209f5 |
04409a6a |
d550f666 |
c8c347a7 |
714260ad |
43ada245 |
24e00850 |
f92939eb |
7 |
e5030380 |
2b4209f5 |
04409a6a |
d550f666 |
9b27a401 |
714260ad |
43ada245 |
24e00850 |
8 |
85a07b5f |
e5030380 |
2b4209f5 |
04409a6a |
0c657a79 |
9b27a401 |
714260ad |
43ada245 |
9 |
8e04ecb9 |
85a07b5f |
e5030380 |
2b4209f5 |
32ca2d8c |
0c657a79 |
9b27a401 |
714260ad |
10 |
8c87346b |
8e04ecb9 |
85a07b5f |
e5030380 |
1cc92596 |
32ca2d8c |
0c657a79 |
9b27a401 |
11 |
4798a3f4 |
8c87346b |
8e04ecb9 |
85a07b5f |
436b23e8 |
1cc92596 |
32ca2d8c |
0c657a79 |
12 |
f71fc5a9 |
4798a3f4 |
8c87346b |
8e04ecb9 |
816fd6e9 |
436b23e8 |
1cc92596 |
32ca2d8c |
13 |
87912990 |
f71fc5a9 |
4798a3f4 |
8c87346b |
1e578218 |
816fd6e9 |
436b23e8 |
1cc92596 |
14 |
d932eb16 |
87912990 |
f71fc5a9 |
4798a3f4 |
745a48de |
1e578218 |
816fd6e9 |
436b23e8 |
15 |
c0645fde |
d932eb16 |
87912990 |
f71fc5a9 |
0b92f20c |
745a48de |
1e578218 |
816fd6e9 |
16 |
b0fa238e |
c0645fde |
d932eb16 |
87912990 |
07590dcd |
0b92f20c |
745a48de |
1e578218 |
17 |
21da9a9b |
b0fa238e |
c0645fde |
d932eb16 |
8034229c |
07590dcd |
0b92f20c |
745a48de |
18 |
c2fbd9d1 |
21da9a9b |
b0fa238e |
c0645fde |
846ee454 |
8034229c |
07590dcd |
0b92f20c |
19 |
fe777bbf |
c2fbd9d1 |
21da9a9b |
b0fa238e |
cc899961 |
846ee454 |
8034229c |
07590dcd |
20 |
e1f20c33 |
fe777bbf |
c2fbd9d1 |
21da9a9b |
b0638179 |
cc899961 |
846ee454 |
8034229c |
21 |
9dc68b63 |
e1f20c33 |
fe777bbf |
c2fbd9d1 |
8ada8930 |
b0638179 |
cc899961 |
846ee454 |
22 |
c2606d6d |
9dc68b63 |
e1f20c33 |
fe777bbf |
e1257970 |
8ada8930 |
b0638179 |
cc899961 |
23 |
a7a3623f |
c2606d6d |
9dc68b63 |
e1f20c33 |
49f5114a |
e1257970 |
8ada8930 |
b0638179 |
24 |
c5d53d8d |
a7a3623f |
c2606d6d |
9dc68b63 |
aa47c347 |
49f5114a |
e1257970 |
8ada8930 |
25 |
1c2c2838 |
c5d53d8d |
a7a3623f |
c2606d6d |
2823ef91 |
aa47c347 |
49f5114a |
e1257970 |
26 |
cde8037d |
1c2c2838 |
c5d53d8d |
a7a3623f |
14383d8e |
2823ef91 |
aa47c347 |
49f5114a |
27 |
b62ec4bc |
cde8037d |
1c2c2838 |
c5d53d8d |
c74c6516 |
14383d8e |
2823ef91 |
aa47c347 |
28 |
77d37528 |
b62ec4bc |
cde8037d |
1c2c2838 |
edffbff8 |
c74c6516 |
14383d8e |
2823ef91 |
29 |
363482c9 |
77d37528 |
b62ec4bc |
cde8037d |
6112a3b7 |
edffbff8 |
c74c6516 |
14383d8e |
30 |
a0060b30 |
363482c9 |
77d37528 |
b62ec4bc |
ade79437 |
6112a3b7 |
edffbff8 |
c74c6516 |
31 |
ea992a22 |
a0060b30 |
363482c9 |
77d37528 |
0109ab3a |
ade79437 |
6112a3b7 |
edffbff8 |
32 |
73b33bf5 |
ea992a22 |
a0060b30 |
363482c9 |
ba591112 |
0109ab3a |
ade79437 |
6112a3b7 |
33 |
98e12507 |
73b33bf5 |
ea992a22 |
a0060b30 |
9cd9f5f6 |
ba591112 |
0109ab3a |
ade79437 |
34 |
fe604df5 |
98e12507 |
73b33bf5 |
ea992a22 |
59249dd3 |
9cd9f5f6 |
ba591112 |
0109ab3a |
35 |
a9a7738c |
fe604df5 |
98e12507 |
73b33bf5 |
085f3833 |
59249dd3 |
9cd9f5f6 |
ba591112 |
36 |
65a0cfe4 |
a9a7738c |
fe604df5 |
98e12507 |
f4b002d6 |
085f3833 |
59249dd3 |
9cd9f5f6 |
37 |
41a65cb1 |
65a0cfe4 |
a9a7738c |
fe604df5 |
0772a26b |
f4b002d6 |
085f3833 |
59249dd3 |
38 |
34df1604 |
41a65cb1 |
65a0cfe4 |
a9a7738c |
a507a53d |
0772a26b |
f4b002d6 |
085f3833 |
39 |
6dc57a8a |
34df1604 |
41a65cb1 |
65a0cfe4 |
f0781bc8 |
a507a53d |
0772a26b |
f4b002d6 |
40 |
79ea687a |
6dc57a8a |
34df1604 |
41a65cb1 |
1efbc0a0 |
f0781bc8 |
a507a53d |
0772a26b |
41 |
d6670766 |
79ea687a |
6dc57a8a |
34df1604 |
26352d63 |
1efbc0a0 |
f0781bc8 |
a507a53d |
42 |
df46652f |
d6670766 |
79ea687a |
6dc57a8a |
838b2711 |
26352d63 |
1efbc0a0 |
f0781bc8 |
43 |
17aa0dfe |
df46652f |
d6670766 |
79ea687a |
decd4715 |
838b2711 |
26352d63 |
1efbc0a0 |
44 |
9d4baf93 |
17aa0dfe |
df46652f |
d6670766 |
fda24c2e |
decd4715 |
838b2711 |
26352d63 |
45 |
26628815 |
9d4baf93 |
17aa0dfe |
df46652f |
a80f11f0 |
fda24c2e |
decd4715 |
838b2711 |
46 |
72ab4b91 |
26628815 |
9d4baf93 |
17aa0dfe |
b7755da1 |
a80f11f0 |
fda24c2e |
decd4715 |
47 |
a14c14b0 |
72ab4b91 |
26628815 |
9d4baf93 |
d57b94a9 |
b7755da1 |
a80f11f0 |
fda24c2e |
48 |
4172328d |
a14c14b0 |
72ab4b91 |
26628815 |
fecf0bc6 |
d57b94a9 |
b7755da1 |
a80f11f0 |
49 |
05757ceb |
4172328d |
a14c14b0 |
72ab4b91 |
bd714038 |
fecf0bc6 |
d57b94a9 |
b7755da1 |
50 |
f11bfaa8 |
05757ceb |
4172328d |
a14c14b0 |
6e5c390c |
bd714038 |
fecf0bc6 |
d57b94a9 |
51 |
7a0508a1 |
f11bfaa8 |
05757ceb |
4172328d |
52f1ccf7 |
6e5c390c |
bd714038 |
fecf0bc6 |
52 |
886e7a22 |
7a0508a1 |
f11bfaa8 |
05757ceb |
49231c1e |
52f1ccf7 |
6e5c390c |
bd714038 |
53 |
101fd28f |
886e7a22 |
7a0508a1 |
f11bfaa8 |
529e7d00 |
49231c1e |
52f1ccf7 |
6e5c390c |
54 |
f5702fdb |
101fd28f |
886e7a22 |
7a0508a1 |
9f4787c3 |
529e7d00 |
49231c1e |
52f1ccf7 |
55 |
3ec45cdb |
f5702fdb |
101fd28f |
886e7a22 |
e50e1b4f |
9f4787c3 |
529e7d00 |
49231c1e |
56 |
38cc9913 |
3ec45cdb |
f5702fdb |
101fd28f |
54cb266b |
e50e1b4f |
9f4787c3 |
529e7d00 |
57 |
fcd1887b |
38cc9913 |
3ec45cdb |
f5702fdb |
9b5e906c |
54cb266b |
e50e1b4f |
9f4787c3 |
58 |
c062d46f |
fcd1887b |
38cc9913 |
3ec45cdb |
7e44008e |
9b5e906c |
54cb266b |
e50e1b4f |
59 |
ffb70472 |
c062d46f |
fcd1887b |
38cc9913 |
6d83bfc6 |
7e44008e |
9b5e906c |
54cb266b |
60 |
b6ae8fff |
ffb70472 |
c062d46f |
fcd1887b |
b21bad3d |
6d83bfc6 |
7e44008e |
9b5e906c |
61 |
b85e2ce9 |
b6ae8fff |
ffb70472 |
c062d46f |
961f4894 |
b21bad3d |
6d83bfc6 |
7e44008e |
62 |
04d24d6c |
b85e2ce9 |
b6ae8fff |
ffb70472 |
948d25b6 |
961f4894 |
b21bad3d |
6d83bfc6 |
63 |
d39a2165 |
04d24d6c |
b85e2ce9 |
b6ae8fff |
fb121210 |
948d25b6 |
961f4894 |
b21bad3d |
64 |
506e3058 |
d39a2165 |
04d24d6c |
b85e2ce9 |
5ef50f24 |
fb121210 |
948d25b6 |
961f4894 |
杂凑值h0~h7
h0:0xba7816bf
h1:0x8f01cfea
h2:0x414140de
h3:0x5dae2223
h4:0xb00361a3
h5:0x96177a9c
h6:0xb410ff61
h7:0xf20015ad
参考文献:
[1]https://en.wikipedia.org/wiki/Secure_Hash_Algorithms