Wyvern67

Sha1 lib by regex.info

Jan 25th, 2013
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 30.28 KB | None | 0 0
  1. local function ZERO()
  2.    return {
  3.       false, false, false, false,     false, false, false, false,
  4.       false, false, false, false,     false, false, false, false,
  5.       false, false, false, false,     false, false, false, false,
  6.       false, false, false, false,     false, false, false, false,
  7.    }
  8. end
  9.  
  10. local hex_to_bits = {
  11.    ["0"] = { false, false, false, false },
  12.    ["1"] = { false, false, false, true  },
  13.    ["2"] = { false, false, true,  false },
  14.    ["3"] = { false, false, true,  true  },
  15.  
  16.    ["4"] = { false, true,  false, false },
  17.    ["5"] = { false, true,  false, true  },
  18.    ["6"] = { false, true,  true,  false },
  19.    ["7"] = { false, true,  true,  true  },
  20.  
  21.    ["8"] = { true,  false, false, false },
  22.    ["9"] = { true,  false, false, true  },
  23.    ["A"] = { true,  false, true,  false },
  24.    ["B"] = { true,  false, true,  true  },
  25.  
  26.    ["C"] = { true,  true,  false, false },
  27.    ["D"] = { true,  true,  false, true  },
  28.    ["E"] = { true,  true,  true,  false },
  29.    ["F"] = { true,  true,  true,  true  },
  30.  
  31.    ["a"] = { true,  false, true,  false },
  32.    ["b"] = { true,  false, true,  true  },
  33.    ["c"] = { true,  true,  false, false },
  34.    ["d"] = { true,  true,  false, true  },
  35.    ["e"] = { true,  true,  true,  false },
  36.    ["f"] = { true,  true,  true,  true  },
  37. }
  38.  
  39. --
  40. -- Given a string of 8 hex digits, return a W32 object representing that number
  41. --
  42. local function from_hex(hex)
  43.  
  44.    assert(type(hex) == 'string')
  45.    assert(hex:match('^[0123456789abcdefABCDEF]+$'))
  46.    assert(#hex == 8)
  47.  
  48.    local W32 = { }
  49.  
  50.    for letter in hex:gmatch('.') do
  51.       local b = hex_to_bits[letter]
  52.       assert(b)
  53.       table.insert(W32, 1, b[1])
  54.       table.insert(W32, 1, b[2])
  55.       table.insert(W32, 1, b[3])
  56.       table.insert(W32, 1, b[4])
  57.    end
  58.  
  59.    return W32
  60. end
  61.  
  62. local function COPY(old)
  63.    local W32 = { }
  64.    for k,v in pairs(old) do
  65.       W32[k] = v
  66.    end
  67.  
  68.    return W32
  69. end
  70.  
  71. local function ADD(first, ...)
  72.  
  73.    local a = COPY(first)
  74.  
  75.    local C, b, sum
  76.  
  77.    for v = 1, select('#', ...) do
  78.       b = select(v, ...)
  79.       C = 0
  80.  
  81.       for i = 1, #a do
  82.          sum = (a[i] and 1 or 0)
  83.              + (b[i] and 1 or 0)
  84.              + C
  85.  
  86.          if sum == 0 then
  87.             a[i] = false
  88.             C    = 0
  89.          elseif sum == 1 then
  90.             a[i] = true
  91.             C    = 0
  92.          elseif sum == 2 then
  93.             a[i] = false
  94.             C    = 1
  95.          else
  96.             a[i] = true
  97.             C    = 1
  98.          end
  99.       end
  100.       -- we drop any ending carry
  101.  
  102.    end
  103.  
  104.    return a
  105. end
  106.  
  107. local function XOR(first, ...)
  108.  
  109.    local a = COPY(first)
  110.    local b
  111.    for v = 1, select('#', ...) do
  112.       b = select(v, ...)
  113.       for i = 1, #a do
  114.          a[i] = a[i] ~= b[i]
  115.       end
  116.    end
  117.  
  118.    return a
  119.  
  120. end
  121.  
  122. local function AND(a, b)
  123.  
  124.    local c = ZERO()
  125.  
  126.    for i = 1, #a do
  127.       -- only need to set true bits; other bits remain false
  128.       if  a[i] and b[i] then
  129.          c[i] = true
  130.       end
  131.    end
  132.  
  133.    return c
  134. end
  135.  
  136. local function OR(a, b)
  137.  
  138.    local c = ZERO()
  139.  
  140.    for i = 1, #a do
  141.       -- only need to set true bits; other bits remain false
  142.       if  a[i] or b[i] then
  143.          c[i] = true
  144.       end
  145.    end
  146.  
  147.    return c
  148. end
  149.  
  150. local function OR3(a, b, c)
  151.  
  152.    local d = ZERO()
  153.  
  154.    for i = 1, #a do
  155.       -- only need to set true bits; other bits remain false
  156.       if a[i] or b[i] or c[i] then
  157.          d[i] = true
  158.       end
  159.    end
  160.  
  161.    return d
  162. end
  163.  
  164. local function NOT(a)
  165.  
  166.    local b = ZERO()
  167.  
  168.    for i = 1, #a do
  169.       -- only need to set true bits; other bits remain false
  170.       if not a[i] then
  171.          b[i] = true
  172.       end
  173.    end
  174.  
  175.    return b
  176. end
  177.  
  178. local function ROTATE(bits, a)
  179.  
  180.    local b = COPY(a)
  181.  
  182.    while bits > 0 do
  183.       bits = bits - 1
  184.       table.insert(b, 1, table.remove(b))
  185.    end
  186.  
  187.    return b
  188.  
  189. end
  190.  
  191.  
  192. local binary_to_hex = {
  193.    ["0000"] = "0",
  194.    ["0001"] = "1",
  195.    ["0010"] = "2",
  196.    ["0011"] = "3",
  197.    ["0100"] = "4",
  198.    ["0101"] = "5",
  199.    ["0110"] = "6",
  200.    ["0111"] = "7",
  201.    ["1000"] = "8",
  202.    ["1001"] = "9",
  203.    ["1010"] = "a",
  204.    ["1011"] = "b",
  205.    ["1100"] = "c",
  206.    ["1101"] = "d",
  207.    ["1110"] = "e",
  208.    ["1111"] = "f",
  209. }
  210.  
  211. function asHEX(a)
  212.  
  213.    local hex = ""
  214.    local i = 1
  215.    while i < #a do
  216.       local binary = (a[i + 3] and '1' or '0')
  217.                      ..
  218.                      (a[i + 2] and '1' or '0')
  219.                      ..
  220.                      (a[i + 1] and '1' or '0')
  221.                      ..
  222.                      (a[i + 0] and '1' or '0')
  223.  
  224.       hex = binary_to_hex[binary] .. hex
  225.  
  226.       i = i + 4
  227.    end
  228.  
  229.    return hex
  230.  
  231. end
  232.  
  233. local x67452301 = from_hex("67452301")
  234. local xEFCDAB89 = from_hex("EFCDAB89")
  235. local x98BADCFE = from_hex("98BADCFE")
  236. local x10325476 = from_hex("10325476")
  237. local xC3D2E1F0 = from_hex("C3D2E1F0")
  238.  
  239. local x5A827999 = from_hex("5A827999")
  240. local x6ED9EBA1 = from_hex("6ED9EBA1")
  241. local x8F1BBCDC = from_hex("8F1BBCDC")
  242. local xCA62C1D6 = from_hex("CA62C1D6")
  243.  
  244.  
  245. function sha1(msg)
  246.  
  247.    assert(type(msg) == 'string')
  248.    assert(#msg < 0x7FFFFFFF) -- have no idea what would happen if it were large
  249.  
  250.    local H0 = x67452301
  251.    local H1 = xEFCDAB89
  252.    local H2 = x98BADCFE
  253.    local H3 = x10325476
  254.    local H4 = xC3D2E1F0
  255.  
  256.    local msg_len_in_bits = #msg * 8
  257.  
  258.    local first_append = string.char(0x80) -- append a '1' bit plus seven '0' bits
  259.  
  260.    local non_zero_message_bytes = #msg +1 +8 -- the +1 is the appended bit 1, the +8 are for the final appended length
  261.    local current_mod = non_zero_message_bytes % 64
  262.    local second_append = ""
  263.    if current_mod ~= 0 then
  264.       second_append = string.rep(string.char(0), 64 - current_mod)
  265.    end
  266.  
  267.    -- now to append the length as a 64-bit number.
  268.    local B1, R1 = math.modf(msg_len_in_bits  / 0x01000000)
  269.    local B2, R2 = math.modf( 0x01000000 * R1 / 0x00010000)
  270.    local B3, R3 = math.modf( 0x00010000 * R2 / 0x00000100)
  271.    local B4     =            0x00000100 * R3
  272.  
  273.    local L64 = string.char( 0) .. string.char( 0) .. string.char( 0) .. string.char( 0) -- high 32 bits
  274.             .. string.char(B1) .. string.char(B2) .. string.char(B3) .. string.char(B4) --  low 32 bits
  275.  
  276.  
  277.  
  278.    msg = msg .. first_append .. second_append .. L64        
  279.  
  280.    assert(#msg % 64 == 0)
  281.  
  282.    --local fd = io.open("/tmp/msg", "wb")
  283.    --fd:write(msg)
  284.    --fd:close()
  285.  
  286.    local chunks = #msg / 64
  287.  
  288.    local W = { }
  289.    local start, A, B, C, D, E, f, K, TEMP
  290.    local chunk = 0
  291.  
  292.    while chunk < chunks do
  293.       --
  294.       -- break chunk up into W[0] through W[15]
  295.       --
  296.       start = chunk * 64 + 1
  297.       chunk = chunk + 1
  298.  
  299.       for t = 0, 15 do
  300.          W[t] = from_hex(string.format("%02x%02x%02x%02x", msg:byte(start, start + 3)))
  301.          start = start + 4
  302.       end
  303.  
  304.       --
  305.       -- build W[16] through W[79]
  306.       --
  307.       for t = 16, 79 do
  308.          -- For t = 16 to 79 let Wt = S1(Wt-3 XOR Wt-8 XOR Wt-14 XOR Wt-16).
  309.          W[t] = ROTATE(1, XOR(W[t-3], W[t-8], W[t-14], W[t-16]))
  310.       end
  311.  
  312.       A = H0
  313.       B = H1
  314.       C = H2
  315.       D = H3
  316.       E = H4
  317.  
  318.       for t = 0, 79 do
  319.          if t <= 19 then
  320.             -- (B AND C) OR ((NOT B) AND D)
  321.             f = OR(AND(B, C), AND(NOT(B), D))
  322.             K = x5A827999
  323.          elseif t <= 39 then
  324.             -- B XOR C XOR D
  325.             f = XOR(B, C, D)
  326.             K = x6ED9EBA1
  327.          elseif t <= 59 then
  328.             -- (B AND C) OR (B AND D) OR (C AND D
  329.             f = OR3(AND(B, C), AND(B, D), AND(C, D))
  330.             K = x8F1BBCDC
  331.          else
  332.             -- B XOR C XOR D
  333.             f = XOR(B, C, D)
  334.             K = xCA62C1D6
  335.          end
  336.  
  337.          -- TEMP = S5(A) + ft(B,C,D) + E + Wt + Kt;
  338.          TEMP = ADD(ROTATE(5, A), f, E, W[t], K)
  339.  
  340.          --E = D;   D = C;    C = S30(B);   B = A;   A = TEMP;
  341.          E = D
  342.          D = C
  343.          C = ROTATE(30, B)
  344.          B = A
  345.          A = TEMP
  346.  
  347.          --printf("t = %2d: %s  %s  %s  %s  %s", t, A:HEX(), B:HEX(), C:HEX(), D:HEX(), E:HEX())
  348.       end
  349.  
  350.       -- Let H0 = H0 + A, H1 = H1 + B, H2 = H2 + C, H3 = H3 + D, H4 = H4 + E.
  351.       H0 = ADD(H0, A)
  352.       H1 = ADD(H1, B)
  353.       H2 = ADD(H2, C)
  354.       H3 = ADD(H3, D)
  355.       H4 = ADD(H4, E)
  356.    end
  357.  
  358.    return asHEX(H0) .. asHEX(H1) .. asHEX(H2) .. asHEX(H3) .. asHEX(H4)
  359. end
  360.  
  361. local function hex_to_binary(hex)
  362.    return hex:gsub('..', function(hexval)
  363.                             return string.char(tonumber(hexval, 16))
  364.                          end)
  365. end
  366.  
  367. function sha1_binary(msg)
  368.    return hex_to_binary(sha1(msg))
  369. end
  370.  
  371. local xor_with_0x5c = {
  372.    [string.char(  0)] = string.char( 92),   [string.char(  1)] = string.char( 93),
  373.    [string.char(  2)] = string.char( 94),   [string.char(  3)] = string.char( 95),
  374.    [string.char(  4)] = string.char( 88),   [string.char(  5)] = string.char( 89),
  375.    [string.char(  6)] = string.char( 90),   [string.char(  7)] = string.char( 91),
  376.    [string.char(  8)] = string.char( 84),   [string.char(  9)] = string.char( 85),
  377.    [string.char( 10)] = string.char( 86),   [string.char( 11)] = string.char( 87),
  378.    [string.char( 12)] = string.char( 80),   [string.char( 13)] = string.char( 81),
  379.    [string.char( 14)] = string.char( 82),   [string.char( 15)] = string.char( 83),
  380.    [string.char( 16)] = string.char( 76),   [string.char( 17)] = string.char( 77),
  381.    [string.char( 18)] = string.char( 78),   [string.char( 19)] = string.char( 79),
  382.    [string.char( 20)] = string.char( 72),   [string.char( 21)] = string.char( 73),
  383.    [string.char( 22)] = string.char( 74),   [string.char( 23)] = string.char( 75),
  384.    [string.char( 24)] = string.char( 68),   [string.char( 25)] = string.char( 69),
  385.    [string.char( 26)] = string.char( 70),   [string.char( 27)] = string.char( 71),
  386.    [string.char( 28)] = string.char( 64),   [string.char( 29)] = string.char( 65),
  387.    [string.char( 30)] = string.char( 66),   [string.char( 31)] = string.char( 67),
  388.    [string.char( 32)] = string.char(124),   [string.char( 33)] = string.char(125),
  389.    [string.char( 34)] = string.char(126),   [string.char( 35)] = string.char(127),
  390.    [string.char( 36)] = string.char(120),   [string.char( 37)] = string.char(121),
  391.    [string.char( 38)] = string.char(122),   [string.char( 39)] = string.char(123),
  392.    [string.char( 40)] = string.char(116),   [string.char( 41)] = string.char(117),
  393.    [string.char( 42)] = string.char(118),   [string.char( 43)] = string.char(119),
  394.    [string.char( 44)] = string.char(112),   [string.char( 45)] = string.char(113),
  395.    [string.char( 46)] = string.char(114),   [string.char( 47)] = string.char(115),
  396.    [string.char( 48)] = string.char(108),   [string.char( 49)] = string.char(109),
  397.    [string.char( 50)] = string.char(110),   [string.char( 51)] = string.char(111),
  398.    [string.char( 52)] = string.char(104),   [string.char( 53)] = string.char(105),
  399.    [string.char( 54)] = string.char(106),   [string.char( 55)] = string.char(107),
  400.    [string.char( 56)] = string.char(100),   [string.char( 57)] = string.char(101),
  401.    [string.char( 58)] = string.char(102),   [string.char( 59)] = string.char(103),
  402.    [string.char( 60)] = string.char( 96),   [string.char( 61)] = string.char( 97),
  403.    [string.char( 62)] = string.char( 98),   [string.char( 63)] = string.char( 99),
  404.    [string.char( 64)] = string.char( 28),   [string.char( 65)] = string.char( 29),
  405.    [string.char( 66)] = string.char( 30),   [string.char( 67)] = string.char( 31),
  406.    [string.char( 68)] = string.char( 24),   [string.char( 69)] = string.char( 25),
  407.    [string.char( 70)] = string.char( 26),   [string.char( 71)] = string.char( 27),
  408.    [string.char( 72)] = string.char( 20),   [string.char( 73)] = string.char( 21),
  409.    [string.char( 74)] = string.char( 22),   [string.char( 75)] = string.char( 23),
  410.    [string.char( 76)] = string.char( 16),   [string.char( 77)] = string.char( 17),
  411.    [string.char( 78)] = string.char( 18),   [string.char( 79)] = string.char( 19),
  412.    [string.char( 80)] = string.char( 12),   [string.char( 81)] = string.char( 13),
  413.    [string.char( 82)] = string.char( 14),   [string.char( 83)] = string.char( 15),
  414.    [string.char( 84)] = string.char(  8),   [string.char( 85)] = string.char(  9),
  415.    [string.char( 86)] = string.char( 10),   [string.char( 87)] = string.char( 11),
  416.    [string.char( 88)] = string.char(  4),   [string.char( 89)] = string.char(  5),
  417.    [string.char( 90)] = string.char(  6),   [string.char( 91)] = string.char(  7),
  418.    [string.char( 92)] = string.char(  0),   [string.char( 93)] = string.char(  1),
  419.    [string.char( 94)] = string.char(  2),   [string.char( 95)] = string.char(  3),
  420.    [string.char( 96)] = string.char( 60),   [string.char( 97)] = string.char( 61),
  421.    [string.char( 98)] = string.char( 62),   [string.char( 99)] = string.char( 63),
  422.    [string.char(100)] = string.char( 56),   [string.char(101)] = string.char( 57),
  423.    [string.char(102)] = string.char( 58),   [string.char(103)] = string.char( 59),
  424.    [string.char(104)] = string.char( 52),   [string.char(105)] = string.char( 53),
  425.    [string.char(106)] = string.char( 54),   [string.char(107)] = string.char( 55),
  426.    [string.char(108)] = string.char( 48),   [string.char(109)] = string.char( 49),
  427.    [string.char(110)] = string.char( 50),   [string.char(111)] = string.char( 51),
  428.    [string.char(112)] = string.char( 44),   [string.char(113)] = string.char( 45),
  429.    [string.char(114)] = string.char( 46),   [string.char(115)] = string.char( 47),
  430.    [string.char(116)] = string.char( 40),   [string.char(117)] = string.char( 41),
  431.    [string.char(118)] = string.char( 42),   [string.char(119)] = string.char( 43),
  432.    [string.char(120)] = string.char( 36),   [string.char(121)] = string.char( 37),
  433.    [string.char(122)] = string.char( 38),   [string.char(123)] = string.char( 39),
  434.    [string.char(124)] = string.char( 32),   [string.char(125)] = string.char( 33),
  435.    [string.char(126)] = string.char( 34),   [string.char(127)] = string.char( 35),
  436.    [string.char(128)] = string.char(220),   [string.char(129)] = string.char(221),
  437.    [string.char(130)] = string.char(222),   [string.char(131)] = string.char(223),
  438.    [string.char(132)] = string.char(216),   [string.char(133)] = string.char(217),
  439.    [string.char(134)] = string.char(218),   [string.char(135)] = string.char(219),
  440.    [string.char(136)] = string.char(212),   [string.char(137)] = string.char(213),
  441.    [string.char(138)] = string.char(214),   [string.char(139)] = string.char(215),
  442.    [string.char(140)] = string.char(208),   [string.char(141)] = string.char(209),
  443.    [string.char(142)] = string.char(210),   [string.char(143)] = string.char(211),
  444.    [string.char(144)] = string.char(204),   [string.char(145)] = string.char(205),
  445.    [string.char(146)] = string.char(206),   [string.char(147)] = string.char(207),
  446.    [string.char(148)] = string.char(200),   [string.char(149)] = string.char(201),
  447.    [string.char(150)] = string.char(202),   [string.char(151)] = string.char(203),
  448.    [string.char(152)] = string.char(196),   [string.char(153)] = string.char(197),
  449.    [string.char(154)] = string.char(198),   [string.char(155)] = string.char(199),
  450.    [string.char(156)] = string.char(192),   [string.char(157)] = string.char(193),
  451.    [string.char(158)] = string.char(194),   [string.char(159)] = string.char(195),
  452.    [string.char(160)] = string.char(252),   [string.char(161)] = string.char(253),
  453.    [string.char(162)] = string.char(254),   [string.char(163)] = string.char(255),
  454.    [string.char(164)] = string.char(248),   [string.char(165)] = string.char(249),
  455.    [string.char(166)] = string.char(250),   [string.char(167)] = string.char(251),
  456.    [string.char(168)] = string.char(244),   [string.char(169)] = string.char(245),
  457.    [string.char(170)] = string.char(246),   [string.char(171)] = string.char(247),
  458.    [string.char(172)] = string.char(240),   [string.char(173)] = string.char(241),
  459.    [string.char(174)] = string.char(242),   [string.char(175)] = string.char(243),
  460.    [string.char(176)] = string.char(236),   [string.char(177)] = string.char(237),
  461.    [string.char(178)] = string.char(238),   [string.char(179)] = string.char(239),
  462.    [string.char(180)] = string.char(232),   [string.char(181)] = string.char(233),
  463.    [string.char(182)] = string.char(234),   [string.char(183)] = string.char(235),
  464.    [string.char(184)] = string.char(228),   [string.char(185)] = string.char(229),
  465.    [string.char(186)] = string.char(230),   [string.char(187)] = string.char(231),
  466.    [string.char(188)] = string.char(224),   [string.char(189)] = string.char(225),
  467.    [string.char(190)] = string.char(226),   [string.char(191)] = string.char(227),
  468.    [string.char(192)] = string.char(156),   [string.char(193)] = string.char(157),
  469.    [string.char(194)] = string.char(158),   [string.char(195)] = string.char(159),
  470.    [string.char(196)] = string.char(152),   [string.char(197)] = string.char(153),
  471.    [string.char(198)] = string.char(154),   [string.char(199)] = string.char(155),
  472.    [string.char(200)] = string.char(148),   [string.char(201)] = string.char(149),
  473.    [string.char(202)] = string.char(150),   [string.char(203)] = string.char(151),
  474.    [string.char(204)] = string.char(144),   [string.char(205)] = string.char(145),
  475.    [string.char(206)] = string.char(146),   [string.char(207)] = string.char(147),
  476.    [string.char(208)] = string.char(140),   [string.char(209)] = string.char(141),
  477.    [string.char(210)] = string.char(142),   [string.char(211)] = string.char(143),
  478.    [string.char(212)] = string.char(136),   [string.char(213)] = string.char(137),
  479.    [string.char(214)] = string.char(138),   [string.char(215)] = string.char(139),
  480.    [string.char(216)] = string.char(132),   [string.char(217)] = string.char(133),
  481.    [string.char(218)] = string.char(134),   [string.char(219)] = string.char(135),
  482.    [string.char(220)] = string.char(128),   [string.char(221)] = string.char(129),
  483.    [string.char(222)] = string.char(130),   [string.char(223)] = string.char(131),
  484.    [string.char(224)] = string.char(188),   [string.char(225)] = string.char(189),
  485.    [string.char(226)] = string.char(190),   [string.char(227)] = string.char(191),
  486.    [string.char(228)] = string.char(184),   [string.char(229)] = string.char(185),
  487.    [string.char(230)] = string.char(186),   [string.char(231)] = string.char(187),
  488.    [string.char(232)] = string.char(180),   [string.char(233)] = string.char(181),
  489.    [string.char(234)] = string.char(182),   [string.char(235)] = string.char(183),
  490.    [string.char(236)] = string.char(176),   [string.char(237)] = string.char(177),
  491.    [string.char(238)] = string.char(178),   [string.char(239)] = string.char(179),
  492.    [string.char(240)] = string.char(172),   [string.char(241)] = string.char(173),
  493.    [string.char(242)] = string.char(174),   [string.char(243)] = string.char(175),
  494.    [string.char(244)] = string.char(168),   [string.char(245)] = string.char(169),
  495.    [string.char(246)] = string.char(170),   [string.char(247)] = string.char(171),
  496.    [string.char(248)] = string.char(164),   [string.char(249)] = string.char(165),
  497.    [string.char(250)] = string.char(166),   [string.char(251)] = string.char(167),
  498.    [string.char(252)] = string.char(160),   [string.char(253)] = string.char(161),
  499.    [string.char(254)] = string.char(162),   [string.char(255)] = string.char(163),
  500. }
  501.  
  502. local xor_with_0x36 = {
  503.    [string.char(  0)] = string.char( 54),   [string.char(  1)] = string.char( 55),
  504.    [string.char(  2)] = string.char( 52),   [string.char(  3)] = string.char( 53),
  505.    [string.char(  4)] = string.char( 50),   [string.char(  5)] = string.char( 51),
  506.    [string.char(  6)] = string.char( 48),   [string.char(  7)] = string.char( 49),
  507.    [string.char(  8)] = string.char( 62),   [string.char(  9)] = string.char( 63),
  508.    [string.char( 10)] = string.char( 60),   [string.char( 11)] = string.char( 61),
  509.    [string.char( 12)] = string.char( 58),   [string.char( 13)] = string.char( 59),
  510.    [string.char( 14)] = string.char( 56),   [string.char( 15)] = string.char( 57),
  511.    [string.char( 16)] = string.char( 38),   [string.char( 17)] = string.char( 39),
  512.    [string.char( 18)] = string.char( 36),   [string.char( 19)] = string.char( 37),
  513.    [string.char( 20)] = string.char( 34),   [string.char( 21)] = string.char( 35),
  514.    [string.char( 22)] = string.char( 32),   [string.char( 23)] = string.char( 33),
  515.    [string.char( 24)] = string.char( 46),   [string.char( 25)] = string.char( 47),
  516.    [string.char( 26)] = string.char( 44),   [string.char( 27)] = string.char( 45),
  517.    [string.char( 28)] = string.char( 42),   [string.char( 29)] = string.char( 43),
  518.    [string.char( 30)] = string.char( 40),   [string.char( 31)] = string.char( 41),
  519.    [string.char( 32)] = string.char( 22),   [string.char( 33)] = string.char( 23),
  520.    [string.char( 34)] = string.char( 20),   [string.char( 35)] = string.char( 21),
  521.    [string.char( 36)] = string.char( 18),   [string.char( 37)] = string.char( 19),
  522.    [string.char( 38)] = string.char( 16),   [string.char( 39)] = string.char( 17),
  523.    [string.char( 40)] = string.char( 30),   [string.char( 41)] = string.char( 31),
  524.    [string.char( 42)] = string.char( 28),   [string.char( 43)] = string.char( 29),
  525.    [string.char( 44)] = string.char( 26),   [string.char( 45)] = string.char( 27),
  526.    [string.char( 46)] = string.char( 24),   [string.char( 47)] = string.char( 25),
  527.    [string.char( 48)] = string.char(  6),   [string.char( 49)] = string.char(  7),
  528.    [string.char( 50)] = string.char(  4),   [string.char( 51)] = string.char(  5),
  529.    [string.char( 52)] = string.char(  2),   [string.char( 53)] = string.char(  3),
  530.    [string.char( 54)] = string.char(  0),   [string.char( 55)] = string.char(  1),
  531.    [string.char( 56)] = string.char( 14),   [string.char( 57)] = string.char( 15),
  532.    [string.char( 58)] = string.char( 12),   [string.char( 59)] = string.char( 13),
  533.    [string.char( 60)] = string.char( 10),   [string.char( 61)] = string.char( 11),
  534.    [string.char( 62)] = string.char(  8),   [string.char( 63)] = string.char(  9),
  535.    [string.char( 64)] = string.char(118),   [string.char( 65)] = string.char(119),
  536.    [string.char( 66)] = string.char(116),   [string.char( 67)] = string.char(117),
  537.    [string.char( 68)] = string.char(114),   [string.char( 69)] = string.char(115),
  538.    [string.char( 70)] = string.char(112),   [string.char( 71)] = string.char(113),
  539.    [string.char( 72)] = string.char(126),   [string.char( 73)] = string.char(127),
  540.    [string.char( 74)] = string.char(124),   [string.char( 75)] = string.char(125),
  541.    [string.char( 76)] = string.char(122),   [string.char( 77)] = string.char(123),
  542.    [string.char( 78)] = string.char(120),   [string.char( 79)] = string.char(121),
  543.    [string.char( 80)] = string.char(102),   [string.char( 81)] = string.char(103),
  544.    [string.char( 82)] = string.char(100),   [string.char( 83)] = string.char(101),
  545.    [string.char( 84)] = string.char( 98),   [string.char( 85)] = string.char( 99),
  546.    [string.char( 86)] = string.char( 96),   [string.char( 87)] = string.char( 97),
  547.    [string.char( 88)] = string.char(110),   [string.char( 89)] = string.char(111),
  548.    [string.char( 90)] = string.char(108),   [string.char( 91)] = string.char(109),
  549.    [string.char( 92)] = string.char(106),   [string.char( 93)] = string.char(107),
  550.    [string.char( 94)] = string.char(104),   [string.char( 95)] = string.char(105),
  551.    [string.char( 96)] = string.char( 86),   [string.char( 97)] = string.char( 87),
  552.    [string.char( 98)] = string.char( 84),   [string.char( 99)] = string.char( 85),
  553.    [string.char(100)] = string.char( 82),   [string.char(101)] = string.char( 83),
  554.    [string.char(102)] = string.char( 80),   [string.char(103)] = string.char( 81),
  555.    [string.char(104)] = string.char( 94),   [string.char(105)] = string.char( 95),
  556.    [string.char(106)] = string.char( 92),   [string.char(107)] = string.char( 93),
  557.    [string.char(108)] = string.char( 90),   [string.char(109)] = string.char( 91),
  558.    [string.char(110)] = string.char( 88),   [string.char(111)] = string.char( 89),
  559.    [string.char(112)] = string.char( 70),   [string.char(113)] = string.char( 71),
  560.    [string.char(114)] = string.char( 68),   [string.char(115)] = string.char( 69),
  561.    [string.char(116)] = string.char( 66),   [string.char(117)] = string.char( 67),
  562.    [string.char(118)] = string.char( 64),   [string.char(119)] = string.char( 65),
  563.    [string.char(120)] = string.char( 78),   [string.char(121)] = string.char( 79),
  564.    [string.char(122)] = string.char( 76),   [string.char(123)] = string.char( 77),
  565.    [string.char(124)] = string.char( 74),   [string.char(125)] = string.char( 75),
  566.    [string.char(126)] = string.char( 72),   [string.char(127)] = string.char( 73),
  567.    [string.char(128)] = string.char(182),   [string.char(129)] = string.char(183),
  568.    [string.char(130)] = string.char(180),   [string.char(131)] = string.char(181),
  569.    [string.char(132)] = string.char(178),   [string.char(133)] = string.char(179),
  570.    [string.char(134)] = string.char(176),   [string.char(135)] = string.char(177),
  571.    [string.char(136)] = string.char(190),   [string.char(137)] = string.char(191),
  572.    [string.char(138)] = string.char(188),   [string.char(139)] = string.char(189),
  573.    [string.char(140)] = string.char(186),   [string.char(141)] = string.char(187),
  574.    [string.char(142)] = string.char(184),   [string.char(143)] = string.char(185),
  575.    [string.char(144)] = string.char(166),   [string.char(145)] = string.char(167),
  576.    [string.char(146)] = string.char(164),   [string.char(147)] = string.char(165),
  577.    [string.char(148)] = string.char(162),   [string.char(149)] = string.char(163),
  578.    [string.char(150)] = string.char(160),   [string.char(151)] = string.char(161),
  579.    [string.char(152)] = string.char(174),   [string.char(153)] = string.char(175),
  580.    [string.char(154)] = string.char(172),   [string.char(155)] = string.char(173),
  581.    [string.char(156)] = string.char(170),   [string.char(157)] = string.char(171),
  582.    [string.char(158)] = string.char(168),   [string.char(159)] = string.char(169),
  583.    [string.char(160)] = string.char(150),   [string.char(161)] = string.char(151),
  584.    [string.char(162)] = string.char(148),   [string.char(163)] = string.char(149),
  585.    [string.char(164)] = string.char(146),   [string.char(165)] = string.char(147),
  586.    [string.char(166)] = string.char(144),   [string.char(167)] = string.char(145),
  587.    [string.char(168)] = string.char(158),   [string.char(169)] = string.char(159),
  588.    [string.char(170)] = string.char(156),   [string.char(171)] = string.char(157),
  589.    [string.char(172)] = string.char(154),   [string.char(173)] = string.char(155),
  590.    [string.char(174)] = string.char(152),   [string.char(175)] = string.char(153),
  591.    [string.char(176)] = string.char(134),   [string.char(177)] = string.char(135),
  592.    [string.char(178)] = string.char(132),   [string.char(179)] = string.char(133),
  593.    [string.char(180)] = string.char(130),   [string.char(181)] = string.char(131),
  594.    [string.char(182)] = string.char(128),   [string.char(183)] = string.char(129),
  595.    [string.char(184)] = string.char(142),   [string.char(185)] = string.char(143),
  596.    [string.char(186)] = string.char(140),   [string.char(187)] = string.char(141),
  597.    [string.char(188)] = string.char(138),   [string.char(189)] = string.char(139),
  598.    [string.char(190)] = string.char(136),   [string.char(191)] = string.char(137),
  599.    [string.char(192)] = string.char(246),   [string.char(193)] = string.char(247),
  600.    [string.char(194)] = string.char(244),   [string.char(195)] = string.char(245),
  601.    [string.char(196)] = string.char(242),   [string.char(197)] = string.char(243),
  602.    [string.char(198)] = string.char(240),   [string.char(199)] = string.char(241),
  603.    [string.char(200)] = string.char(254),   [string.char(201)] = string.char(255),
  604.    [string.char(202)] = string.char(252),   [string.char(203)] = string.char(253),
  605.    [string.char(204)] = string.char(250),   [string.char(205)] = string.char(251),
  606.    [string.char(206)] = string.char(248),   [string.char(207)] = string.char(249),
  607.    [string.char(208)] = string.char(230),   [string.char(209)] = string.char(231),
  608.    [string.char(210)] = string.char(228),   [string.char(211)] = string.char(229),
  609.    [string.char(212)] = string.char(226),   [string.char(213)] = string.char(227),
  610.    [string.char(214)] = string.char(224),   [string.char(215)] = string.char(225),
  611.    [string.char(216)] = string.char(238),   [string.char(217)] = string.char(239),
  612.    [string.char(218)] = string.char(236),   [string.char(219)] = string.char(237),
  613.    [string.char(220)] = string.char(234),   [string.char(221)] = string.char(235),
  614.    [string.char(222)] = string.char(232),   [string.char(223)] = string.char(233),
  615.    [string.char(224)] = string.char(214),   [string.char(225)] = string.char(215),
  616.    [string.char(226)] = string.char(212),   [string.char(227)] = string.char(213),
  617.    [string.char(228)] = string.char(210),   [string.char(229)] = string.char(211),
  618.    [string.char(230)] = string.char(208),   [string.char(231)] = string.char(209),
  619.    [string.char(232)] = string.char(222),   [string.char(233)] = string.char(223),
  620.    [string.char(234)] = string.char(220),   [string.char(235)] = string.char(221),
  621.    [string.char(236)] = string.char(218),   [string.char(237)] = string.char(219),
  622.    [string.char(238)] = string.char(216),   [string.char(239)] = string.char(217),
  623.    [string.char(240)] = string.char(198),   [string.char(241)] = string.char(199),
  624.    [string.char(242)] = string.char(196),   [string.char(243)] = string.char(197),
  625.    [string.char(244)] = string.char(194),   [string.char(245)] = string.char(195),
  626.    [string.char(246)] = string.char(192),   [string.char(247)] = string.char(193),
  627.    [string.char(248)] = string.char(206),   [string.char(249)] = string.char(207),
  628.    [string.char(250)] = string.char(204),   [string.char(251)] = string.char(205),
  629.    [string.char(252)] = string.char(202),   [string.char(253)] = string.char(203),
  630.    [string.char(254)] = string.char(200),   [string.char(255)] = string.char(201),
  631. }
  632.  
  633.  
  634. local blocksize = 64 -- 512 bits
  635.  
  636. function hmac_sha1(key, text)
  637.    assert(type(key)  == 'string', "key passed to hmac_sha1 should be a string")
  638.    assert(type(text) == 'string', "text passed to hmac_sha1 should be a string")
  639.  
  640.    if #key > blocksize then
  641.       key = sha1_binary(key)
  642.    end
  643.  
  644.    local key_xord_with_0x36 = key:gsub('.', xor_with_0x36) .. string.rep(string.char(0x36), blocksize - #key)
  645.    local key_xord_with_0x5c = key:gsub('.', xor_with_0x5c) .. string.rep(string.char(0x5c), blocksize - #key)
  646.  
  647.    return sha1(key_xord_with_0x5c .. sha1_binary(key_xord_with_0x36 .. text))
  648. end
  649.  
  650. function hmac_sha1_binary(key, text)
  651.    return hex_to_binary(hmac_sha1(key, text))
  652. end
Add Comment
Please, Sign In to add comment