util/sasl/scram.lua

changeset 2268
0a675bb365a9
parent 2267
6e662e4f7ab2
child 2288
ef7027a0f0c9
equal deleted inserted replaced
2267:6e662e4f7ab2 2268:0a675bb365a9
19 local hmac_sha1 = require "util.hmac".sha1; 19 local hmac_sha1 = require "util.hmac".sha1;
20 local sha1 = require "util.hashes".sha1; 20 local sha1 = require "util.hashes".sha1;
21 local generate_uuid = require "util.uuid".generate; 21 local generate_uuid = require "util.uuid".generate;
22 local saslprep = require "util.encodings".stringprep.saslprep; 22 local saslprep = require "util.encodings".stringprep.saslprep;
23 local log = require "util.logger".init("sasl"); 23 local log = require "util.logger".init("sasl");
24 local t_concat = table.concat;
25 local char = string.char;
26 local byte = string.byte;
24 27
25 module "scram" 28 module "scram"
26 29
27 --========================= 30 --=========================
28 --SASL SCRAM-SHA-1 according to draft-ietf-sasl-scram-10 31 --SASL SCRAM-SHA-1 according to draft-ietf-sasl-scram-10
34 result = result.."\\"..b:byte(i) 37 result = result.."\\"..b:byte(i)
35 end 38 end
36 return result 39 return result
37 end 40 end
38 41
42 local xor_map = {0;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;1;0;3;2;5;4;7;6;9;8;11;10;13;12;15;14;2;3;0;1;6;7;4;5;10;11;8;9;14;15;12;13;3;2;1;0;7;6;5;4;11;10;9;8;15;14;13;12;4;5;6;7;0;1;2;3;12;13;14;15;8;9;10;11;5;4;7;6;1;0;3;2;13;12;15;14;9;8;11;10;6;7;4;5;2;3;0;1;14;15;12;13;10;11;8;9;7;6;5;4;3;2;1;0;15;14;13;12;11;10;9;8;8;9;10;11;12;13;14;15;0;1;2;3;4;5;6;7;9;8;11;10;13;12;15;14;1;0;3;2;5;4;7;6;10;11;8;9;14;15;12;13;2;3;0;1;6;7;4;5;11;10;9;8;15;14;13;12;3;2;1;0;7;6;5;4;12;13;14;15;8;9;10;11;4;5;6;7;0;1;2;3;13;12;15;14;9;8;11;10;5;4;7;6;1;0;3;2;14;15;12;13;10;11;8;9;6;7;4;5;2;3;0;1;15;14;13;12;11;10;9;8;7;6;5;4;3;2;1;0;};
43
44 local result = {};
39 local function binaryXOR( a, b ) 45 local function binaryXOR( a, b )
40 if a:len() > b:len() then 46 for i=1, #a do
41 b = string.rep("\0", a:len() - b:len())..b 47 local x, y = byte(a, i), byte(b, i);
42 elseif string.len(a) < string.len(b) then 48 local lowx, lowy = x % 16, y % 16;
43 a = string.rep("\0", b:len() - a:len())..a 49 local hix, hiy = (x - lowx) / 16, (y - lowy) / 16;
50 local lowr, hir = xor_map[lowx * 16 + lowy + 1], xor_map[hix * 16 + hiy + 1];
51 local r = hir * 16 + lowr;
52 result[i] = char(r)
44 end 53 end
45 local result = "" 54 return t_concat(result);
46 for i=1, a:len() do
47 result = result..string.char(xor(a:byte(i), b:byte(i)))
48 end
49 return result
50 end 55 end
51 56
52 -- hash algorithm independent Hi(PBKDF2) implementation 57 -- hash algorithm independent Hi(PBKDF2) implementation
53 local function Hi(hmac, str, salt, i) 58 local function Hi(hmac, str, salt, i)
54 local Ust = hmac(str, salt.."\0\0\0\1"); 59 local Ust = hmac(str, salt.."\0\0\0\1");

mercurial