| 1 |
#!/usr/bin/env lua5.0 |
| 2 |
|
| 3 |
dofile "md5.lua" |
| 4 |
|
| 5 |
|
| 6 |
assert(md5.exor('', '') == '') |
| 7 |
assert(md5.exor('alo alo', '\0\0\0\0\0\0\0') == 'alo alo') |
| 8 |
|
| 9 |
local function invert (s) |
| 10 |
return md5.exor(s, string.rep('\255', string.len(s))) |
| 11 |
end |
| 12 |
|
| 13 |
x = string.rep('0123456789', 1000) |
| 14 |
assert(md5.exor(x,x) == string.rep('\0', 10000)) |
| 15 |
assert(md5.exor(x,invert(x)) == string.rep('\255', 10000)) |
| 16 |
|
| 17 |
assert(invert(invert('alo alo')) == 'alo alo') |
| 18 |
|
| 19 |
assert(invert(invert(invert('alo\0\255alo'))) == invert('alo\0\255alo')) |
| 20 |
|
| 21 |
|
| 22 |
assert(md5.sumhexa("") == "d41d8cd98f00b204e9800998ecf8427e") |
| 23 |
assert(md5.sumhexa("a") == "0cc175b9c0f1b6a831c399e269772661") |
| 24 |
assert(md5.sumhexa("abc") == "900150983cd24fb0d6963f7d28e17f72") |
| 25 |
assert(md5.sumhexa("message digest") == "f96b697d7cb7938d525a2f31aaf161d0") |
| 26 |
assert(md5.sumhexa("abcdefghijklmnopqrstuvwxyz") == "c3fcd3d76192e4007dfb496cca67e13b") |
| 27 |
assert(md5.sumhexa("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") |
| 28 |
== "d174ab98d277d9f5a5611c2c9f419d9f") |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
assert(md5.sumhexa(string.rep('a',53)) == "e9e7e260dce84ffa6e0e7eb5fd9d37fc") |
| 33 |
assert(md5.sumhexa(string.rep('a',54)) == "eced9e0b81ef2bba605cbc5e2e76a1d0") |
| 34 |
assert(md5.sumhexa(string.rep('a',55)) == "ef1772b6dff9a122358552954ad0df65") |
| 35 |
assert(md5.sumhexa(string.rep('a',56)) == "3b0c8ac703f828b04c6c197006d17218") |
| 36 |
assert(md5.sumhexa(string.rep('a',57)) == "652b906d60af96844ebd21b674f35e93") |
| 37 |
assert(md5.sumhexa(string.rep('a',63)) == "b06521f39153d618550606be297466d5") |
| 38 |
assert(md5.sumhexa(string.rep('a',64)) == "014842d480b571495a4a0363793f7367") |
| 39 |
assert(md5.sumhexa(string.rep('a',65)) == "c743a45e0d2e6a95cb859adae0248435") |
| 40 |
assert(md5.sumhexa(string.rep('a',255)) == "46bc249a5a8fc5d622cf12c42c463ae0") |
| 41 |
assert(md5.sumhexa(string.rep('a',256)) == "81109eec5aa1a284fb5327b10e9c16b9") |
| 42 |
|
| 43 |
assert(md5.sumhexa( |
| 44 |
"12345678901234567890123456789012345678901234567890123456789012345678901234567890") |
| 45 |
== "57edf4a22be3c955ac49da2e2107b67a") |
| 46 |
|
| 47 |
print '+' |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
local tolerance = 1.12 |
| 52 |
|
| 53 |
local function contchars (s) |
| 54 |
local a = {} |
| 55 |
for i=0,255 do a[string.char(i)] = 0 end |
| 56 |
for c in string.gfind(s, '.') do |
| 57 |
a[c] = a[c] + 1 |
| 58 |
end |
| 59 |
local av = string.len(s)/256 |
| 60 |
for i=0,255 do |
| 61 |
local c = string.char(i) |
| 62 |
assert(a[c] < av*tolerance and a[c] > av/tolerance, i) |
| 63 |
end |
| 64 |
end |
| 65 |
|
| 66 |
|
| 67 |
local key = 'xuxu bacana' |
| 68 |
assert(md5.decrypt(md5.crypt('', key), key) == '') |
| 69 |
assert(md5.decrypt(md5.crypt('', key, '\0\0seed\0'), key) == '') |
| 70 |
assert(md5.decrypt(md5.crypt('a', key), key) == 'a') |
| 71 |
local msg = string.rep("1233456789\0\1\2\3\0\255", 10000) |
| 72 |
local code = md5.crypt(msg, key, "seed") |
| 73 |
assert(md5.decrypt(code, key) == msg) |
| 74 |
contchars(code) |
| 75 |
|
| 76 |
assert(md5.crypt('a', 'a') ~= md5.crypt('a', 'b')) |
| 77 |
|
| 78 |
print"OK" |