-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.lua
135 lines (127 loc) · 2.6 KB
/
test.lua
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
local sha2 = require("sha2");
local function test(func, input, expected)
local digest = func(input);
if digest ~= expected then -- lul, thanks
print(input);
print(digest);
print(expected);
error("Test failed");
end
end
--[[
test(sha2.sha224, "abc",
"f5c93b6f06f7c56d7ea720c121e3b1fb6730e5cf5f18d776bf0f2d88");
--]]
test(sha2.sha256, "abc",
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
test(sha2.sha256,
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1");
test(sha2.sha256, ("a"):rep(1e6),
"cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0");
-- test padding
test(sha2.sha256, ("a"):rep(54),
"a3f01b6939256127582ac8ae9fb47a382a244680806a3f613a118851c1ca1d47");
test(sha2.sha256, ("a"):rep(55),
"9f4390f8d30c2dd92ec9f095b65e2b9ae9b0a925a5258e241c9f1e910f734318");
test(sha2.sha256, ("a"):rep(56),
"b35439a4ac6f0948b6d6f9e3c6af0f5f590ce20f1bde7090ef7970686ec6738a");
test(sha2.sha256, ("a"):rep(64+54),
"879dc4f05b19ebc3b037f4683633df1332b054cf52fa372d323c421cb893a2aa");
test(sha2.sha256, ("a"):rep(64+55),
"31eba51c313a5c08226adf18d4a359cfdfd8d2e816b13f4af952f7ea6584dcfb");
test(sha2.sha256, ("a"):rep(64+56),
"2f3d335432c70b580af0e8e1b3674a7c020d683aa5f73aaaedfdc55af904c21c");
local s = ("a"):rep(200* 2^20); -- x * 1MB
local sha256 = sha2.sha256;
local time = os.clock();
local output = sha256(s);
print(os.clock()-time, output);
-- 3.93
-- 3.24
-- 3.35, 3.31, 3.3
-- 3.2, 3.18, 3.18
-- 3.13, 3.11
-- 3.08, 3.13, 3.1, 3.03
-- 2.91
--[==[
do
local s = [[
428a2f98
d807aa98
e49b69c1
983e5152
27b70a85
a2bfe8a1
19a4c116
748f82ee
71374491
12835b01
efbe4786
a831c66d
2e1b2138
a81a664b
1e376c08
78a5636f
b5c0fbcf
243185be
0fc19dc6
b00327c8
4d2c6dfc
c24b8b70
2748774c
84c87814
e9b5dba5
550c7dc3
240ca1cc
bf597fc7
53380d13
c76c51a3
34b0bcb5
8cc70208
3956c25b
72be5d74
2de92c6f
c6e00bf3
650a7354
d192e819
391c0cb3
90befffa
59f111f1
80deb1fe
4a7484aa
d5a79147
766a0abb
d6990624
4ed8aa4a
a4506ceb
923f82a4
9bdc06a7
5cb0a9dc
06ca6351
81c2c92e
f40e3585
5b9cca4f
bef9a3f7
ab1c5ed5
c19bf174
76f988da
14292967
92722c85
106aa070
682e6ff3
c67178f2
]]
local words = {}; for word in s:gmatch("(%x+)%s") do words[#words + 1] = tonumber(word,16); end
local right = true;
for i = 1, 8 do
for y = 1, 2 do
local mini = {};
for j = 1, 4 do
mini[#mini + 1] = words[(y==1 and 0 or 32)+i+(j-1)*8]
end
print(("0x%08x, 0x%08x, 0x%08x, 0x%08x,"):format(unpack(mini)));
end
end
end
--]==]