-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsha1.lua
More file actions
221 lines (195 loc) · 4.56 KB
/
sha1.lua
File metadata and controls
221 lines (195 loc) · 4.56 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
-------------------------------------------------
--- *** BitLibEmu for Lua *** ---
-------------------------------------------------
--- Author: Martin Huesser ---
--- Date: 2008-06-16 ---
--- License: You may use this code in your ---
--- projects as long as this header ---
--- stays intact. ---
-------------------------------------------------
--[[
local mod = math.fmod
local floor = math.floor
bit = {}
----------------------------------------
local function cap(x)
return mod(x,4294967296)
end
----------------------------------------
function bit.bnot(x)
return 4294967295-cap(x)
end
----------------------------------------
function bit.lshift(x,n)
return cap(cap(x)*2^n)
end
----------------------------------------
function bit.rshift(x,n)
return floor(cap(x)/2^n)
end
----------------------------------------
function bit.band(x,y)
local z,i,j = 0,1
for j = 0,31 do
if (mod(x,2)==1 and mod(y,2)==1) then
z = z + i
end
x = bit.rshift(x,1)
y = bit.rshift(y,1)
i = i*2
end
return z
end
----------------------------------------
function bit.bor(x,y)
local z,i,j = 0,1
for j = 0,31 do
if (mod(x,2)==1 or mod(y,2)==1) then
z = z + i
end
x = bit.rshift(x,1)
y = bit.rshift(y,1)
i = i*2
end
return z
end
----------------------------------------
function bit.bxor(x,y)
local z,i,j = 0,1
for j = 0,31 do
if (mod(x,2)~=mod(y,2)) then
z = z + i
end
x = bit.rshift(x,1)
y = bit.rshift(y,1)
i = i*2
end
return z
end
]]
-------------------------------------------------
--- *** SHA-1 algorithm for Lua *** ---
-------------------------------------------------
--- Author: Martin Huesser ---
--- Date: 2008-06-16 ---
--- License: You may use this code in your ---
--- projects as long as this header ---
--- stays intact. ---
-------------------------------------------------
local strlen = string.len
local strchar = string.char
local strbyte = string.byte
local strsub = string.sub
local floor = math.floor
local bnot = bit.bnot
local band = bit.band
local bor = bit.bor
local bxor = bit.bxor
local shl = bit.lshift
local shr = bit.rshift
local h0, h1, h2, h3, h4
-------------------------------------------------
local function LeftRotate(val, nr)
return shl(val, nr) + shr(val, 32 - nr)
end
-------------------------------------------------
local function ToHex(num)
local i, d
local str = ""
for i = 1, 8 do
d = band(num, 15)
if (d < 10) then
str = strchar(d + 48) .. str
else
str = strchar(d + 87) .. str
end
num = floor(num / 16)
end
return str
end
-------------------------------------------------
local function PreProcess(str)
local bitlen, i
local str2 = ""
bitlen = strlen(str) * 8
str = str .. strchar(128)
i = 56 - band(strlen(str), 63)
if (i < 0) then
i = i + 64
end
for i = 1, i do
str = str .. strchar(0)
end
for i = 1, 8 do
str2 = strchar(band(bitlen, 255)) .. str2
bitlen = floor(bitlen / 256)
end
return str .. str2
end
-------------------------------------------------
local function MainLoop(str)
local a, b, c, d, e, f, k, t
local i, j
local w = {}
while (str ~= "") do
for i = 0, 15 do
w[i] = 0
for j = 1, 4 do
w[i] = w[i] * 256 + strbyte(str, i * 4 + j)
end
end
for i = 16, 79 do
w[i] = LeftRotate(bxor(bxor(w[i - 3], w[i - 8]), bxor(w[i - 14], w[i - 16])), 1)
end
a = h0
b = h1
c = h2
d = h3
e = h4
for i = 0, 79 do
if (i < 20) then
f = bor(band(b, c), band(bnot(b), d))
k = 1518500249
elseif (i < 40) then
f = bxor(bxor(b, c), d)
k = 1859775393
elseif (i < 60) then
f = bor(bor(band(b, c), band(b, d)), band(c, d))
k = 2400959708
else
f = bxor(bxor(b, c), d)
k = 3395469782
end
t = LeftRotate(a, 5) + f + e + k + w[i]
e = d
d = c
c = LeftRotate(b, 30)
b = a
a = t
end
h0 = band(h0 + a, 4294967295)
h1 = band(h1 + b, 4294967295)
h2 = band(h2 + c, 4294967295)
h3 = band(h3 + d, 4294967295)
h4 = band(h4 + e, 4294967295)
str = strsub(str, 65)
end
end
-------------------------------------------------
function Sha1(str)
str = PreProcess(str)
h0 = 1732584193
h1 = 4023233417
h2 = 2562383102
h3 = 0271733878
h4 = 3285377520
MainLoop(str)
return ToHex(h0) ..
ToHex(h1) ..
ToHex(h2) ..
ToHex(h3) ..
ToHex(h4)
end
-------------------------------------------------
-------------------------------------------------
-------------------------------------------------