-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhash.lua
More file actions
252 lines (227 loc) · 4.97 KB
/
hash.lua
File metadata and controls
252 lines (227 loc) · 4.97 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
local bit = require("bit")
local mod = require("mod")
local base64 = require("base64")
local function hash(pwd, m)
m = m or 0xFFFFFFFF
pwd = tostring(pwd)
pwd = pwd .. "@" .. tostring(pwd:len())
local h = 0
for i = 1, #pwd do
h = (31 * h + pwd:sub(i, i):byte()) % m
end
return h
end
local function primes(limit)
local f = io.open("primes_"..tostring(limit)..".dat", "r")
if f then
local primes = {}
while true do
local p = f:read()
if p then
table.insert(primes, tonumber(p))
else
break
end
end
f:close()
return primes
end
local sievebound = math.floor((limit - 1) / 2)
local sieve = {}
for i = 1, sievebound do
sieve[i] = false
end
local crosslimit = math.floor((math.sqrt(limit) - 1) / 2)
for i = 1, crosslimit do
if sieve[i] == false then
for j = 2 * i * (i + 1), sievebound, 2 * i + 1 do
sieve[j] = true
end
end
end
local f = io.open("primes_"..tostring(limit)..".dat", "w")
local primes = {2}
f:write("2\n")
for i = 1, sievebound do
if sieve[i] == false then
table.insert(primes, 2 * i + 1)
f:write(2 * i + 1, "\n")
end
end
f:close()
return primes
end
local function gcd(a, b)
local q = math.floor(a/b)
local r = a%b
if r == 0 then
return b
else
return gcd(b, r)
end
end
local function inverse(a, n)
local t = 0
local newt = 1
local r = n
local newr = a
while newr ~= 0 do
local quotient = math.floor(r/newr)
t, newt = newt, (t - quotient * newt)
r, newr = newr, (r - quotient * newr)
end
if r > 1 then
return nil
elseif t < 0 then
t = t + n
end
return t
end
local function genprimes()
math.randomseed(os.time())
local n1, n2
while n1 == n2 do
n1, n2 = math.random(513708), math.random(513708)
end
local f = io.open("primes.dat", "r")
local count = 0
while true do
local line = f:read()
if line == nil then
break
end
count = count + 1
if count == n1 then
p = tonumber(line)
elseif count == n2 then
q = tonumber(line)
end
if p ~= nil and q ~= nil then
break
end
end
f:close()
return p, q
end
local function genkey(p, q, e)
if p == nil or q == nil then
p, q = genprimes()
end
local n = p * q
local phi = (p - 1) * (q - 1)
if e == nil then
for i = phi - 2, 2, -1 do
if gcd(i, phi) == 1 then
e = i
break
end
end
end
local d = inverse(e, phi)
return {n, e}, {n, d}
end
local function crypt(msg, key)
local n, e = unpack(key)
return mod.exp(msg, e, n)
end
local function encrypt(data, key)
local nums = {}
for i = 1, #data, 5 do
local m = 0
for j = 0, 3 do
m = m + (data:sub(i + j):byte() or 0)
m = m * 256
end
m = m + (data:sub(i + 4):byte() or 0)
local c = crypt(m, key)
table.insert(nums, c)
end
return base64.enc48(nums)
end
local function decrypt(data, key)
local bytes = base64.dec(data)
local result = ""
for i = 1, #bytes, 6 do
local c = 0
for j = 0, 4 do
c = c + bytes[i + j]
c = c * 256
end
c = c + bytes[i + 5]
local m = crypt(c, key)
result = result .. string.char(math.floor(m / 4294967296 % 256))
result = result .. string.char(math.floor(m / 16777216 % 256))
result = result .. string.char(math.floor(m / 65536 % 256))
result = result .. string.char(math.floor(m / 256 % 256))
result = result .. string.char(m % 256)
end
return result
end
local function sign(m, key)
local h = hash(m, key[0])
return crypt(h, key)
end
local tArgs = { ... }
local function test()
local pub, pri = genkey()
print(string.format("%.0f : %.0f", unpack(pub)))
print(base64.enc48(pub))
local decpub = base64.dec48(base64.enc48(pub))
print(string.format("%.0f : %.0f", unpack(decpub)))
print(string.format("%.0f : %.0f", unpack(pri)))
print(base64.enc48(pri))
local msg = 65
local enc = encrypt(tArgs[1], pub)
print(enc)
local dec = decrypt(enc, pri)
print(dec)
--[[
local enc = crypt(msg, pub)
local encsign = sign(msg, pri)
local dec = crypt(enc, pri)
local decsign = crypt(encsign, pub)
print("---------")
print(msg, enc, dec)
print(hash(msg, pri[1]), encsign, decsign)
--]]
end
local function dh()
--[[
local ps = primes(2^24)
for n = #ps, 3, -1 do
--print(ps[n])
local ps2 = (ps[n] - 1) / 2
--print("prime: ", ps[n], ps2)
local isprime = true
for i = 1, #ps do
if ps2 == ps[i] then
break
elseif ps2 % ps[i] == 0 then
isprime = false
break
end
end
if isprime then
print(ps2, ps2*2+1)
end
end
--]]
math.randomseed(os.time())
local p = 16776899
local g = 5
local a = math.random(2, 1000)
local A = crypt(g, {p, a})
local b = math.random(2, 1000)
local B = crypt(g, {p, b})
local s = crypt(B, {p, a})
local S = crypt(A, {p, b})
print(s, S)
end
dh()
return {
hash = hash,
genkey = genkey,
gcd = gcd,
inverse = inverse,
crypt = crypt
}