|
| 1 | +--[[-- |
| 2 | +MD5 hash library. |
| 3 | + ]] |
| 4 | + |
| 5 | +local ffi = require "ffi" |
| 6 | +local bit = require "bit" |
| 7 | +local bxor = bit.bxor |
| 8 | +local bnot = bit.bnot |
| 9 | +local band = bit.band |
| 10 | +local bor = bit.bor |
| 11 | +local rshift = bit.rshift |
| 12 | +local lshift = bit.lshift |
| 13 | +local copy = ffi.copy |
| 14 | +local fill = ffi.fill |
| 15 | + |
| 16 | +ffi.cdef[[ |
| 17 | +typedef struct MD5Context { |
| 18 | + uint32_t buf[4]; |
| 19 | + uint32_t bits[2]; |
| 20 | + unsigned char input[64]; |
| 21 | +} MD5_CTX; |
| 22 | +]] |
| 23 | + |
| 24 | +local function byteReverse(buf, len) |
| 25 | + -- TODO: implement for big-endian architectures? |
| 26 | +end |
| 27 | + |
| 28 | +local function F1(x, y, z) return bxor(z, band(x, bxor(y, z))) end |
| 29 | +local function F2(x, y, z) return F1(z, x, y) end |
| 30 | +local function F3(x, y, z) return bxor(x, y, z) end |
| 31 | +local function F4(x, y, z) return bxor(y, bor(x, bnot(z))) end |
| 32 | + |
| 33 | +local function MD5STEP(f, w, x, y, z, data, s) |
| 34 | + w = w + f(x, y, z) + data |
| 35 | + w = bor(lshift(w,s), rshift(w,(32-s))) |
| 36 | + w = w + x |
| 37 | + |
| 38 | + return w |
| 39 | +end |
| 40 | + |
| 41 | +-- Start MD5 accumulation. Set bit count to 0 and buffer to mysterious |
| 42 | +-- initialization constants. |
| 43 | +local function MD5Init(ctx) |
| 44 | + ctx.buf[0] = 0x67452301 |
| 45 | + ctx.buf[1] = 0xefcdab89 |
| 46 | + ctx.buf[2] = 0x98badcfe |
| 47 | + ctx.buf[3] = 0x10325476 |
| 48 | + |
| 49 | + ctx.bits[0] = 0 |
| 50 | + ctx.bits[1] = 0 |
| 51 | +end |
| 52 | + |
| 53 | +local function MD5Transform(buf, input) |
| 54 | + local a = buf[0] |
| 55 | + local b = buf[1] |
| 56 | + local c = buf[2] |
| 57 | + local d = buf[3] |
| 58 | + |
| 59 | + a = MD5STEP(F1, a, b, c, d, input[0] + 0xd76aa478, 7) |
| 60 | + d = MD5STEP(F1, d, a, b, c, input[1] + 0xe8c7b756, 12) |
| 61 | + c = MD5STEP(F1, c, d, a, b, input[2] + 0x242070db, 17) |
| 62 | + b = MD5STEP(F1, b, c, d, a, input[3] + 0xc1bdceee, 22) |
| 63 | + a = MD5STEP(F1, a, b, c, d, input[4] + 0xf57c0faf, 7) |
| 64 | + d = MD5STEP(F1, d, a, b, c, input[5] + 0x4787c62a, 12) |
| 65 | + c = MD5STEP(F1, c, d, a, b, input[6] + 0xa8304613, 17) |
| 66 | + b = MD5STEP(F1, b, c, d, a, input[7] + 0xfd469501, 22) |
| 67 | + a = MD5STEP(F1, a, b, c, d, input[8] + 0x698098d8, 7) |
| 68 | + d = MD5STEP(F1, d, a, b, c, input[9] + 0x8b44f7af, 12) |
| 69 | + c = MD5STEP(F1, c, d, a, b, input[10] + 0xffff5bb1, 17) |
| 70 | + b = MD5STEP(F1, b, c, d, a, input[11] + 0x895cd7be, 22) |
| 71 | + a = MD5STEP(F1, a, b, c, d, input[12] + 0x6b901122, 7) |
| 72 | + d = MD5STEP(F1, d, a, b, c, input[13] + 0xfd987193, 12) |
| 73 | + c = MD5STEP(F1, c, d, a, b, input[14] + 0xa679438e, 17) |
| 74 | + b = MD5STEP(F1, b, c, d, a, input[15] + 0x49b40821, 22) |
| 75 | + |
| 76 | + a = MD5STEP(F2, a, b, c, d, input[1] + 0xf61e2562, 5) |
| 77 | + d = MD5STEP(F2, d, a, b, c, input[6] + 0xc040b340, 9) |
| 78 | + c = MD5STEP(F2, c, d, a, b, input[11] + 0x265e5a51, 14) |
| 79 | + b = MD5STEP(F2, b, c, d, a, input[0] + 0xe9b6c7aa, 20) |
| 80 | + a = MD5STEP(F2, a, b, c, d, input[5] + 0xd62f105d, 5) |
| 81 | + d = MD5STEP(F2, d, a, b, c, input[10] + 0x02441453, 9) |
| 82 | + c = MD5STEP(F2, c, d, a, b, input[15] + 0xd8a1e681, 14) |
| 83 | + b = MD5STEP(F2, b, c, d, a, input[4] + 0xe7d3fbc8, 20) |
| 84 | + a = MD5STEP(F2, a, b, c, d, input[9] + 0x21e1cde6, 5) |
| 85 | + d = MD5STEP(F2, d, a, b, c, input[14] + 0xc33707d6, 9) |
| 86 | + c = MD5STEP(F2, c, d, a, b, input[3] + 0xf4d50d87, 14) |
| 87 | + b = MD5STEP(F2, b, c, d, a, input[8] + 0x455a14ed, 20) |
| 88 | + a = MD5STEP(F2, a, b, c, d, input[13] + 0xa9e3e905, 5) |
| 89 | + d = MD5STEP(F2, d, a, b, c, input[2] + 0xfcefa3f8, 9) |
| 90 | + c = MD5STEP(F2, c, d, a, b, input[7] + 0x676f02d9, 14) |
| 91 | + b = MD5STEP(F2, b, c, d, a, input[12] + 0x8d2a4c8a, 20) |
| 92 | + |
| 93 | + a = MD5STEP(F3, a, b, c, d, input[5] + 0xfffa3942, 4) |
| 94 | + d = MD5STEP(F3, d, a, b, c, input[8] + 0x8771f681, 11) |
| 95 | + c = MD5STEP(F3, c, d, a, b, input[11] + 0x6d9d6122, 16) |
| 96 | + b = MD5STEP(F3, b, c, d, a, input[14] + 0xfde5380c, 23) |
| 97 | + a = MD5STEP(F3, a, b, c, d, input[1] + 0xa4beea44, 4) |
| 98 | + d = MD5STEP(F3, d, a, b, c, input[4] + 0x4bdecfa9, 11) |
| 99 | + c = MD5STEP(F3, c, d, a, b, input[7] + 0xf6bb4b60, 16) |
| 100 | + b = MD5STEP(F3, b, c, d, a, input[10] + 0xbebfbc70, 23) |
| 101 | + a = MD5STEP(F3, a, b, c, d, input[13] + 0x289b7ec6, 4) |
| 102 | + d = MD5STEP(F3, d, a, b, c, input[0] + 0xeaa127fa, 11) |
| 103 | + c = MD5STEP(F3, c, d, a, b, input[3] + 0xd4ef3085, 16) |
| 104 | + b = MD5STEP(F3, b, c, d, a, input[6] + 0x04881d05, 23) |
| 105 | + a = MD5STEP(F3, a, b, c, d, input[9] + 0xd9d4d039, 4) |
| 106 | + d = MD5STEP(F3, d, a, b, c, input[12] + 0xe6db99e5, 11) |
| 107 | + c = MD5STEP(F3, c, d, a, b, input[15] + 0x1fa27cf8, 16) |
| 108 | + b = MD5STEP(F3, b, c, d, a, input[2] + 0xc4ac5665, 23) |
| 109 | + |
| 110 | + a = MD5STEP(F4, a, b, c, d, input[0] + 0xf4292244, 6) |
| 111 | + d = MD5STEP(F4, d, a, b, c, input[7] + 0x432aff97, 10) |
| 112 | + c = MD5STEP(F4, c, d, a, b, input[14] + 0xab9423a7, 15) |
| 113 | + b = MD5STEP(F4, b, c, d, a, input[5] + 0xfc93a039, 21) |
| 114 | + a = MD5STEP(F4, a, b, c, d, input[12] + 0x655b59c3, 6) |
| 115 | + d = MD5STEP(F4, d, a, b, c, input[3] + 0x8f0ccc92, 10) |
| 116 | + c = MD5STEP(F4, c, d, a, b, input[10] + 0xffeff47d, 15) |
| 117 | + b = MD5STEP(F4, b, c, d, a, input[1] + 0x85845dd1, 21) |
| 118 | + a = MD5STEP(F4, a, b, c, d, input[8] + 0x6fa87e4f, 6) |
| 119 | + d = MD5STEP(F4, d, a, b, c, input[15] + 0xfe2ce6e0, 10) |
| 120 | + c = MD5STEP(F4, c, d, a, b, input[6] + 0xa3014314, 15) |
| 121 | + b = MD5STEP(F4, b, c, d, a, input[13] + 0x4e0811a1, 21) |
| 122 | + a = MD5STEP(F4, a, b, c, d, input[4] + 0xf7537e82, 6) |
| 123 | + d = MD5STEP(F4, d, a, b, c, input[11] + 0xbd3af235, 10) |
| 124 | + c = MD5STEP(F4, c, d, a, b, input[2] + 0x2ad7d2bb, 15) |
| 125 | + b = MD5STEP(F4, b, c, d, a, input[9] + 0xeb86d391, 21) |
| 126 | + |
| 127 | + buf[0] = band(buf[0] + a, 0xFFFFFFFF) |
| 128 | + buf[1] = band(buf[1] + b, 0xFFFFFFFF) |
| 129 | + buf[2] = band(buf[2] + c, 0xFFFFFFFF) |
| 130 | + buf[3] = band(buf[3] + d, 0xFFFFFFFF) |
| 131 | +end |
| 132 | + |
| 133 | +local function MD5Update(ctx, buf, len) |
| 134 | + local t |
| 135 | + |
| 136 | + t = ctx.bits[0] |
| 137 | + ctx.bits[0] = t + lshift( len, 3) |
| 138 | + if (ctx.bits[0] < t) then |
| 139 | + ctx.bits[1] = ctx.bits[1] + 1 |
| 140 | + end |
| 141 | + |
| 142 | + ctx.bits[1] = ctx.bits[1] + rshift(len, 29) |
| 143 | + |
| 144 | + t = band(rshift(t, 3), 0x3f) |
| 145 | + |
| 146 | + if (t > 0) then |
| 147 | + local p = ffi.cast("unsigned char *", ctx.input + t) |
| 148 | + |
| 149 | + t = 64 - t |
| 150 | + if (len < t) then |
| 151 | + copy(p, buf, len) |
| 152 | + return |
| 153 | + end |
| 154 | + |
| 155 | + copy(p, buf, t) |
| 156 | + byteReverse(ctx.input, 16) |
| 157 | + MD5Transform(ctx.buf, ffi.cast("uint32_t *", ctx.input)) |
| 158 | + buf = buf + t |
| 159 | + len = len - t |
| 160 | + end |
| 161 | + |
| 162 | + while (len >= 64) do |
| 163 | + copy(ctx.input, buf, 64) |
| 164 | + byteReverse(ctx.input, 16) |
| 165 | + MD5Transform(ctx.buf, ffi.cast("uint32_t *", ctx.input)) |
| 166 | + buf = buf + 64 |
| 167 | + len = len - 64 |
| 168 | + end |
| 169 | + |
| 170 | + copy(ctx.input, buf, len) |
| 171 | +end |
| 172 | + |
| 173 | +local function MD5Final(digest, ctx) |
| 174 | + |
| 175 | + local count |
| 176 | + local p |
| 177 | + |
| 178 | + count = band(rshift(ctx.bits[0], 3), 0x3F) |
| 179 | + |
| 180 | + p = ctx.input + count |
| 181 | + p[0] = 0x80 |
| 182 | + p = p + 1 |
| 183 | + count = 64 - 1 - count |
| 184 | + |
| 185 | + if (count < 8) then |
| 186 | + fill(p, count, 0) |
| 187 | + byteReverse(ctx.input, 16) |
| 188 | + MD5Transform(ctx.buf, ffi.cast("uint32_t *", ctx.input)) |
| 189 | + fill(ctx.input, 56, 0) |
| 190 | + else |
| 191 | + fill(p, count - 8, 0) |
| 192 | + end |
| 193 | + |
| 194 | + byteReverse(ctx.input, 14) |
| 195 | + |
| 196 | + ffi.cast("uint32_t *", ctx.input)[14] = ctx.bits[0] |
| 197 | + ffi.cast("uint32_t *", ctx.input)[15] = ctx.bits[1] |
| 198 | + |
| 199 | + MD5Transform(ctx.buf, ffi.cast("uint32_t *", ctx.input)) |
| 200 | + byteReverse(ffi.cast("unsigned char *",ctx.buf), 4) |
| 201 | + copy(digest, ctx.buf, 16) |
| 202 | + fill(ffi.cast("char *", ctx), ffi.sizeof(ctx), 0) |
| 203 | +end |
| 204 | + |
| 205 | +local hex = ffi.new("const char[16]", "0123456789abcdef") |
| 206 | +local function bin2str(output, input, len) |
| 207 | + if len > 0 then |
| 208 | + output[0] = hex[rshift(input[0], 4)] |
| 209 | + output[1] = hex[band(input[0], 0xF)] |
| 210 | + return bin2str(output+2, input+1, len-1) |
| 211 | + end |
| 212 | +end |
| 213 | + |
| 214 | + |
| 215 | +local md5_proto = {} |
| 216 | +md5_proto.__index = md5_proto |
| 217 | + |
| 218 | +--- Feed content to md5 hashing instance. |
| 219 | +---- @string luastr stream to process |
| 220 | +function md5_proto:update(luastr) |
| 221 | + MD5Update(self.ctx, ffi.cast("const char*", luastr), #luastr) |
| 222 | +end |
| 223 | + |
| 224 | +--- Calcualte md5 sum with the state of a md5 hashing instance. |
| 225 | +---- @string luastr stream to process |
| 226 | +---- @treturn string md5 sum in Lua string |
| 227 | +function md5_proto:sum(luastr) |
| 228 | + local buf = ffi.new("char[33]") |
| 229 | + local hash = ffi.new("uint8_t[16]") |
| 230 | + if luastr then |
| 231 | + self:update(luastr) |
| 232 | + end |
| 233 | + MD5Final(hash, self.ctx) |
| 234 | + |
| 235 | + bin2str(buf, hash, ffi.sizeof(hash)) |
| 236 | + return ffi.string(buf) |
| 237 | +end |
| 238 | + |
| 239 | + |
| 240 | +local md5 = {} |
| 241 | + |
| 242 | +--- Create a new md5 hashing instance. |
| 243 | +---- @return md5 instance |
| 244 | +function md5.new() |
| 245 | + local o = {} |
| 246 | + o.ctx = ffi.new("MD5_CTX") |
| 247 | + MD5Init(o.ctx) |
| 248 | + setmetatable(o, md5_proto) |
| 249 | + return o |
| 250 | +end |
| 251 | + |
| 252 | +--- Calcualte md5 sum. |
| 253 | +---- @string luastr stream to process |
| 254 | +---- @treturn string md5 sum in Lua string |
| 255 | +function md5.sum(luastr) |
| 256 | + return md5:new():sum(luastr) |
| 257 | +end |
| 258 | + |
| 259 | +--- Calcualte md5 sum for a file. |
| 260 | +---- @string filename |
| 261 | +---- @treturn string md5 sum in Lua string |
| 262 | +function md5.sumFile(filename) |
| 263 | + local m = md5.new() |
| 264 | + local fd, err = io.open(filename, "rb") |
| 265 | + if err ~= nil then |
| 266 | + return nil, err |
| 267 | + end |
| 268 | + while true do |
| 269 | + local bytes = fd:read(8192) |
| 270 | + if not bytes then |
| 271 | + fd:close() |
| 272 | + break |
| 273 | + end |
| 274 | + m:update(bytes) |
| 275 | + end |
| 276 | + return m:sum() |
| 277 | +end |
| 278 | + |
| 279 | +return md5 |
0 commit comments