Annotate

util/hmac.lua @ 1502:0f895c06e03f

mod_console: Check for commands when not executing in the global environment
author Matthew Wild <mwild1@gmail.com>
date Thu, 09 Jul 2009 04:34:55 +0100
parent 1482:9734231a569f
child 1516:4c9bd0527d1d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1456
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
1 local hashes = require "util.hashes"
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
2 local xor = require "bit".bxor
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
3
1482
9734231a569f util.hmac: Some optimisations
Matthew Wild <mwild1@gmail.com>
parents: 1481
diff changeset
4 local t_insert, t_concat = table.insert, table.concat;
9734231a569f util.hmac: Some optimisations
Matthew Wild <mwild1@gmail.com>
parents: 1481
diff changeset
5 local s_char = string.char;
9734231a569f util.hmac: Some optimisations
Matthew Wild <mwild1@gmail.com>
parents: 1481
diff changeset
6
1456
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
7 module "hmac"
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
8
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
9 local function arraystr(array)
1481
6401460891b2 util.hmac: Fix a global set
Matthew Wild <mwild1@gmail.com>
parents: 1456
diff changeset
10 local t = {}
1482
9734231a569f util.hmac: Some optimisations
Matthew Wild <mwild1@gmail.com>
parents: 1481
diff changeset
11 for i = 1,#array do
9734231a569f util.hmac: Some optimisations
Matthew Wild <mwild1@gmail.com>
parents: 1481
diff changeset
12 t_insert(t, s_char(array[i]))
1456
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
13 end
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
14
1482
9734231a569f util.hmac: Some optimisations
Matthew Wild <mwild1@gmail.com>
parents: 1481
diff changeset
15 return t_concat(t)
1456
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
16 end
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
17
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
18 --[[
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
19 key
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
20 the key to use in the hash
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
21 message
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
22 the message to hash
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
23 hash
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
24 the hash function
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
25 blocksize
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
26 the blocksize for the hash function in bytes
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
27 hex
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
28 return raw hash or hexadecimal string
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
29 --]]
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
30 function hmac(key, message, hash, blocksize, hex)
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
31 local opad = {}
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
32 local ipad = {}
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
33
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
34 for i = 1,blocksize do
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
35 opad[i] = 0x5c
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
36 ipad[i] = 0x36
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
37 end
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
38
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
39 if #key > blocksize then
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
40 key = hash(key)
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
41 end
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
42
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
43 for i = 1,#key do
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
44 ipad[i] = xor(ipad[i],key:sub(i,i):byte())
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
45 opad[i] = xor(opad[i],key:sub(i,i):byte())
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
46 end
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
47
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
48 opad = arraystr(opad)
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
49 ipad = arraystr(ipad)
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
50
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
51 if hex then
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
52 return hash(opad..hash(ipad..message), true)
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
53 else
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
54 return hash(opad..hash(ipad..message))
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
55 end
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
56 end
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
57
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
58 function md5(key, message, hex)
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
59 return hmac(key, message, hashes.md5, 64, hex)
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
60 end
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
61
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
62 function sha1(key, message, hex)
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
63 return hmac(key, message, hashes.sha1, 64, hex)
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
64 end
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
65
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
66 function sha256(key, message, hex)
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
67 return hmac(key, message, hashes.sha256, 64, hex)
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
68 end
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
69
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
70 return _M