-
Notifications
You must be signed in to change notification settings - Fork 1
/
sha1.zsh
138 lines (128 loc) · 3.34 KB
/
sha1.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
source binutils.zsh
source array.zsh
function _sha1_f00()
{
echo -n $((($1 & $2)|((0xffffffff - $1) & $3)))
}
function _sha1_f20()
{
echo -n $(($1 ^ $2 ^ $3))
}
function _sha1_f40()
{
echo -n $((($1 & $2)|($1 & $3)|($2 & $3)))
}
function _sha1_f60()
{
echo -n $(($1 ^ $2 ^ $3))
}
function _sha1_circlar_shift()
{
echo -n $(((($2 << $1) & 0xffffffff)|(($2 >> (32 - $1)) & 0xffffffff)))
}
function _sha1_ulong_hex()
{
local table
table="0123456789ABCDEF"
for ((i=1; i<=8; i+=1)) do
echo -n $table[$(((($1 >> (32 - $i*4)) & 0xf)+1))]
done
}
function _sha1_hash()
{
local len buffer i
eval buffer=$(_array_toarray $1)
len=$(($#buffer * 8))
# extends
buffer[$(($#buffer + 1))]=$((0x80))
if [ $(($#buffer % 64)) -gt 56 ]; then
while [ $(($#buffer % 64)) -gt 56 ]; do
buffer[$(($#buffer + 1))]=0
done
fi
for ((i=$(($#buffer % 64 + 1)); i<=56; i+=1)) do
buffer[$(($#buffer + 1))]=0
done
buffer[$(($#buffer + 1))]=$((($len >> 52) & 0xff))
buffer[$(($#buffer + 1))]=$((($len >> 48) & 0xff))
buffer[$(($#buffer + 1))]=$((($len >> 40) & 0xff))
buffer[$(($#buffer + 1))]=$((($len >> 32) & 0xff))
buffer[$(($#buffer + 1))]=$((($len >> 24) & 0xff))
buffer[$(($#buffer + 1))]=$((($len >> 16) & 0xff))
buffer[$(($#buffer + 1))]=$((($len >> 8) & 0xff))
buffer[$(($#buffer + 1))]=$((($len >> 0) & 0xff))
local K
K=($((0x5A827999)) $((0x6ED9EBA1)) $((0x8F1BBCDC)) $((0xCA62C1D6)))
# calc digest
local A B C D E H0 H1 H2 H3 H4 W TMP index
H0=$((0x67452301))
H1=$((0xEFCDAB89))
H2=$((0x98BADCFE))
H3=$((0x10325476))
H4=$((0xC3D2E1F0))
W=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
index=0
while [ $index -lt $(($#buffer / 64)) ]; do
for ((i=1; i<=80; i+=1)) do W[$i]=0; done
# step a
for ((i=0; i<16; i+=1)) do
W[$(($i+1))]=$((($buffer[$(($i*4+1+($index*64)))]<<24)|($buffer[$(($i*4+2+($index*64)))]<<16)|($buffer[$(($i*4+3+($index*64)))]<<8)|($buffer[$(($i*4+4+($index*64)))])))
done
# step b
for ((i=16; i<80; i+=1)) do
W[$(($i+1))]=$(_sha1_circlar_shift 1 $(($W[$(($i-2))]^$W[$(($i-7))]^$W[$(($i-13))]^$W[$(($i-15))])))
done
# step c
A=$H0
B=$H1
C=$H2
D=$H3
E=$H4
# step d
for ((i=1; i<=20; i+=1)) do
TMP=$((($(_sha1_circlar_shift 5 $A) + $(_sha1_f00 $B $C $D) + $E + $W[$i] + $K[1]) & 0xffffffff))
E=$D
D=$C
C=$(_sha1_circlar_shift 30 $B)
B=$A
A=$TMP
done
for ((i=21; i<=40; i+=1)) do
TMP=$((($(_sha1_circlar_shift 5 $A) + $(_sha1_f20 $B $C $D) + $E + $W[$i] + $K[2]) & 0xffffffff))
E=$D
D=$C
C=$(_sha1_circlar_shift 30 $B)
B=$A
A=$TMP
done
for ((i=41; i<=60; i+=1)) do
TMP=$((($(_sha1_circlar_shift 5 $A) + $(_sha1_f40 $B $C $D) + $E + $W[$i] + $K[3]) & 0xffffffff))
E=$D
D=$C
C=$(_sha1_circlar_shift 30 $B)
B=$A
A=$TMP
done
for ((i=61; i<=80; i+=1)) do
TMP=$((($(_sha1_circlar_shift 5 $A) + $(_sha1_f60 $B $C $D) + $E + $W[$i] + $K[4]) & 0xffffffff))
E=$D
D=$C
C=$(_sha1_circlar_shift 30 $B)
B=$A
A=$TMP
done
# step e
H0=$((($H0 + $A) & 0xffffffff))
H1=$((($H1 + $B) & 0xffffffff))
H2=$((($H2 + $C) & 0xffffffff))
H3=$((($H3 + $D) & 0xffffffff))
H4=$((($H4 + $E) & 0xffffffff))
index=$(($index + 1))
done
# output
_sha1_ulong_hex $H0
_sha1_ulong_hex $H1
_sha1_ulong_hex $H2
_sha1_ulong_hex $H3
_sha1_ulong_hex $H4
}