Mercurial > prosody-hg
annotate util/hmac.lua @ 1516:4c9bd0527d1d
util.hmac: Convert spaces to tabs
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Fri, 10 Jul 2009 02:19:51 +0100 |
| parents | 9734231a569f |
| children | 569d58d21612 |
| rev | line source |
|---|---|
| 1456 | 1 local hashes = require "util.hashes" |
| 2 local xor = require "bit".bxor | |
| 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 | 7 module "hmac" |
| 8 | |
| 9 local function arraystr(array) | |
|
1516
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
10 local t = {} |
|
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
11 for i = 1,#array do |
|
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
12 t_insert(t, s_char(array[i])) |
|
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
13 end |
| 1456 | 14 |
|
1516
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
15 return t_concat(t) |
| 1456 | 16 end |
| 17 | |
| 18 --[[ | |
| 19 key | |
|
1516
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
20 the key to use in the hash |
| 1456 | 21 message |
|
1516
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
22 the message to hash |
| 1456 | 23 hash |
|
1516
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
24 the hash function |
| 1456 | 25 blocksize |
|
1516
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
26 the blocksize for the hash function in bytes |
| 1456 | 27 hex |
| 28 return raw hash or hexadecimal string | |
| 29 --]] | |
| 30 function hmac(key, message, hash, blocksize, hex) | |
|
1516
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
31 local opad = {} |
|
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
32 local ipad = {} |
|
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
33 |
|
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
34 for i = 1,blocksize do |
|
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
35 opad[i] = 0x5c |
|
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
36 ipad[i] = 0x36 |
|
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
37 end |
| 1456 | 38 |
|
1516
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
39 if #key > blocksize then |
|
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
40 key = hash(key) |
|
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
41 end |
| 1456 | 42 |
|
1516
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
43 for i = 1,#key do |
|
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
44 ipad[i] = xor(ipad[i],key:sub(i,i):byte()) |
|
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
45 opad[i] = xor(opad[i],key:sub(i,i):byte()) |
|
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
46 end |
| 1456 | 47 |
|
1516
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
48 opad = arraystr(opad) |
|
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
49 ipad = arraystr(ipad) |
| 1456 | 50 |
|
1516
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
51 if hex then |
|
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
52 return hash(opad..hash(ipad..message), true) |
|
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
53 else |
|
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
54 return hash(opad..hash(ipad..message)) |
|
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
55 end |
| 1456 | 56 end |
| 57 | |
| 58 function md5(key, message, hex) | |
|
1516
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
59 return hmac(key, message, hashes.md5, 64, hex) |
| 1456 | 60 end |
| 61 | |
| 62 function sha1(key, message, hex) | |
|
1516
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
63 return hmac(key, message, hashes.sha1, 64, hex) |
| 1456 | 64 end |
| 65 | |
| 66 function sha256(key, message, hex) | |
|
1516
4c9bd0527d1d
util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents:
1482
diff
changeset
|
67 return hmac(key, message, hashes.sha256, 64, hex) |
| 1456 | 68 end |
| 69 | |
| 70 return _M |
