File: test.lua

package info (click to toggle)
lua-md5 1.2%2Bgit%2B1%2B8d87fee-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 276 kB
  • ctags: 120
  • sloc: ansic: 1,082; makefile: 39; sh: 20
file content (128 lines) | stat: -rwxr-xr-x 3,937 bytes parent folder | download | duplicates (3)
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
#!/usr/local/bin/lua50

-- Testing MD5

if string.find(_VERSION, "Lua 5.0") then
	LUA_PATH = "?;?.lua;/usr/local/share/lua/5.0/?.lua"
	string.gmatch = string.gfind
end
if select(1,...) == 'md5' then
local md5 = require"md5"


assert(md5.exor('', '') == '')
assert(md5.exor('alo alo', '\0\0\0\0\0\0\0') == 'alo alo')

local function invert (s)
  return md5.exor(s, string.rep('\255', string.len(s)))
end

x = string.rep('0123456789', 1000)
assert(md5.exor(x,x) == string.rep('\0', 10000))
assert(md5.exor(x,invert(x)) == string.rep('\255', 10000))

assert(invert(invert('alo alo')) == 'alo alo')

assert(invert(invert(invert('alo\0\255alo'))) == invert('alo\0\255alo'))

-- test some known sums
assert(md5.sumhexa("") == "d41d8cd98f00b204e9800998ecf8427e")
assert(md5.sumhexa("a") == "0cc175b9c0f1b6a831c399e269772661")
assert(md5.sumhexa("abc") == "900150983cd24fb0d6963f7d28e17f72")
assert(md5.sumhexa("message digest") == "f96b697d7cb7938d525a2f31aaf161d0")
assert(md5.sumhexa("abcdefghijklmnopqrstuvwxyz") == "c3fcd3d76192e4007dfb496cca67e13b")
assert(md5.sumhexa("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
 == "d174ab98d277d9f5a5611c2c9f419d9f")


-- test padding borders
assert(md5.sumhexa(string.rep('a',53)) == "e9e7e260dce84ffa6e0e7eb5fd9d37fc")
assert(md5.sumhexa(string.rep('a',54)) == "eced9e0b81ef2bba605cbc5e2e76a1d0")
assert(md5.sumhexa(string.rep('a',55)) == "ef1772b6dff9a122358552954ad0df65")
assert(md5.sumhexa(string.rep('a',56)) == "3b0c8ac703f828b04c6c197006d17218")
assert(md5.sumhexa(string.rep('a',57)) == "652b906d60af96844ebd21b674f35e93")
assert(md5.sumhexa(string.rep('a',63)) == "b06521f39153d618550606be297466d5")
assert(md5.sumhexa(string.rep('a',64)) == "014842d480b571495a4a0363793f7367")
assert(md5.sumhexa(string.rep('a',65)) == "c743a45e0d2e6a95cb859adae0248435")
assert(md5.sumhexa(string.rep('a',255)) == "46bc249a5a8fc5d622cf12c42c463ae0")
assert(md5.sumhexa(string.rep('a',256)) == "81109eec5aa1a284fb5327b10e9c16b9")

assert(md5.sumhexa(
"12345678901234567890123456789012345678901234567890123456789012345678901234567890")
   == "57edf4a22be3c955ac49da2e2107b67a")

print '+'



local tolerance = 1.12

local function contchars (s)
  local a = {}
  for i=0,255 do a[string.char(i)] = 0 end
  for c in string.gmatch(s, '.') do
    a[c] = a[c] + 1
  end
  local av = string.len(s)/256
  for i=0,255 do
    local c = string.char(i)
    assert(a[c] < av*tolerance and a[c] > av/tolerance, i)
  end
end


local key = 'xuxu bacana'
assert(md5.decrypt(md5.crypt('', key), key) == '')
assert(md5.decrypt(md5.crypt('', key, '\0\0seed\0'), key) == '')
assert(md5.decrypt(md5.crypt('a', key), key) == 'a')
local msg = string.rep("1233456789\0\1\2\3\0\255", 10000)
local code = md5.crypt(msg, key, "seed")
assert(md5.decrypt(code, key) == msg)
contchars(code)

local seed = 'some_seed'
assert(md5.crypt('a','a',seed) ~= md5.crypt('a','b',seed))

print"MD5 OK"

elseif select(1,...) == 'des56' then

-- Testing DES 56
local des56
if string.find(_VERSION, "Lua 5.0") then
	local cpath = os.getenv"LUA_CPATH" or "/usr/local/lib/lua/5.0/"
	des56 = loadlib(cpath.."des56.so", "luaopen_des56")()
else
	des56 = require 'des56'
end

local key = '&3g4&gs*&3'

assert(des56.decrypt(des56.crypt('', key), key) == '')
assert(des56.decrypt(des56.crypt('', key), key) == '')
assert(des56.decrypt(des56.crypt('a', key), key) == 'a')
assert(des56.decrypt(des56.crypt('1234567890', key), key) == '1234567890')

local msg = string.rep("1233456789\0\1\2\3\0\255", 10000)
local code = des56.crypt(msg, key)

assert(des56.decrypt(code, key) == msg)
assert(des56.crypt('a', '12345678') ~= des56.crypt('a', '87654321'))

local ascii = ""

for i = 0, 255 do
	ascii = ascii..string.char(i)
end

assert(des56.decrypt(des56.crypt(ascii, key), key) == ascii)
key = string.sub(ascii, 2)
assert(des56.decrypt(des56.crypt(ascii, key), key) == ascii)

print"DES56 OK"

else

	error 'Specify an algorithm to test'

end