Software /
code /
verse
Annotate
util/sha1.lua @ 309:6297d658769f
plugins.adhoc: Collect a <note/>
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 04 Jul 2012 03:37:16 +0200 |
parent | 111:75c73ac9d5a6 |
child | 486:6416ea3fff86 |
rev | line source |
---|---|
57
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 ------------------------------------------------- |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 --- *** SHA-1 algorithm for Lua *** --- |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 ------------------------------------------------- |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 --- Author: Martin Huesser --- |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 --- Date: 2008-06-16 --- |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 --- License: You may use this code in your --- |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 --- projects as long as this header --- |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 --- stays intact. --- |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 ------------------------------------------------- |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 local strlen = string.len |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 local strchar = string.char |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 local strbyte = string.byte |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 local strsub = string.sub |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 local floor = math.floor |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 local bit = require "bit" |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 local bnot = bit.bnot |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 local band = bit.band |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 local bor = bit.bor |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 local bxor = bit.bxor |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 local shl = bit.lshift |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 local shr = bit.rshift |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 local h0, h1, h2, h3, h4 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 ------------------------------------------------- |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 local function LeftRotate(val, nr) |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 return shl(val, nr) + shr(val, 32 - nr) |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 end |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 ------------------------------------------------- |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 local function ToHex(num) |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 local i, d |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 local str = "" |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 for i = 1, 8 do |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 d = band(num, 15) |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 if (d < 10) then |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 str = strchar(d + 48) .. str |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 else |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 str = strchar(d + 87) .. str |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 end |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 num = floor(num / 16) |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 end |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 return str |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 end |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 ------------------------------------------------- |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 local function PreProcess(str) |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 local bitlen, i |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 local str2 = "" |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 bitlen = strlen(str) * 8 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 str = str .. strchar(128) |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 i = 56 - band(strlen(str), 63) |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 if (i < 0) then |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 i = i + 64 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 end |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 for i = 1, i do |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 str = str .. strchar(0) |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 end |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 for i = 1, 8 do |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 str2 = strchar(band(bitlen, 255)) .. str2 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 bitlen = floor(bitlen / 256) |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 end |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 return str .. str2 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 end |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 ------------------------------------------------- |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 local function MainLoop(str) |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 local a, b, c, d, e, f, k, t |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 local i, j |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 local w = {} |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 while (str ~= "") do |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 for i = 0, 15 do |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 w[i] = 0 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 for j = 1, 4 do |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 w[i] = w[i] * 256 + strbyte(str, i * 4 + j) |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 end |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 end |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 for i = 16, 79 do |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 w[i] = LeftRotate(bxor(bxor(w[i - 3], w[i - 8]), bxor(w[i - 14], w[i - 16])), 1) |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 end |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 a = h0 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 b = h1 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 c = h2 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 d = h3 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 e = h4 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 for i = 0, 79 do |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 if (i < 20) then |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 f = bor(band(b, c), band(bnot(b), d)) |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 k = 1518500249 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 elseif (i < 40) then |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 f = bxor(bxor(b, c), d) |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 k = 1859775393 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 elseif (i < 60) then |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 f = bor(bor(band(b, c), band(b, d)), band(c, d)) |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 k = 2400959708 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 else |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 f = bxor(bxor(b, c), d) |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 k = 3395469782 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 end |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 t = LeftRotate(a, 5) + f + e + k + w[i] |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 e = d |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 d = c |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 c = LeftRotate(b, 30) |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 b = a |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 a = t |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 end |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 h0 = band(h0 + a, 4294967295) |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 h1 = band(h1 + b, 4294967295) |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 h2 = band(h2 + c, 4294967295) |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 h3 = band(h3 + d, 4294967295) |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 h4 = band(h4 + e, 4294967295) |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 str = strsub(str, 65) |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 end |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 end |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 ------------------------------------------------- |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 |
105
0f2446a9f65f
util.sha1: Obey second parameter which decides whether to return the result in ASCII hex encoding
Matthew Wild <mwild1@gmail.com>
parents:
57
diff
changeset
|
122 local function sha1(str, hexres) |
57
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 str = PreProcess(str) |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 h0 = 1732584193 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 h1 = 4023233417 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 h2 = 2562383102 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 h3 = 0271733878 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 h4 = 3285377520 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 MainLoop(str) |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 local hex = ToHex(h0)..ToHex(h1)..ToHex(h2) |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 ..ToHex(h3)..ToHex(h4); |
105
0f2446a9f65f
util.sha1: Obey second parameter which decides whether to return the result in ASCII hex encoding
Matthew Wild <mwild1@gmail.com>
parents:
57
diff
changeset
|
132 if hexres then |
57
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 return hex; |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 else |
111
75c73ac9d5a6
util.sha1: Truncate to a single return result when returning the binary form of the hash
Matthew Wild <mwild1@gmail.com>
parents:
105
diff
changeset
|
135 return (hex:gsub("..", function (byte) |
57
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 return string.char(tonumber(byte, 16)); |
111
75c73ac9d5a6
util.sha1: Truncate to a single return result when returning the binary form of the hash
Matthew Wild <mwild1@gmail.com>
parents:
105
diff
changeset
|
137 end)); |
57
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 end |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 end |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 _G.sha1 = {sha1 = sha1}; |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 return _G.sha1; |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 ------------------------------------------------- |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 ------------------------------------------------- |
8e8bac82e119
util.sha1, squishy: Add sha1 lib needed for proxy65 (and soon XEP-0114)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 ------------------------------------------------- |