|
| 1 | +-- SHA-256 code in Lua 5.2; based on the pseudo-code from |
| 2 | +-- Wikipedia (http://en.wikipedia.org/wiki/SHA-2) |
| 3 | +-- The original code here was written by Roberto Ierusalimschy, and is licensed under MIT (see http://lua-users.org/lists/lua-l/2014-08/msg00628.html) |
| 4 | +-- This version has been slightly modified for mpv_thumbnail_script.lua |
| 5 | + |
| 6 | +local _sha = function() |
| 7 | + local band, rrotate, bxor, rshift, bnot = |
| 8 | + bit32.band, bit32.rrotate, bit32.bxor, bit32.rshift, bit32.bnot |
| 9 | + |
| 10 | + -- Initialize table of round constants |
| 11 | + -- (first 32 bits of the fractional parts of the cube roots of the first |
| 12 | + -- 64 primes 2..311): |
| 13 | + local k = { |
| 14 | + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, |
| 15 | + 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, |
| 16 | + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, |
| 17 | + 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, |
| 18 | + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, |
| 19 | + 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, |
| 20 | + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, |
| 21 | + 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, |
| 22 | + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, |
| 23 | + 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, |
| 24 | + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, |
| 25 | + 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, |
| 26 | + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, |
| 27 | + 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, |
| 28 | + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, |
| 29 | + 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2, |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | + -- transform a string of bytes in a string of hexadecimal digits |
| 34 | + local function str2hexa (s) |
| 35 | + local h = string.gsub(s, ".", function(c) |
| 36 | + return string.format("%02x", string.byte(c)) |
| 37 | + end) |
| 38 | + return h |
| 39 | + end |
| 40 | + |
| 41 | + |
| 42 | + -- transform number 'l' in a big-endian sequence of 'n' bytes |
| 43 | + -- (coded as a string) |
| 44 | + local function num2s (l, n) |
| 45 | + local s = "" |
| 46 | + for i = 1, n do |
| 47 | + local rem = l % 256 |
| 48 | + s = string.char(rem) .. s |
| 49 | + l = (l - rem) / 256 |
| 50 | + end |
| 51 | + return s |
| 52 | + end |
| 53 | + |
| 54 | + -- transform the big-endian sequence of four bytes starting at |
| 55 | + -- index 'i' in 's' into a number |
| 56 | + local function s232num (s, i) |
| 57 | + local n = 0 |
| 58 | + for i = i, i + 3 do |
| 59 | + n = n*256 + string.byte(s, i) |
| 60 | + end |
| 61 | + return n |
| 62 | + end |
| 63 | + |
| 64 | + |
| 65 | + -- append the bit '1' to the message |
| 66 | + -- append k bits '0', where k is the minimum number >= 0 such that the |
| 67 | + -- resulting message length (in bits) is congruent to 448 (mod 512) |
| 68 | + -- append length of message (before pre-processing), in bits, as 64-bit |
| 69 | + -- big-endian integer |
| 70 | + local function preproc (msg, len) |
| 71 | + local extra = -(len + 1 + 8) % 64 |
| 72 | + len = num2s(8 * len, 8) -- original len in bits, coded |
| 73 | + msg = msg .. "\128" .. string.rep("\0", extra) .. len |
| 74 | + assert(#msg % 64 == 0) |
| 75 | + return msg |
| 76 | + end |
| 77 | + |
| 78 | + |
| 79 | + local function initH224 (H) |
| 80 | + -- (second 32 bits of the fractional parts of the square roots of the |
| 81 | + -- 9th through 16th primes 23..53) |
| 82 | + H[1] = 0xc1059ed8 |
| 83 | + H[2] = 0x367cd507 |
| 84 | + H[3] = 0x3070dd17 |
| 85 | + H[4] = 0xf70e5939 |
| 86 | + H[5] = 0xffc00b31 |
| 87 | + H[6] = 0x68581511 |
| 88 | + H[7] = 0x64f98fa7 |
| 89 | + H[8] = 0xbefa4fa4 |
| 90 | + return H |
| 91 | + end |
| 92 | + |
| 93 | + |
| 94 | + local function initH256 (H) |
| 95 | + -- (first 32 bits of the fractional parts of the square roots of the |
| 96 | + -- first 8 primes 2..19): |
| 97 | + H[1] = 0x6a09e667 |
| 98 | + H[2] = 0xbb67ae85 |
| 99 | + H[3] = 0x3c6ef372 |
| 100 | + H[4] = 0xa54ff53a |
| 101 | + H[5] = 0x510e527f |
| 102 | + H[6] = 0x9b05688c |
| 103 | + H[7] = 0x1f83d9ab |
| 104 | + H[8] = 0x5be0cd19 |
| 105 | + return H |
| 106 | + end |
| 107 | + |
| 108 | + |
| 109 | + local function digestblock (msg, i, H) |
| 110 | + |
| 111 | + -- break chunk into sixteen 32-bit big-endian words w[1..16] |
| 112 | + local w = {} |
| 113 | + for j = 1, 16 do |
| 114 | + w[j] = s232num(msg, i + (j - 1)*4) |
| 115 | + end |
| 116 | + |
| 117 | + -- Extend the sixteen 32-bit words into sixty-four 32-bit words: |
| 118 | + for j = 17, 64 do |
| 119 | + local v = w[j - 15] |
| 120 | + local s0 = bxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3)) |
| 121 | + v = w[j - 2] |
| 122 | + local s1 = bxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10)) |
| 123 | + w[j] = w[j - 16] + s0 + w[j - 7] + s1 |
| 124 | + end |
| 125 | + |
| 126 | + -- Initialize hash value for this chunk: |
| 127 | + local a, b, c, d, e, f, g, h = |
| 128 | + H[1], H[2], H[3], H[4], H[5], H[6], H[7], H[8] |
| 129 | + |
| 130 | + -- Main loop: |
| 131 | + for i = 1, 64 do |
| 132 | + local s0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22)) |
| 133 | + local maj = bxor(band(a, b), band(a, c), band(b, c)) |
| 134 | + local t2 = s0 + maj |
| 135 | + local s1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25)) |
| 136 | + local ch = bxor (band(e, f), band(bnot(e), g)) |
| 137 | + local t1 = h + s1 + ch + k[i] + w[i] |
| 138 | + |
| 139 | + h = g |
| 140 | + g = f |
| 141 | + f = e |
| 142 | + e = d + t1 |
| 143 | + d = c |
| 144 | + c = b |
| 145 | + b = a |
| 146 | + a = t1 + t2 |
| 147 | + end |
| 148 | + |
| 149 | + -- Add (mod 2^32) this chunk's hash to result so far: |
| 150 | + H[1] = band(H[1] + a) |
| 151 | + H[2] = band(H[2] + b) |
| 152 | + H[3] = band(H[3] + c) |
| 153 | + H[4] = band(H[4] + d) |
| 154 | + H[5] = band(H[5] + e) |
| 155 | + H[6] = band(H[6] + f) |
| 156 | + H[7] = band(H[7] + g) |
| 157 | + H[8] = band(H[8] + h) |
| 158 | + |
| 159 | + end |
| 160 | + |
| 161 | + |
| 162 | + local function finalresult224 (H) |
| 163 | + -- Produce the final hash value (big-endian): |
| 164 | + return |
| 165 | + str2hexa(num2s(H[1], 4)..num2s(H[2], 4)..num2s(H[3], 4)..num2s(H[4], 4).. |
| 166 | + num2s(H[5], 4)..num2s(H[6], 4)..num2s(H[7], 4)) |
| 167 | + end |
| 168 | + |
| 169 | + |
| 170 | + local function finalresult256 (H) |
| 171 | + -- Produce the final hash value (big-endian): |
| 172 | + return |
| 173 | + str2hexa(num2s(H[1], 4)..num2s(H[2], 4)..num2s(H[3], 4)..num2s(H[4], 4).. |
| 174 | + num2s(H[5], 4)..num2s(H[6], 4)..num2s(H[7], 4)..num2s(H[8], 4)) |
| 175 | + end |
| 176 | + |
| 177 | + |
| 178 | + ---------------------------------------------------------------------- |
| 179 | + local HH = {} -- to reuse |
| 180 | + |
| 181 | + local function hash224 (msg) |
| 182 | + msg = preproc(msg, #msg) |
| 183 | + local H = initH224(HH) |
| 184 | + |
| 185 | + -- Process the message in successive 512-bit (64 bytes) chunks: |
| 186 | + for i = 1, #msg, 64 do |
| 187 | + digestblock(msg, i, H) |
| 188 | + end |
| 189 | + |
| 190 | + return finalresult224(H) |
| 191 | + end |
| 192 | + |
| 193 | + |
| 194 | + local function hash256 (msg) |
| 195 | + msg = preproc(msg, #msg) |
| 196 | + local H = initH256(HH) |
| 197 | + |
| 198 | + -- Process the message in successive 512-bit (64 bytes) chunks: |
| 199 | + for i = 1, #msg, 64 do |
| 200 | + digestblock(msg, i, H) |
| 201 | + end |
| 202 | + |
| 203 | + return finalresult256(H) |
| 204 | + end |
| 205 | + ---------------------------------------------------------------------- |
| 206 | + local mt = {} |
| 207 | + |
| 208 | + local function new256 () |
| 209 | + local o = {H = initH256({}), msg = "", len = 0} |
| 210 | + setmetatable(o, mt) |
| 211 | + return o |
| 212 | + end |
| 213 | + |
| 214 | + mt.__index = mt |
| 215 | + |
| 216 | + function mt:add (m) |
| 217 | + self.msg = self.msg .. m |
| 218 | + self.len = self.len + #m |
| 219 | + local t = 0 |
| 220 | + while #self.msg - t >= 64 do |
| 221 | + digestblock(self.msg, t + 1, self.H) |
| 222 | + t = t + 64 |
| 223 | + end |
| 224 | + self.msg = self.msg:sub(t + 1, -1) |
| 225 | + end |
| 226 | + |
| 227 | + |
| 228 | + function mt:close () |
| 229 | + self.msg = preproc(self.msg, self.len) |
| 230 | + self:add("") |
| 231 | + return finalresult256(self.H) |
| 232 | + end |
| 233 | + ---------------------------------------------------------------------- |
| 234 | + |
| 235 | + return { |
| 236 | + hash224 = hash224, |
| 237 | + hash256 = hash256, |
| 238 | + new256 = new256, |
| 239 | + } |
| 240 | +end |
| 241 | +local sha256 = _sha() |
0 commit comments