Annotate

util/hmac.lua @ 1943:3d4c703d9333

mod_tls: Don't try to start TLS if we can't actually do it (thanks Florob)
author Matthew Wild <mwild1@gmail.com>
date Fri, 09 Oct 2009 17:48:45 +0100
parent 1522:569d58d21612
child 2268:770a75623ed7
child 2923:b7049746bd29
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1516
diff changeset
1 -- Prosody IM
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1516
diff changeset
2 -- Copyright (C) 2008-2009 Matthew Wild
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1516
diff changeset
3 -- Copyright (C) 2008-2009 Waqas Hussain
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1516
diff changeset
4 --
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1516
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1516
diff changeset
6 -- COPYING file in the source package for more information.
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1516
diff changeset
7 --
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1516
diff changeset
8
1456
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
9 local hashes = require "util.hashes"
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
10 local xor = require "bit".bxor
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
11
1482
9734231a569f util.hmac: Some optimisations
Matthew Wild <mwild1@gmail.com>
parents: 1481
diff changeset
12 local t_insert, t_concat = table.insert, table.concat;
9734231a569f util.hmac: Some optimisations
Matthew Wild <mwild1@gmail.com>
parents: 1481
diff changeset
13 local s_char = string.char;
9734231a569f util.hmac: Some optimisations
Matthew Wild <mwild1@gmail.com>
parents: 1481
diff changeset
14
1456
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
15 module "hmac"
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
16
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
17 local function arraystr(array)
1516
4c9bd0527d1d util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents: 1482
diff changeset
18 local t = {}
4c9bd0527d1d util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents: 1482
diff changeset
19 for i = 1,#array do
4c9bd0527d1d util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents: 1482
diff changeset
20 t_insert(t, s_char(array[i]))
4c9bd0527d1d util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents: 1482
diff changeset
21 end
1456
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
22
1516
4c9bd0527d1d util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents: 1482
diff changeset
23 return t_concat(t)
1456
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
24 end
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
25
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
26 --[[
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
27 key
1516
4c9bd0527d1d util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents: 1482
diff changeset
28 the key to use in the hash
1456
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
29 message
1516
4c9bd0527d1d util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents: 1482
diff changeset
30 the message to hash
1456
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
31 hash
1516
4c9bd0527d1d util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents: 1482
diff changeset
32 the hash function
1456
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
33 blocksize
1516
4c9bd0527d1d util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents: 1482
diff changeset
34 the blocksize for the hash function in bytes
1456
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
35 hex
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
36 return raw hash or hexadecimal string
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
37 --]]
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
38 function hmac(key, message, hash, blocksize, hex)
1516
4c9bd0527d1d util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents: 1482
diff changeset
39 local opad = {}
4c9bd0527d1d util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents: 1482
diff changeset
40 local ipad = {}
4c9bd0527d1d util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents: 1482
diff changeset
41
4c9bd0527d1d util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents: 1482
diff changeset
42 for i = 1,blocksize do
4c9bd0527d1d util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents: 1482
diff changeset
43 opad[i] = 0x5c
4c9bd0527d1d util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents: 1482
diff changeset
44 ipad[i] = 0x36
4c9bd0527d1d util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents: 1482
diff changeset
45 end
1456
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
46
1516
4c9bd0527d1d util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents: 1482
diff changeset
47 if #key > blocksize then
4c9bd0527d1d util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents: 1482
diff changeset
48 key = hash(key)
4c9bd0527d1d util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents: 1482
diff changeset
49 end
1456
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
50
1516
4c9bd0527d1d util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents: 1482
diff changeset
51 for i = 1,#key do
4c9bd0527d1d util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents: 1482
diff changeset
52 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
53 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
54 end
1456
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
55
1516
4c9bd0527d1d util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents: 1482
diff changeset
56 opad = arraystr(opad)
4c9bd0527d1d util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents: 1482
diff changeset
57 ipad = arraystr(ipad)
1456
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
58
1516
4c9bd0527d1d util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents: 1482
diff changeset
59 if hex then
4c9bd0527d1d util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents: 1482
diff changeset
60 return hash(opad..hash(ipad..message), true)
4c9bd0527d1d util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents: 1482
diff changeset
61 else
4c9bd0527d1d util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents: 1482
diff changeset
62 return hash(opad..hash(ipad..message))
4c9bd0527d1d util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents: 1482
diff changeset
63 end
1456
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 md5(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.md5, 64, hex)
1456
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 function sha1(key, message, hex)
1516
4c9bd0527d1d util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents: 1482
diff changeset
71 return hmac(key, message, hashes.sha1, 64, hex)
1456
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
72 end
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
73
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
74 function sha256(key, message, hex)
1516
4c9bd0527d1d util.hmac: Convert spaces to tabs
Matthew Wild <mwild1@gmail.com>
parents: 1482
diff changeset
75 return hmac(key, message, hashes.sha256, 64, hex)
1456
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
76 end
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
77
3135cf40110d Added HMAC utility module
Dwayne Bent <dbb.0@liqd.org>
parents:
diff changeset
78 return _M