From dab1b130bd9922dfab82b227e2329f31deabda1f Mon Sep 17 00:00:00 2001 From: kikito Date: Fri, 30 Aug 2013 19:12:29 +0200 Subject: [PATCH 01/60] initial commit with working implementation, readme and license --- MIT-LICENSE.txt | 20 +++ README.md | 40 ++++++ md5.lua | 356 ++++++++++++++++++++++++++++++++++++++++++++++ spec/md5_spec.lua | 28 ++++ 4 files changed, 444 insertions(+) create mode 100644 MIT-LICENSE.txt create mode 100644 README.md create mode 100644 md5.lua create mode 100644 spec/md5_spec.lua diff --git a/MIT-LICENSE.txt b/MIT-LICENSE.txt new file mode 100644 index 0000000..1630e47 --- /dev/null +++ b/MIT-LICENSE.txt @@ -0,0 +1,20 @@ +Copyright (c) 2013 Enrique García Cota + Adam Baldwin + hanzao + Equi 4 Software + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..6591c11 --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +md5.lua +======== + +This pure-Lua module computes md5 in Lua 5.1. + +It implements md5.sum and md5.sumhex like the [kernel project md5 package](http://www.keplerproject.org/md5/), but it's done completely in Lua, with no dependencies on other libs or C files. + +Usage +===== + + local md5 = require 'md5' + + local md5_as_hex = md5.sumhex(message) -- returns a hex string + local md5_as_data = md5.sum(message) -- returns raw bytes + +Credits +======= + +This is a cleanup of an implementation by Adam Baldwin - https://gist.github.com/evilpacket/3647908 + +Which in turn was a mix of the bitwise lib, http://luaforge.net/projects/bit/ by hanzhao (`abrash_han - at - hotmail.com`), +and http://equi4.com/md5/md5calc.lua, by Equi 4 Software. + + +License +======= + +This library, as well as all the previous ones in which is based, is released under the MIT license (See license file for details). + +Specs +===== + +The specs for this library are implemented with [busted](http://ovinelabs.com/busted/). In order to run them, install busted and then: + + cd path/to/where/the/spec/folder/is + busted + + + + diff --git a/md5.lua b/md5.lua new file mode 100644 index 0000000..6007eef --- /dev/null +++ b/md5.lua @@ -0,0 +1,356 @@ +------------------------------------------------------------------------------- +-- MD5 computation in Lua (5.1) +------------------------------------------------------------------------------- +-- bit lib implementions + +local floor, abs, max = math.floor, math.abs, math.max +local char, byte, format, rep, sub = + string.char, string.byte, string.format, string.rep, string.sub + +local function check_int(n) + -- checking not float + if(n - floor(n) > 0) then + error("trying to use bitwise operation on non-integer!") + end +end + +local function tbl_to_number(tbl) + local n = #tbl + + local rslt = 0 + local power = 1 + for i = 1, n do + rslt = rslt + tbl[i]*power + power = power*2 + end + + return rslt +end + +local function expand(tbl_m, tbl_n) + local big = {} + local small = {} + if(#tbl_m > #tbl_n) then + big = tbl_m + small = tbl_n + else + big = tbl_n + small = tbl_m + end + -- expand small + for i = #small + 1, #big do + small[i] = 0 + end + +end + +local to_bits -- needs to be declared before bit_not + +local function bit_not(n) + local tbl = to_bits(n) + local size = max(#tbl, 32) + for i = 1, size do + if(tbl[i] == 1) then + tbl[i] = 0 + else + tbl[i] = 1 + end + end + return tbl_to_number(tbl) +end + +-- defined as local above +to_bits = function (n) + check_int(n) + if(n < 0) then + -- negative + return to_bits(bit_not(abs(n)) + 1) + end + -- to bits table + local tbl = {} + local cnt = 1 + while (n > 0) do + local last = math.mod(n,2) + if(last == 1) then + tbl[cnt] = 1 + else + tbl[cnt] = 0 + end + n = (n-last)/2 + cnt = cnt + 1 + end + + return tbl +end + +local function bit_or(m, n) + local tbl_m = to_bits(m) + local tbl_n = to_bits(n) + expand(tbl_m, tbl_n) + + local tbl = {} + local rslt = max(#tbl_m, #tbl_n) + for i = 1, rslt do + if(tbl_m[i]== 0 and tbl_n[i] == 0) then + tbl[i] = 0 + else + tbl[i] = 1 + end + end + + return tbl_to_number(tbl) +end + +local function bit_and(m, n) + local tbl_m = to_bits(m) + local tbl_n = to_bits(n) + expand(tbl_m, tbl_n) + + local tbl = {} + local rslt = max(#tbl_m, #tbl_n) + for i = 1, rslt do + if(tbl_m[i]== 0 or tbl_n[i] == 0) then + tbl[i] = 0 + else + tbl[i] = 1 + end + end + + return tbl_to_number(tbl) +end + +local function bit_xor(m, n) + local tbl_m = to_bits(m) + local tbl_n = to_bits(n) + expand(tbl_m, tbl_n) + + local tbl = {} + local rslt = max(#tbl_m, #tbl_n) + for i = 1, rslt do + if(tbl_m[i] ~= tbl_n[i]) then + tbl[i] = 1 + else + tbl[i] = 0 + end + end + + return tbl_to_number(tbl) +end + +local function bit_rshift(n, bits) + check_int(n) + + local high_bit = 0 + if(n < 0) then + -- negative + n = bit_not(abs(n)) + 1 + high_bit = 2147483648 -- 0x80000000 + end + + for i=1, bits do + n = n/2 + n = bit_or(floor(n), high_bit) + end + return floor(n) +end + +local function bit_lshift(n, bits) + check_int(n) + + if(n < 0) then + -- negative + n = bit_not(abs(n)) + 1 + end + + for i=1, bits do + n = n*2 + end + return bit_and(n, 4294967295) -- 0xFFFFFFFF +end + +-- convert little-endian 32-bit int to a 4-char string +local function lei_2_str(i) + local f=function (s) return char( bit_and( bit_rshift(i, s), 255)) end + return f(0)..f(8)..f(16)..f(24) +end + +-- convert raw string to big-endian int +local function str_2_bei(s) + local v=0 + for i=1, #s do + v = v * 256 + byte(s, i) + end + return v +end + +-- convert raw string to little-endian int +local function str_2_lei(s) + local v=0 + for i = #s,1,-1 do + v = v*256 + byte(s, i) + end + return v +end + +-- cut up a string in little-endian ints of given size +local function cut_le_str(s,...) + local o, r = 1, {} + local args = {...} + for i=1, #args do + table.insert(r, str_2_lei(sub(s, o, o + args[i] - 1))) + o = o + args[i] + end + return r +end + +local swap = function (w) return str_2_bei(lei_2_str(w)) end + +local function hex_to_binary(hex) + local result, _ = hex:gsub('..', function(hexval) + return string.char(tonumber(hexval, 16)) + end) + return result +end + +-- An MD5 mplementation in Lua, requires bitlib (hacked to use LuaBit from above, ugh) +-- 10/02/2001 jcw@equi4.com + +local FF = 0xffffffff +local CONSTS = { + 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, + 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, + 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, + 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, + 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, + 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, + 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, + 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, + 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, + 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, + 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, + 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, + 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, + 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, + 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, + 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391, + 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476 +} + +local f=function (x,y,z) return bit_or(bit_and(x,y),bit_and(-x-1,z)) end +local g=function (x,y,z) return bit_or(bit_and(x,z),bit_and(y,-z-1)) end +local h=function (x,y,z) return bit_xor(x,bit_xor(y,z)) end +local i=function (x,y,z) return bit_xor(y,bit_or(x,-z-1)) end +local z=function (f,a,b,c,d,x,s,ac) + a=bit_and(a+f(b,c,d)+x+ac,FF) + -- be *very* careful that left shift does not cause rounding! + return bit_or(bit_lshift(bit_and(a,bit_rshift(FF,s)),s),bit_rshift(a,32-s))+b +end + +local function transform(A,B,C,D,X) + local a,b,c,d=A,B,C,D + local t=CONSTS + + a=z(f,a,b,c,d,X[ 0], 7,t[ 1]) + d=z(f,d,a,b,c,X[ 1],12,t[ 2]) + c=z(f,c,d,a,b,X[ 2],17,t[ 3]) + b=z(f,b,c,d,a,X[ 3],22,t[ 4]) + a=z(f,a,b,c,d,X[ 4], 7,t[ 5]) + d=z(f,d,a,b,c,X[ 5],12,t[ 6]) + c=z(f,c,d,a,b,X[ 6],17,t[ 7]) + b=z(f,b,c,d,a,X[ 7],22,t[ 8]) + a=z(f,a,b,c,d,X[ 8], 7,t[ 9]) + d=z(f,d,a,b,c,X[ 9],12,t[10]) + c=z(f,c,d,a,b,X[10],17,t[11]) + b=z(f,b,c,d,a,X[11],22,t[12]) + a=z(f,a,b,c,d,X[12], 7,t[13]) + d=z(f,d,a,b,c,X[13],12,t[14]) + c=z(f,c,d,a,b,X[14],17,t[15]) + b=z(f,b,c,d,a,X[15],22,t[16]) + + a=z(g,a,b,c,d,X[ 1], 5,t[17]) + d=z(g,d,a,b,c,X[ 6], 9,t[18]) + c=z(g,c,d,a,b,X[11],14,t[19]) + b=z(g,b,c,d,a,X[ 0],20,t[20]) + a=z(g,a,b,c,d,X[ 5], 5,t[21]) + d=z(g,d,a,b,c,X[10], 9,t[22]) + c=z(g,c,d,a,b,X[15],14,t[23]) + b=z(g,b,c,d,a,X[ 4],20,t[24]) + a=z(g,a,b,c,d,X[ 9], 5,t[25]) + d=z(g,d,a,b,c,X[14], 9,t[26]) + c=z(g,c,d,a,b,X[ 3],14,t[27]) + b=z(g,b,c,d,a,X[ 8],20,t[28]) + a=z(g,a,b,c,d,X[13], 5,t[29]) + d=z(g,d,a,b,c,X[ 2], 9,t[30]) + c=z(g,c,d,a,b,X[ 7],14,t[31]) + b=z(g,b,c,d,a,X[12],20,t[32]) + + a=z(h,a,b,c,d,X[ 5], 4,t[33]) + d=z(h,d,a,b,c,X[ 8],11,t[34]) + c=z(h,c,d,a,b,X[11],16,t[35]) + b=z(h,b,c,d,a,X[14],23,t[36]) + a=z(h,a,b,c,d,X[ 1], 4,t[37]) + d=z(h,d,a,b,c,X[ 4],11,t[38]) + c=z(h,c,d,a,b,X[ 7],16,t[39]) + b=z(h,b,c,d,a,X[10],23,t[40]) + a=z(h,a,b,c,d,X[13], 4,t[41]) + d=z(h,d,a,b,c,X[ 0],11,t[42]) + c=z(h,c,d,a,b,X[ 3],16,t[43]) + b=z(h,b,c,d,a,X[ 6],23,t[44]) + a=z(h,a,b,c,d,X[ 9], 4,t[45]) + d=z(h,d,a,b,c,X[12],11,t[46]) + c=z(h,c,d,a,b,X[15],16,t[47]) + b=z(h,b,c,d,a,X[ 2],23,t[48]) + + a=z(i,a,b,c,d,X[ 0], 6,t[49]) + d=z(i,d,a,b,c,X[ 7],10,t[50]) + c=z(i,c,d,a,b,X[14],15,t[51]) + b=z(i,b,c,d,a,X[ 5],21,t[52]) + a=z(i,a,b,c,d,X[12], 6,t[53]) + d=z(i,d,a,b,c,X[ 3],10,t[54]) + c=z(i,c,d,a,b,X[10],15,t[55]) + b=z(i,b,c,d,a,X[ 1],21,t[56]) + a=z(i,a,b,c,d,X[ 8], 6,t[57]) + d=z(i,d,a,b,c,X[15],10,t[58]) + c=z(i,c,d,a,b,X[ 6],15,t[59]) + b=z(i,b,c,d,a,X[13],21,t[60]) + a=z(i,a,b,c,d,X[ 4], 6,t[61]) + d=z(i,d,a,b,c,X[11],10,t[62]) + c=z(i,c,d,a,b,X[ 2],15,t[63]) + b=z(i,b,c,d,a,X[ 9],21,t[64]) + + return A+a,B+b,C+c,D+d +end + +local md5 = {} + +function md5.sumhexa(s) + local msgLen = #s + local padLen = 56 - msgLen % 64 + + if msgLen % 64 > 56 then padLen = padLen + 64 end + + if padLen == 0 then padLen = 64 end + + s = s .. char(128) .. rep(char(0),padLen-1) .. + lei_2_str(8*msgLen) .. lei_2_str(0) + + assert(#s % 64 == 0) + + local t = CONSTS + local a,b,c,d = t[65],t[66],t[67],t[68] + + for i=1,#s,64 do + local X = cut_le_str(sub(s,i,i+63),4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4) + assert(#X == 16) + X[0] = table.remove(X,1) -- zero based! + a,b,c,d = transform(a,b,c,d,X) + end + + return format("%08x%08x%08x%08x",swap(a),swap(b),swap(c),swap(d)) +end + +function md5.sum(s) + return hex_to_binary(md5.sumhexa(s)) +end + +return md5 diff --git a/spec/md5_spec.lua b/spec/md5_spec.lua new file mode 100644 index 0000000..d31ed04 --- /dev/null +++ b/spec/md5_spec.lua @@ -0,0 +1,28 @@ +local md5 = require('md5') + +local function hex2bin(hex) + local result, _ = hex:gsub('..', function(hexval) + return string.char(tonumber(hexval, 16)) + end) + return result +end + +describe('md5', function() + describe('md5.sumhexa', function() + it('works', function() + assert.equal(md5.sumhexa("asdf"), '912ec803b2ce49e4a541068d495ab570') + assert.equal(md5.sumhexa('The quick brown fox jumps over the lazy dog'), '9e107d9d372bb6826bd81d3542a419d6') + assert.equal(md5.sumhexa('The quick brown fox jumps over the lazy dog.'), 'e4d909c290d0fb1ca068ffaddf22cbd0') + assert.equal(md5.sumhexa(''), 'd41d8cd98f00b204e9800998ecf8427e') + end) + end) + + describe('md5.sum', function() + it('works', function() + assert.equal(md5.sum("asdf"), hex2bin '912ec803b2ce49e4a541068d495ab570') + assert.equal(md5.sum('The quick brown fox jumps over the lazy dog'), hex2bin'9e107d9d372bb6826bd81d3542a419d6') + assert.equal(md5.sum('The quick brown fox jumps over the lazy dog.'), hex2bin'e4d909c290d0fb1ca068ffaddf22cbd0') + assert.equal(md5.sum(''), hex2bin'd41d8cd98f00b204e9800998ecf8427e') + end) + end) +end) From a9905a9c13814a2beefe32c927cda1c3c040b072 Mon Sep 17 00:00:00 2001 From: kikito Date: Fri, 30 Aug 2013 19:16:49 +0200 Subject: [PATCH 02/60] more cleanup --- md5.lua | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/md5.lua b/md5.lua index 6007eef..14b5ac3 100644 --- a/md5.lua +++ b/md5.lua @@ -14,7 +14,7 @@ local function check_int(n) end end -local function tbl_to_number(tbl) +local function tbl2number(tbl) local n = #tbl local rslt = 0 @@ -56,7 +56,7 @@ local function bit_not(n) tbl[i] = 1 end end - return tbl_to_number(tbl) + return tbl2number(tbl) end -- defined as local above @@ -98,7 +98,7 @@ local function bit_or(m, n) end end - return tbl_to_number(tbl) + return tbl2number(tbl) end local function bit_and(m, n) @@ -116,7 +116,7 @@ local function bit_and(m, n) end end - return tbl_to_number(tbl) + return tbl2number(tbl) end local function bit_xor(m, n) @@ -134,7 +134,7 @@ local function bit_xor(m, n) end end - return tbl_to_number(tbl) + return tbl2number(tbl) end local function bit_rshift(n, bits) @@ -169,13 +169,13 @@ local function bit_lshift(n, bits) end -- convert little-endian 32-bit int to a 4-char string -local function lei_2_str(i) +local function lei2str(i) local f=function (s) return char( bit_and( bit_rshift(i, s), 255)) end return f(0)..f(8)..f(16)..f(24) end -- convert raw string to big-endian int -local function str_2_bei(s) +local function str2bei(s) local v=0 for i=1, #s do v = v * 256 + byte(s, i) @@ -184,7 +184,7 @@ local function str_2_bei(s) end -- convert raw string to little-endian int -local function str_2_lei(s) +local function str2lei(s) local v=0 for i = #s,1,-1 do v = v*256 + byte(s, i) @@ -197,18 +197,20 @@ local function cut_le_str(s,...) local o, r = 1, {} local args = {...} for i=1, #args do - table.insert(r, str_2_lei(sub(s, o, o + args[i] - 1))) + table.insert(r, str2lei(sub(s, o, o + args[i] - 1))) o = o + args[i] end return r end -local swap = function (w) return str_2_bei(lei_2_str(w)) end +local swap = function (w) return str2bei(lei2str(w)) end -local function hex_to_binary(hex) - local result, _ = hex:gsub('..', function(hexval) - return string.char(tonumber(hexval, 16)) - end) +local function hex2binaryaux(hexval) + return char(tonumber(hexval, 16)) +end + +local function hex2binary(hex) + local result, _ = hex:gsub('..', hex2binaryaux) return result end @@ -321,6 +323,8 @@ local function transform(A,B,C,D,X) return A+a,B+b,C+c,D+d end +---------------------------------------------------------------- + local md5 = {} function md5.sumhexa(s) @@ -331,8 +335,7 @@ function md5.sumhexa(s) if padLen == 0 then padLen = 64 end - s = s .. char(128) .. rep(char(0),padLen-1) .. - lei_2_str(8*msgLen) .. lei_2_str(0) + s = s .. char(128) .. rep(char(0),padLen-1) .. lei2str(8*msgLen) .. lei2str(0) assert(#s % 64 == 0) @@ -350,7 +353,7 @@ function md5.sumhexa(s) end function md5.sum(s) - return hex_to_binary(md5.sumhexa(s)) + return hex2binary(md5.sumhexa(s)) end return md5 From a34df17b174c64c863d20336d0782344a7335df5 Mon Sep 17 00:00:00 2001 From: kikito Date: Sat, 14 Sep 2013 14:14:06 +0200 Subject: [PATCH 03/60] added VERSION, DESCRIPTION, URL and LICENSE --- md5.lua | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/md5.lua b/md5.lua index 14b5ac3..8b03439 100644 --- a/md5.lua +++ b/md5.lua @@ -1,6 +1,33 @@ -------------------------------------------------------------------------------- --- MD5 computation in Lua (5.1) -------------------------------------------------------------------------------- +local md5 = { + _VERSION = "md5.lua 0.5.0", + _DESCRIPTION = "MD5 computation in Lua (5.1)", + _URL = "https://github.com/kikito/md5.lua", + _LICENSE = [[ + MIT LICENSE + + Copyright (c) 2013 Enrique García Cota + Adam Baldwin + hanzao + Equi 4 Software + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the + "Software"), to deal in the Software without restriction, including + without limitation the rights to use, copy, modify, merge, publish, + distribute, sublicense, and/or sell copies of the Software, and to + permit persons to whom the Software is furnished to do so, subject to + the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + ]] +} + -- bit lib implementions local floor, abs, max = math.floor, math.abs, math.max @@ -206,7 +233,7 @@ end local swap = function (w) return str2bei(lei2str(w)) end local function hex2binaryaux(hexval) - return char(tonumber(hexval, 16)) + return char(tonumber(hexval, 16)) end local function hex2binary(hex) @@ -325,8 +352,6 @@ end ---------------------------------------------------------------- -local md5 = {} - function md5.sumhexa(s) local msgLen = #s local padLen = 56 - msgLen % 64 From 191b02680778664f80f03adc7bfa0dcc873ed32d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Garc=C3=ADa?= Date: Fri, 21 Feb 2014 10:09:19 +0100 Subject: [PATCH 04/60] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6591c11..b396b2c 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Usage local md5 = require 'md5' - local md5_as_hex = md5.sumhex(message) -- returns a hex string + local md5_as_hex = md5.sumhexa(message) -- returns a hex string local md5_as_data = md5.sum(message) -- returns raw bytes Credits From e7df9d53cfc68f311aa7adcb87be2cf3742e0fdd Mon Sep 17 00:00:00 2001 From: Pablo Mayobre Date: Sat, 7 Feb 2015 23:13:01 -0300 Subject: [PATCH 05/60] Made md5.lua use BitOp or Bit32 libraries where available --- md5.lua | 298 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 153 insertions(+), 145 deletions(-) diff --git a/md5.lua b/md5.lua index 8b03439..85753a3 100644 --- a/md5.lua +++ b/md5.lua @@ -33,166 +33,174 @@ local md5 = { local floor, abs, max = math.floor, math.abs, math.max local char, byte, format, rep, sub = string.char, string.byte, string.format, string.rep, string.sub +local bit_or, bit_and, bit_not, bit_xor, bit_rshift, bit_lshift -local function check_int(n) - -- checking not float - if(n - floor(n) > 0) then - error("trying to use bitwise operation on non-integer!") - end -end - -local function tbl2number(tbl) - local n = #tbl - - local rslt = 0 - local power = 1 - for i = 1, n do - rslt = rslt + tbl[i]*power - power = power*2 - end - - return rslt -end - -local function expand(tbl_m, tbl_n) - local big = {} - local small = {} - if(#tbl_m > #tbl_n) then - big = tbl_m - small = tbl_n - else - big = tbl_n - small = tbl_m - end - -- expand small - for i = #small + 1, #big do - small[i] = 0 - end - -end - -local to_bits -- needs to be declared before bit_not - -local function bit_not(n) - local tbl = to_bits(n) - local size = max(#tbl, 32) - for i = 1, size do - if(tbl[i] == 1) then - tbl[i] = 0 - else - tbl[i] = 1 +if require "bit" or require "bit32" then + local bit = require "bit" or require "bit32" + + bit_or, bit_and, bit_not, bit_xor = bit.bor, bit.band, bit.bnot, bit.bxor + bit_rshift, bit_lshift = bit.rshift, bit.lshift +else + local function check_int(n) + -- checking not float + if(n - floor(n) > 0) then + error("trying to use bitwise operation on non-integer!") end end - return tbl2number(tbl) -end - --- defined as local above -to_bits = function (n) - check_int(n) - if(n < 0) then - -- negative - return to_bits(bit_not(abs(n)) + 1) - end - -- to bits table - local tbl = {} - local cnt = 1 - while (n > 0) do - local last = math.mod(n,2) - if(last == 1) then - tbl[cnt] = 1 - else - tbl[cnt] = 0 + + local function tbl2number(tbl) + local n = #tbl + + local rslt = 0 + local power = 1 + for i = 1, n do + rslt = rslt + tbl[i]*power + power = power*2 end - n = (n-last)/2 - cnt = cnt + 1 + + return rslt end - - return tbl -end - -local function bit_or(m, n) - local tbl_m = to_bits(m) - local tbl_n = to_bits(n) - expand(tbl_m, tbl_n) - - local tbl = {} - local rslt = max(#tbl_m, #tbl_n) - for i = 1, rslt do - if(tbl_m[i]== 0 and tbl_n[i] == 0) then - tbl[i] = 0 + + local function expand(tbl_m, tbl_n) + local big = {} + local small = {} + if(#tbl_m > #tbl_n) then + big = tbl_m + small = tbl_n else - tbl[i] = 1 + big = tbl_n + small = tbl_m end - end - - return tbl2number(tbl) -end - -local function bit_and(m, n) - local tbl_m = to_bits(m) - local tbl_n = to_bits(n) - expand(tbl_m, tbl_n) - - local tbl = {} - local rslt = max(#tbl_m, #tbl_n) - for i = 1, rslt do - if(tbl_m[i]== 0 or tbl_n[i] == 0) then - tbl[i] = 0 - else - tbl[i] = 1 + -- expand small + for i = #small + 1, #big do + small[i] = 0 end + end - - return tbl2number(tbl) -end - -local function bit_xor(m, n) - local tbl_m = to_bits(m) - local tbl_n = to_bits(n) - expand(tbl_m, tbl_n) - - local tbl = {} - local rslt = max(#tbl_m, #tbl_n) - for i = 1, rslt do - if(tbl_m[i] ~= tbl_n[i]) then - tbl[i] = 1 - else - tbl[i] = 0 + + local to_bits -- needs to be declared before bit_not + + function bit_not(n) + local tbl = to_bits(n) + local size = max(#tbl, 32) + for i = 1, size do + if(tbl[i] == 1) then + tbl[i] = 0 + else + tbl[i] = 1 + end end + return tbl2number(tbl) end - - return tbl2number(tbl) -end - -local function bit_rshift(n, bits) - check_int(n) - - local high_bit = 0 - if(n < 0) then - -- negative - n = bit_not(abs(n)) + 1 - high_bit = 2147483648 -- 0x80000000 + + -- defined as local above + to_bits = function (n) + check_int(n) + if(n < 0) then + -- negative + return to_bits(bit_not(abs(n)) + 1) + end + -- to bits table + local tbl = {} + local cnt = 1 + while (n > 0) do + local last = math.mod(n,2) + if(last == 1) then + tbl[cnt] = 1 + else + tbl[cnt] = 0 + end + n = (n-last)/2 + cnt = cnt + 1 + end + + return tbl end - - for i=1, bits do - n = n/2 - n = bit_or(floor(n), high_bit) + + function bit_or(m, n) + local tbl_m = to_bits(m) + local tbl_n = to_bits(n) + expand(tbl_m, tbl_n) + + local tbl = {} + local rslt = max(#tbl_m, #tbl_n) + for i = 1, rslt do + if(tbl_m[i]== 0 and tbl_n[i] == 0) then + tbl[i] = 0 + else + tbl[i] = 1 + end + end + + return tbl2number(tbl) end - return floor(n) -end - -local function bit_lshift(n, bits) - check_int(n) - - if(n < 0) then - -- negative - n = bit_not(abs(n)) + 1 + + function bit_and(m, n) + local tbl_m = to_bits(m) + local tbl_n = to_bits(n) + expand(tbl_m, tbl_n) + + local tbl = {} + local rslt = max(#tbl_m, #tbl_n) + for i = 1, rslt do + if(tbl_m[i]== 0 or tbl_n[i] == 0) then + tbl[i] = 0 + else + tbl[i] = 1 + end + end + + return tbl2number(tbl) end - - for i=1, bits do - n = n*2 + + function bit_xor(m, n) + local tbl_m = to_bits(m) + local tbl_n = to_bits(n) + expand(tbl_m, tbl_n) + + local tbl = {} + local rslt = max(#tbl_m, #tbl_n) + for i = 1, rslt do + if(tbl_m[i] ~= tbl_n[i]) then + tbl[i] = 1 + else + tbl[i] = 0 + end + end + + return tbl2number(tbl) + end + + function bit_rshift(n, bits) + check_int(n) + + local high_bit = 0 + if(n < 0) then + -- negative + n = bit_not(abs(n)) + 1 + high_bit = 2147483648 -- 0x80000000 + end + + for i=1, bits do + n = n/2 + n = bit_or(floor(n), high_bit) + end + return floor(n) + end + + function bit_lshift(n, bits) + check_int(n) + + if(n < 0) then + -- negative + n = bit_not(abs(n)) + 1 + end + + for i=1, bits do + n = n*2 + end + return bit_and(n, 4294967295) -- 0xFFFFFFFF end - return bit_and(n, 4294967295) -- 0xFFFFFFFF end -- convert little-endian 32-bit int to a 4-char string From 57cc66eba67cfff5a9868d3586fdc1a7d9857335 Mon Sep 17 00:00:00 2001 From: Pablo Mayobre Date: Sun, 8 Feb 2015 07:28:31 -0300 Subject: [PATCH 06/60] Checking the libs doesnt error Added pcalls instead of raw requires as suggested by Kikito --- md5.lua | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/md5.lua b/md5.lua index 85753a3..05cafc3 100644 --- a/md5.lua +++ b/md5.lua @@ -35,9 +35,10 @@ local char, byte, format, rep, sub = string.char, string.byte, string.format, string.rep, string.sub local bit_or, bit_and, bit_not, bit_xor, bit_rshift, bit_lshift -if require "bit" or require "bit32" then - local bit = require "bit" or require "bit32" - +local ok, bit = pcall(require, 'bit') +if not ok then ok, bit = pcall(require, 'bit32') end + +if ok then bit_or, bit_and, bit_not, bit_xor = bit.bor, bit.band, bit.bnot, bit.bxor bit_rshift, bit_lshift = bit.rshift, bit.lshift else From 3289e9fa7c2e6eb1ce34df86047e03cde37d79d8 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 9 Feb 2015 11:16:47 +0100 Subject: [PATCH 07/60] change README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b396b2c..dd3bffd 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ md5.lua ======== -This pure-Lua module computes md5 in Lua 5.1. +This pure-Lua module computes md5 in Lua 5.1, Lua 5.2 and LuaJIT, using native bit-manipulation libraries when available, and falling back to table-based manipulation of integers in 5.1. It implements md5.sum and md5.sumhex like the [kernel project md5 package](http://www.keplerproject.org/md5/), but it's done completely in Lua, with no dependencies on other libs or C files. From c3ed7b8713694bce5d3806ae550486cfd3501e42 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 9 Feb 2015 11:27:21 +0100 Subject: [PATCH 08/60] add travis --- .travis.yml | 37 +++++++++++++++++++++++++++++++++++++ README.md | 4 ++-- 2 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..b7e6f4b --- /dev/null +++ b/.travis.yml @@ -0,0 +1,37 @@ +language: erlang + +env: + global: + - LUAROCKS_BASE=luarocks-2.0.13 + matrix: + - LUA=lua5.1 LUA_DEV=liblua5.1-dev LUA_VER=5.1 LUA_SFX=5.1 LUA_INCDIR=/usr/include/lua5.1 + - LUA=lua5.2 LUA_DEV=liblua5.2-dev LUA_VER=5.2 LUA_SFX=5.2 LUA_INCDIR=/usr/include/lua5.2 + - LUA=luajit LUA_DEV=libluajit-5.1-dev LUA_VER=5.1 LUA_SFX=jit LUA_INCDIR=/usr/include/luajit-2.0 + +before_install: + - if [ $LUA = "luajit" ]; then + sudo add-apt-repository ppa:mwild1/ppa -y && sudo apt-get update -y; + fi + - sudo apt-get install $LUA + - sudo apt-get install $LUA_DEV + - lua$LUA_SFX -v + # Install a recent luarocks release + - wget http://luarocks.org/releases/$LUAROCKS_BASE.tar.gz + - tar zxvpf $LUAROCKS_BASE.tar.gz + - cd $LUAROCKS_BASE + - ./configure + --lua-version=$LUA_VER --lua-suffix=$LUA_SFX --with-lua-include="$LUA_INCDIR" + - sudo make + - sudo make install + - cd $TRAVIS_BUILD_DIR + +install: + - sudo -E luarocks install busted 1.11.1-1 + +script: + - sudo -E busted -v + +notifications: + email: + on_success: change + on_failure: always diff --git a/README.md b/README.md index dd3bffd..1b22a9b 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -md5.lua -======== +md5.lua [![Build Status](https://travis-ci.org/kikito/md5.lua.svg)](https://travis-ci.org/kikito/md5.lua) +========================================================================================================= This pure-Lua module computes md5 in Lua 5.1, Lua 5.2 and LuaJIT, using native bit-manipulation libraries when available, and falling back to table-based manipulation of integers in 5.1. From 98844a67fed08f39ee86a2b1bab050a24077abca Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 9 Feb 2015 11:38:22 +0100 Subject: [PATCH 09/60] update luarocks version in travis --- .travis.yml | 2 +- md5.lua | 52 ++++++++++++++++++++++++++-------------------------- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/.travis.yml b/.travis.yml index b7e6f4b..cf1c620 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,7 @@ language: erlang env: global: - - LUAROCKS_BASE=luarocks-2.0.13 + - LUAROCKS_BASE=luarocks-2.2.0 matrix: - LUA=lua5.1 LUA_DEV=liblua5.1-dev LUA_VER=5.1 LUA_SFX=5.1 LUA_INCDIR=/usr/include/lua5.1 - LUA=lua5.2 LUA_DEV=liblua5.2-dev LUA_VER=5.2 LUA_SFX=5.2 LUA_INCDIR=/usr/include/lua5.2 diff --git a/md5.lua b/md5.lua index 05cafc3..a68bbc3 100644 --- a/md5.lua +++ b/md5.lua @@ -1,6 +1,6 @@ local md5 = { - _VERSION = "md5.lua 0.5.0", - _DESCRIPTION = "MD5 computation in Lua (5.1)", + _VERSION = "md5.lua 1.0.0", + _DESCRIPTION = "MD5 computation in Lua (5.1-3, LuaJIT)", _URL = "https://github.com/kikito/md5.lua", _LICENSE = [[ MIT LICENSE @@ -48,20 +48,20 @@ else error("trying to use bitwise operation on non-integer!") end end - + local function tbl2number(tbl) local n = #tbl - + local rslt = 0 local power = 1 for i = 1, n do rslt = rslt + tbl[i]*power power = power*2 end - + return rslt end - + local function expand(tbl_m, tbl_n) local big = {} local small = {} @@ -76,11 +76,11 @@ else for i = #small + 1, #big do small[i] = 0 end - + end - + local to_bits -- needs to be declared before bit_not - + function bit_not(n) local tbl = to_bits(n) local size = max(#tbl, 32) @@ -93,7 +93,7 @@ else end return tbl2number(tbl) end - + -- defined as local above to_bits = function (n) check_int(n) @@ -114,15 +114,15 @@ else n = (n-last)/2 cnt = cnt + 1 end - + return tbl end - + function bit_or(m, n) local tbl_m = to_bits(m) local tbl_n = to_bits(n) expand(tbl_m, tbl_n) - + local tbl = {} local rslt = max(#tbl_m, #tbl_n) for i = 1, rslt do @@ -132,15 +132,15 @@ else tbl[i] = 1 end end - + return tbl2number(tbl) end - + function bit_and(m, n) local tbl_m = to_bits(m) local tbl_n = to_bits(n) expand(tbl_m, tbl_n) - + local tbl = {} local rslt = max(#tbl_m, #tbl_n) for i = 1, rslt do @@ -150,15 +150,15 @@ else tbl[i] = 1 end end - + return tbl2number(tbl) end - + function bit_xor(m, n) local tbl_m = to_bits(m) local tbl_n = to_bits(n) expand(tbl_m, tbl_n) - + local tbl = {} local rslt = max(#tbl_m, #tbl_n) for i = 1, rslt do @@ -168,35 +168,35 @@ else tbl[i] = 0 end end - + return tbl2number(tbl) end - + function bit_rshift(n, bits) check_int(n) - + local high_bit = 0 if(n < 0) then -- negative n = bit_not(abs(n)) + 1 high_bit = 2147483648 -- 0x80000000 end - + for i=1, bits do n = n/2 n = bit_or(floor(n), high_bit) end return floor(n) end - + function bit_lshift(n, bits) check_int(n) - + if(n < 0) then -- negative n = bit_not(abs(n)) + 1 end - + for i=1, bits do n = n*2 end From b75e79ea6d63f5c2d2b65dcc71b0ef45a8a032ea Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 9 Feb 2015 11:40:43 +0100 Subject: [PATCH 10/60] downgrade luarocks --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index cf1c620..7ac61c6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,7 @@ language: erlang env: global: - - LUAROCKS_BASE=luarocks-2.2.0 + - LUAROCKS_BASE=luarocks-2.1.0 matrix: - LUA=lua5.1 LUA_DEV=liblua5.1-dev LUA_VER=5.1 LUA_SFX=5.1 LUA_INCDIR=/usr/include/lua5.1 - LUA=lua5.2 LUA_DEV=liblua5.2-dev LUA_VER=5.2 LUA_SFX=5.2 LUA_INCDIR=/usr/include/lua5.2 From af6e779eb8541c415c389f101b7aa5c0be1b29ef Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 9 Feb 2015 12:06:47 +0100 Subject: [PATCH 11/60] up luarocks a little more --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 7ac61c6..344cadc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,7 @@ language: erlang env: global: - - LUAROCKS_BASE=luarocks-2.1.0 + - LUAROCKS_BASE=luarocks-2.1.2 matrix: - LUA=lua5.1 LUA_DEV=liblua5.1-dev LUA_VER=5.1 LUA_SFX=5.1 LUA_INCDIR=/usr/include/lua5.1 - LUA=lua5.2 LUA_DEV=liblua5.2-dev LUA_VER=5.2 LUA_SFX=5.2 LUA_INCDIR=/usr/include/lua5.2 From b97a79f022ceac4d7ad8ee758d3d0907b261a54c Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 9 Feb 2015 12:16:13 +0100 Subject: [PATCH 12/60] Revert "up luarocks a little more" This reverts commit af6e779eb8541c415c389f101b7aa5c0be1b29ef. --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 344cadc..7ac61c6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,7 @@ language: erlang env: global: - - LUAROCKS_BASE=luarocks-2.1.2 + - LUAROCKS_BASE=luarocks-2.1.0 matrix: - LUA=lua5.1 LUA_DEV=liblua5.1-dev LUA_VER=5.1 LUA_SFX=5.1 LUA_INCDIR=/usr/include/lua5.1 - LUA=lua5.2 LUA_DEV=liblua5.2-dev LUA_VER=5.2 LUA_SFX=5.2 LUA_INCDIR=/usr/include/lua5.2 From 1c76e92944b4b5fdaeffa039e826c440164a4c81 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 9 Feb 2015 13:05:06 +0100 Subject: [PATCH 13/60] add rockspecs --- rockspecs/md5-1.0-0.rockspec | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 rockspecs/md5-1.0-0.rockspec diff --git a/rockspecs/md5-1.0-0.rockspec b/rockspecs/md5-1.0-0.rockspec new file mode 100644 index 0000000..4103148 --- /dev/null +++ b/rockspecs/md5-1.0-0.rockspec @@ -0,0 +1,21 @@ +package = "md5" +version = "1.0-0" +source = { + url = "https://github.com/kikito/md5.lua/archive/v1.0.0.tar.gz", + dir = "md5.lua-1.0.0" +} +description = { + summary = "MD5 sum in pure Lua, with no C and no external dependencies", + detailed = "This pure-Lua module computes md5 in Lua 5.1, Lua 5.2 and LuaJIT, using native bit-manipulation libraries when available, and falling back to table-based manipulation of integers in 5.1", + homepage = "https://github.com/kikito/md5.lua", + license = "MIT" +} +dependencies = { + "lua >= 5.1" +} +build = { + type = "builtin", + modules = { + md5 = "md5.lua" + } +} From 9a87570317064df871cb70128f63079d965e55c3 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 9 Feb 2015 13:07:59 +0100 Subject: [PATCH 14/60] add Positive07 to credits --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 1b22a9b..38fd023 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,8 @@ This is a cleanup of an implementation by Adam Baldwin - https://gist.github.com Which in turn was a mix of the bitwise lib, http://luaforge.net/projects/bit/ by hanzhao (`abrash_han - at - hotmail.com`), and http://equi4.com/md5/md5calc.lua, by Equi 4 Software. +Lua 5.2 and LuaJIT compatibility by [Positive07](https://github.com/kikito/md5.lua/pull/2) + License ======= From d1fc9bc9f7fc6146cb6ed264771716a5571da3f4 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 6 Apr 2015 14:09:32 +0200 Subject: [PATCH 15/60] add test for long string. Related with #4 --- spec/md5_spec.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spec/md5_spec.lua b/spec/md5_spec.lua index d31ed04..ed00a0e 100644 --- a/spec/md5_spec.lua +++ b/spec/md5_spec.lua @@ -14,6 +14,7 @@ describe('md5', function() assert.equal(md5.sumhexa('The quick brown fox jumps over the lazy dog'), '9e107d9d372bb6826bd81d3542a419d6') assert.equal(md5.sumhexa('The quick brown fox jumps over the lazy dog.'), 'e4d909c290d0fb1ca068ffaddf22cbd0') assert.equal(md5.sumhexa(''), 'd41d8cd98f00b204e9800998ecf8427e') + assert.equal(md5.sumhexa(('1'):rep(824)), 'a126fd3611ab8d9b7e8a3384e2fa78a0') end) end) @@ -23,6 +24,7 @@ describe('md5', function() assert.equal(md5.sum('The quick brown fox jumps over the lazy dog'), hex2bin'9e107d9d372bb6826bd81d3542a419d6') assert.equal(md5.sum('The quick brown fox jumps over the lazy dog.'), hex2bin'e4d909c290d0fb1ca068ffaddf22cbd0') assert.equal(md5.sum(''), hex2bin'd41d8cd98f00b204e9800998ecf8427e') + assert.equal(md5.sum(('1'):rep(824)), hex2bin'a126fd3611ab8d9b7e8a3384e2fa78a0') end) end) end) From cfbceec4ac310f09833178010f0af6fb63e559de Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 6 Apr 2015 14:22:03 +0200 Subject: [PATCH 16/60] attempt to fix travis --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 7ac61c6..06fbe8a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,6 +26,7 @@ before_install: - cd $TRAVIS_BUILD_DIR install: + - sudo -E luarocks install lua_cliargs 2.3-3 - sudo -E luarocks install busted 1.11.1-1 script: From 60d897e2298ca1033c79dca8895eaa3a20a63752 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 6 Apr 2015 16:54:24 +0200 Subject: [PATCH 17/60] attempt to fix lua 5.2 --- md5.lua | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/md5.lua b/md5.lua index a68bbc3..bf57972 100644 --- a/md5.lua +++ b/md5.lua @@ -39,8 +39,20 @@ local ok, bit = pcall(require, 'bit') if not ok then ok, bit = pcall(require, 'bit32') end if ok then - bit_or, bit_and, bit_not, bit_xor = bit.bor, bit.band, bit.bnot, bit.bxor - bit_rshift, bit_lshift = bit.rshift, bit.lshift + + bit_not = bit.bnot + + local tobit = function(n) + return n <= 0x7fffffff and n or -(bit_not(n) + 1) + end + + local normalize = function(f) + return function(a,b) return tobit(f(tobit(a), tobit(b))) end + end + + bit_or, bit_and, bit_xor = normalize(bit.bor), normalize(bit.band), normalize(bit.bxor) + bit_rshift, bit_lshift = normailize(bit.rshift), normalize(bit.lshift) + else local function check_int(n) -- checking not float From 6a83ce8c21a0e29449c1f82d64795880a044877f Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 6 Apr 2015 17:09:13 +0200 Subject: [PATCH 18/60] typo --- md5.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/md5.lua b/md5.lua index bf57972..4fae313 100644 --- a/md5.lua +++ b/md5.lua @@ -51,7 +51,7 @@ if ok then end bit_or, bit_and, bit_xor = normalize(bit.bor), normalize(bit.band), normalize(bit.bxor) - bit_rshift, bit_lshift = normailize(bit.rshift), normalize(bit.lshift) + bit_rshift, bit_lshift = normalize(bit.rshift), normalize(bit.lshift) else local function check_int(n) From 0950963cd3d47522a1751ad06ad2386756a51015 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 6 Apr 2015 17:22:48 +0200 Subject: [PATCH 19/60] Remove unnecessary checks in lua 5.1 --- md5.lua | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/md5.lua b/md5.lua index 4fae313..6c02f32 100644 --- a/md5.lua +++ b/md5.lua @@ -54,12 +54,6 @@ if ok then bit_rshift, bit_lshift = normalize(bit.rshift), normalize(bit.lshift) else - local function check_int(n) - -- checking not float - if(n - floor(n) > 0) then - error("trying to use bitwise operation on non-integer!") - end - end local function tbl2number(tbl) local n = #tbl @@ -88,7 +82,6 @@ else for i = #small + 1, #big do small[i] = 0 end - end local to_bits -- needs to be declared before bit_not @@ -108,7 +101,6 @@ else -- defined as local above to_bits = function (n) - check_int(n) if(n < 0) then -- negative return to_bits(bit_not(abs(n)) + 1) @@ -185,8 +177,6 @@ else end function bit_rshift(n, bits) - check_int(n) - local high_bit = 0 if(n < 0) then -- negative @@ -202,8 +192,6 @@ else end function bit_lshift(n, bits) - check_int(n) - if(n < 0) then -- negative n = bit_not(abs(n)) + 1 From a08ab4f93d058b65a8cff4523381ada56fab5824 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 6 Apr 2015 17:23:12 +0200 Subject: [PATCH 20/60] Refactor expand in Lua 5.1 --- md5.lua | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/md5.lua b/md5.lua index 6c02f32..dc313b2 100644 --- a/md5.lua +++ b/md5.lua @@ -68,15 +68,10 @@ else return rslt end - local function expand(tbl_m, tbl_n) - local big = {} - local small = {} - if(#tbl_m > #tbl_n) then - big = tbl_m - small = tbl_n - else - big = tbl_n - small = tbl_m + local function expand(t1, t2) + local big, small = t1, t2 + if(#big < #small) then + big, small = small, big end -- expand small for i = #small + 1, #big do From 332517912d5dc0b2a22fb22bb0112afd51f37d1b Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 6 Apr 2015 17:24:46 +0200 Subject: [PATCH 21/60] refactor tbl2number --- md5.lua | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/md5.lua b/md5.lua index dc313b2..c25a6e0 100644 --- a/md5.lua +++ b/md5.lua @@ -56,16 +56,13 @@ if ok then else local function tbl2number(tbl) - local n = #tbl - - local rslt = 0 + local result = 0 local power = 1 - for i = 1, n do - rslt = rslt + tbl[i]*power - power = power*2 + for i = 1, #tbl do + result = result + tbl[i] * power + power = power * 2 end - - return rslt + return result end local function expand(t1, t2) From 858b23b28d727f1baf3b24669b0c60ef95ab7874 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 6 Apr 2015 17:37:16 +0200 Subject: [PATCH 22/60] refactor to_bits --- md5.lua | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/md5.lua b/md5.lua index c25a6e0..0ffa585 100644 --- a/md5.lua +++ b/md5.lua @@ -100,15 +100,12 @@ else -- to bits table local tbl = {} local cnt = 1 - while (n > 0) do - local last = math.mod(n,2) - if(last == 1) then - tbl[cnt] = 1 - else - tbl[cnt] = 0 - end - n = (n-last)/2 - cnt = cnt + 1 + local last + while n > 0 do + last = n % 2 + tbl[cnt] = last + n = (n-last)/2 + cnt = cnt + 1 end return tbl From 907bbd16552acd1cf089a5e2b05056f3357a55dc Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 6 Apr 2015 17:39:17 +0200 Subject: [PATCH 23/60] Remove unneeded locals --- md5.lua | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/md5.lua b/md5.lua index 0ffa585..868e7b8 100644 --- a/md5.lua +++ b/md5.lua @@ -30,7 +30,6 @@ local md5 = { -- bit lib implementions -local floor, abs, max = math.floor, math.abs, math.max local char, byte, format, rep, sub = string.char, string.byte, string.format, string.rep, string.sub local bit_or, bit_and, bit_not, bit_xor, bit_rshift, bit_lshift @@ -80,7 +79,7 @@ else function bit_not(n) local tbl = to_bits(n) - local size = max(#tbl, 32) + local size = math.max(#tbl, 32) for i = 1, size do if(tbl[i] == 1) then tbl[i] = 0 @@ -95,7 +94,7 @@ else to_bits = function (n) if(n < 0) then -- negative - return to_bits(bit_not(abs(n)) + 1) + return to_bits(bit_not(math.abs(n)) + 1) end -- to bits table local tbl = {} @@ -169,10 +168,12 @@ else local high_bit = 0 if(n < 0) then -- negative - n = bit_not(abs(n)) + 1 + n = bit_not(math.abs(n)) + 1 high_bit = 2147483648 -- 0x80000000 end + local floor = math.floor + for i=1, bits do n = n/2 n = bit_or(floor(n), high_bit) @@ -183,7 +184,7 @@ else function bit_lshift(n, bits) if(n < 0) then -- negative - n = bit_not(abs(n)) + 1 + n = bit_not(math.abs(n)) + 1 end for i=1, bits do From 77ac109e6613317419b27e7ef4bf42976d965796 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 6 Apr 2015 17:41:57 +0200 Subject: [PATCH 24/60] Remove unnecessary calculations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The “expand” before the loops ensures that both tables (tbl_m and tbl_n) have the same length --- md5.lua | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/md5.lua b/md5.lua index 868e7b8..b99792b 100644 --- a/md5.lua +++ b/md5.lua @@ -116,8 +116,7 @@ else expand(tbl_m, tbl_n) local tbl = {} - local rslt = max(#tbl_m, #tbl_n) - for i = 1, rslt do + for i = 1, #tbl_m do if(tbl_m[i]== 0 and tbl_n[i] == 0) then tbl[i] = 0 else @@ -134,8 +133,7 @@ else expand(tbl_m, tbl_n) local tbl = {} - local rslt = max(#tbl_m, #tbl_n) - for i = 1, rslt do + for i = 1, #tbl_m do if(tbl_m[i]== 0 or tbl_n[i] == 0) then tbl[i] = 0 else @@ -152,8 +150,7 @@ else expand(tbl_m, tbl_n) local tbl = {} - local rslt = max(#tbl_m, #tbl_n) - for i = 1, rslt do + for i = 1, #tbl_m do if(tbl_m[i] ~= tbl_n[i]) then tbl[i] = 1 else From 65d03bceefdfdf3b25f769bc772b422041b8005f Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 6 Apr 2015 18:13:47 +0200 Subject: [PATCH 25/60] separate lua 5.1 functions into number-based and table-based --- md5.lua | 63 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/md5.lua b/md5.lua index b99792b..f9a0e9a 100644 --- a/md5.lua +++ b/md5.lua @@ -75,24 +75,27 @@ else end end + local bits_not, bits_or, bits_and, bits_not, bits_xor local to_bits -- needs to be declared before bit_not - function bit_not(n) - local tbl = to_bits(n) - local size = math.max(#tbl, 32) - for i = 1, size do - if(tbl[i] == 1) then + bits_not = function(tbl) + for i=1, math.max(#tbl, 32) do + if tbl[i] == 1 then tbl[i] = 0 else tbl[i] = 1 end end - return tbl2number(tbl) + return tbl + end + + bit_not = function(n) + return tbl2number(bits_not(to_bits(n))) end -- defined as local above to_bits = function (n) - if(n < 0) then + if n < 0 then -- negative return to_bits(bit_not(math.abs(n)) + 1) end @@ -110,61 +113,66 @@ else return tbl end - function bit_or(m, n) - local tbl_m = to_bits(m) - local tbl_n = to_bits(n) + bits_or = function(tbl_m, tbl_n) expand(tbl_m, tbl_n) local tbl = {} for i = 1, #tbl_m do - if(tbl_m[i]== 0 and tbl_n[i] == 0) then + if(tbl_m[i] == 0 and tbl_n[i] == 0) then tbl[i] = 0 else tbl[i] = 1 end end - return tbl2number(tbl) + return tbl end - function bit_and(m, n) - local tbl_m = to_bits(m) - local tbl_n = to_bits(n) + bit_or = function(m, n) + return tbl2number(bits_or(tobits(m), tobits(n))) + end + + bits_and = function(tbl_m, tbl_n) expand(tbl_m, tbl_n) local tbl = {} for i = 1, #tbl_m do - if(tbl_m[i]== 0 or tbl_n[i] == 0) then + if tbl_m[i] == 0 or tbl_n[i] == 0 then tbl[i] = 0 else tbl[i] = 1 end end - return tbl2number(tbl) + return tbl end - function bit_xor(m, n) - local tbl_m = to_bits(m) - local tbl_n = to_bits(n) + bit_and = function(m, n) + return tbl2number(bits_and(tobits(m), tobits(n))) + end + + bits_xor = function(tbl_m, tbl_n) expand(tbl_m, tbl_n) local tbl = {} for i = 1, #tbl_m do - if(tbl_m[i] ~= tbl_n[i]) then + if tbl_m[i] ~= tbl_n[i] then tbl[i] = 1 else tbl[i] = 0 end end - return tbl2number(tbl) + return tbl end - function bit_rshift(n, bits) + bit_xor = function(m, n) + return tbl2number(bits_xor(tobits(m), tobits(n))) + end + + bit_rshift = function(n, bits) local high_bit = 0 - if(n < 0) then - -- negative + if n < 0 then n = bit_not(math.abs(n)) + 1 high_bit = 2147483648 -- 0x80000000 end @@ -178,9 +186,8 @@ else return floor(n) end - function bit_lshift(n, bits) - if(n < 0) then - -- negative + bit_lshift = function(n, bits) + if n < 0 then n = bit_not(math.abs(n)) + 1 end From 55cff74afcd80b87e9bd1474d68502bf40ce51a3 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 6 Apr 2015 18:17:23 +0200 Subject: [PATCH 26/60] typo --- md5.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/md5.lua b/md5.lua index f9a0e9a..1a94db2 100644 --- a/md5.lua +++ b/md5.lua @@ -129,7 +129,7 @@ else end bit_or = function(m, n) - return tbl2number(bits_or(tobits(m), tobits(n))) + return tbl2number(bits_or(to_bits(m), to_bits(n))) end bits_and = function(tbl_m, tbl_n) @@ -148,7 +148,7 @@ else end bit_and = function(m, n) - return tbl2number(bits_and(tobits(m), tobits(n))) + return tbl2number(bits_and(to_bits(m), to_bits(n))) end bits_xor = function(tbl_m, tbl_n) @@ -167,7 +167,7 @@ else end bit_xor = function(m, n) - return tbl2number(bits_xor(tobits(m), tobits(n))) + return tbl2number(bits_xor(to_bits(m), to_bits(n))) end bit_rshift = function(n, bits) From 1212664fb7c372dc1e03e7d347a38b7922ed6111 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 6 Apr 2015 18:21:52 +0200 Subject: [PATCH 27/60] Use hex literals everywhere --- md5.lua | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/md5.lua b/md5.lua index 1a94db2..9d722f5 100644 --- a/md5.lua +++ b/md5.lua @@ -174,7 +174,7 @@ else local high_bit = 0 if n < 0 then n = bit_not(math.abs(n)) + 1 - high_bit = 2147483648 -- 0x80000000 + high_bit = 0x80000000 end local floor = math.floor @@ -194,7 +194,7 @@ else for i=1, bits do n = n*2 end - return bit_and(n, 4294967295) -- 0xFFFFFFFF + return bit_and(n, 0xffffffff) end end @@ -247,7 +247,6 @@ end -- An MD5 mplementation in Lua, requires bitlib (hacked to use LuaBit from above, ugh) -- 10/02/2001 jcw@equi4.com -local FF = 0xffffffff local CONSTS = { 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, @@ -273,9 +272,9 @@ local g=function (x,y,z) return bit_or(bit_and(x,z),bit_and(y,-z-1)) end local h=function (x,y,z) return bit_xor(x,bit_xor(y,z)) end local i=function (x,y,z) return bit_xor(y,bit_or(x,-z-1)) end local z=function (f,a,b,c,d,x,s,ac) - a=bit_and(a+f(b,c,d)+x+ac,FF) + a=bit_and(a+f(b,c,d)+x+ac,0xffffffff) -- be *very* careful that left shift does not cause rounding! - return bit_or(bit_lshift(bit_and(a,bit_rshift(FF,s)),s),bit_rshift(a,32-s))+b + return bit_or(bit_lshift(bit_and(a,bit_rshift(0xffffffff,s)),s),bit_rshift(a,32-s))+b end local function transform(A,B,C,D,X) From f70bf13a41d9e818e21bd39fa4db801750dbae10 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 6 Apr 2015 18:30:24 +0200 Subject: [PATCH 28/60] Revert "Use hex literals everywhere" This reverts commit 1212664fb7c372dc1e03e7d347a38b7922ed6111. --- md5.lua | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/md5.lua b/md5.lua index 9d722f5..1a94db2 100644 --- a/md5.lua +++ b/md5.lua @@ -174,7 +174,7 @@ else local high_bit = 0 if n < 0 then n = bit_not(math.abs(n)) + 1 - high_bit = 0x80000000 + high_bit = 2147483648 -- 0x80000000 end local floor = math.floor @@ -194,7 +194,7 @@ else for i=1, bits do n = n*2 end - return bit_and(n, 0xffffffff) + return bit_and(n, 4294967295) -- 0xFFFFFFFF end end @@ -247,6 +247,7 @@ end -- An MD5 mplementation in Lua, requires bitlib (hacked to use LuaBit from above, ugh) -- 10/02/2001 jcw@equi4.com +local FF = 0xffffffff local CONSTS = { 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, @@ -272,9 +273,9 @@ local g=function (x,y,z) return bit_or(bit_and(x,z),bit_and(y,-z-1)) end local h=function (x,y,z) return bit_xor(x,bit_xor(y,z)) end local i=function (x,y,z) return bit_xor(y,bit_or(x,-z-1)) end local z=function (f,a,b,c,d,x,s,ac) - a=bit_and(a+f(b,c,d)+x+ac,0xffffffff) + a=bit_and(a+f(b,c,d)+x+ac,FF) -- be *very* careful that left shift does not cause rounding! - return bit_or(bit_lshift(bit_and(a,bit_rshift(0xffffffff,s)),s),bit_rshift(a,32-s))+b + return bit_or(bit_lshift(bit_and(a,bit_rshift(FF,s)),s),bit_rshift(a,32-s))+b end local function transform(A,B,C,D,X) From 1e7902f65b999322567bee2b6bc8de38d7502f8e Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 6 Apr 2015 18:30:29 +0200 Subject: [PATCH 29/60] Revert "typo" This reverts commit 55cff74afcd80b87e9bd1474d68502bf40ce51a3. --- md5.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/md5.lua b/md5.lua index 1a94db2..f9a0e9a 100644 --- a/md5.lua +++ b/md5.lua @@ -129,7 +129,7 @@ else end bit_or = function(m, n) - return tbl2number(bits_or(to_bits(m), to_bits(n))) + return tbl2number(bits_or(tobits(m), tobits(n))) end bits_and = function(tbl_m, tbl_n) @@ -148,7 +148,7 @@ else end bit_and = function(m, n) - return tbl2number(bits_and(to_bits(m), to_bits(n))) + return tbl2number(bits_and(tobits(m), tobits(n))) end bits_xor = function(tbl_m, tbl_n) @@ -167,7 +167,7 @@ else end bit_xor = function(m, n) - return tbl2number(bits_xor(to_bits(m), to_bits(n))) + return tbl2number(bits_xor(tobits(m), tobits(n))) end bit_rshift = function(n, bits) From b096b1f3c1a48127dade333e152df9a41b3e08e9 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 6 Apr 2015 18:30:32 +0200 Subject: [PATCH 30/60] Revert "separate lua 5.1 functions into number-based and table-based" This reverts commit 65d03bceefdfdf3b25f769bc772b422041b8005f. --- md5.lua | 63 +++++++++++++++++++++++++-------------------------------- 1 file changed, 28 insertions(+), 35 deletions(-) diff --git a/md5.lua b/md5.lua index f9a0e9a..b99792b 100644 --- a/md5.lua +++ b/md5.lua @@ -75,27 +75,24 @@ else end end - local bits_not, bits_or, bits_and, bits_not, bits_xor local to_bits -- needs to be declared before bit_not - bits_not = function(tbl) - for i=1, math.max(#tbl, 32) do - if tbl[i] == 1 then + function bit_not(n) + local tbl = to_bits(n) + local size = math.max(#tbl, 32) + for i = 1, size do + if(tbl[i] == 1) then tbl[i] = 0 else tbl[i] = 1 end end - return tbl - end - - bit_not = function(n) - return tbl2number(bits_not(to_bits(n))) + return tbl2number(tbl) end -- defined as local above to_bits = function (n) - if n < 0 then + if(n < 0) then -- negative return to_bits(bit_not(math.abs(n)) + 1) end @@ -113,66 +110,61 @@ else return tbl end - bits_or = function(tbl_m, tbl_n) + function bit_or(m, n) + local tbl_m = to_bits(m) + local tbl_n = to_bits(n) expand(tbl_m, tbl_n) local tbl = {} for i = 1, #tbl_m do - if(tbl_m[i] == 0 and tbl_n[i] == 0) then + if(tbl_m[i]== 0 and tbl_n[i] == 0) then tbl[i] = 0 else tbl[i] = 1 end end - return tbl + return tbl2number(tbl) end - bit_or = function(m, n) - return tbl2number(bits_or(tobits(m), tobits(n))) - end - - bits_and = function(tbl_m, tbl_n) + function bit_and(m, n) + local tbl_m = to_bits(m) + local tbl_n = to_bits(n) expand(tbl_m, tbl_n) local tbl = {} for i = 1, #tbl_m do - if tbl_m[i] == 0 or tbl_n[i] == 0 then + if(tbl_m[i]== 0 or tbl_n[i] == 0) then tbl[i] = 0 else tbl[i] = 1 end end - return tbl + return tbl2number(tbl) end - bit_and = function(m, n) - return tbl2number(bits_and(tobits(m), tobits(n))) - end - - bits_xor = function(tbl_m, tbl_n) + function bit_xor(m, n) + local tbl_m = to_bits(m) + local tbl_n = to_bits(n) expand(tbl_m, tbl_n) local tbl = {} for i = 1, #tbl_m do - if tbl_m[i] ~= tbl_n[i] then + if(tbl_m[i] ~= tbl_n[i]) then tbl[i] = 1 else tbl[i] = 0 end end - return tbl + return tbl2number(tbl) end - bit_xor = function(m, n) - return tbl2number(bits_xor(tobits(m), tobits(n))) - end - - bit_rshift = function(n, bits) + function bit_rshift(n, bits) local high_bit = 0 - if n < 0 then + if(n < 0) then + -- negative n = bit_not(math.abs(n)) + 1 high_bit = 2147483648 -- 0x80000000 end @@ -186,8 +178,9 @@ else return floor(n) end - bit_lshift = function(n, bits) - if n < 0 then + function bit_lshift(n, bits) + if(n < 0) then + -- negative n = bit_not(math.abs(n)) + 1 end From 4e68bf98a85b20d9aa71c50a580b24c939af416d Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 6 Apr 2015 18:32:11 +0200 Subject: [PATCH 31/60] do not create global variables in 5.1 --- md5.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/md5.lua b/md5.lua index b99792b..c1a35d9 100644 --- a/md5.lua +++ b/md5.lua @@ -77,7 +77,7 @@ else local to_bits -- needs to be declared before bit_not - function bit_not(n) + bit_not = function(n) local tbl = to_bits(n) local size = math.max(#tbl, 32) for i = 1, size do @@ -110,7 +110,7 @@ else return tbl end - function bit_or(m, n) + bit_or = function(m, n) local tbl_m = to_bits(m) local tbl_n = to_bits(n) expand(tbl_m, tbl_n) @@ -127,7 +127,7 @@ else return tbl2number(tbl) end - function bit_and(m, n) + bit_and = function(m, n) local tbl_m = to_bits(m) local tbl_n = to_bits(n) expand(tbl_m, tbl_n) @@ -144,7 +144,7 @@ else return tbl2number(tbl) end - function bit_xor(m, n) + bit_xor = function(m, n) local tbl_m = to_bits(m) local tbl_n = to_bits(n) expand(tbl_m, tbl_n) @@ -161,7 +161,7 @@ else return tbl2number(tbl) end - function bit_rshift(n, bits) + bit_rshift = function(n, bits) local high_bit = 0 if(n < 0) then -- negative @@ -178,7 +178,7 @@ else return floor(n) end - function bit_lshift(n, bits) + bit_lshift = function(n, bits) if(n < 0) then -- negative n = bit_not(math.abs(n)) + 1 From f253f5ffd5627925937e66f1301246ba46d6d142 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 6 Apr 2015 18:34:11 +0200 Subject: [PATCH 32/60] Use hex literals everywhere --- md5.lua | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/md5.lua b/md5.lua index c1a35d9..a97a41a 100644 --- a/md5.lua +++ b/md5.lua @@ -166,7 +166,7 @@ else if(n < 0) then -- negative n = bit_not(math.abs(n)) + 1 - high_bit = 2147483648 -- 0x80000000 + high_bit = 0x80000000 end local floor = math.floor @@ -187,7 +187,7 @@ else for i=1, bits do n = n*2 end - return bit_and(n, 4294967295) -- 0xFFFFFFFF + return bit_and(n, 0xFFFFFFFF) end end @@ -240,7 +240,6 @@ end -- An MD5 mplementation in Lua, requires bitlib (hacked to use LuaBit from above, ugh) -- 10/02/2001 jcw@equi4.com -local FF = 0xffffffff local CONSTS = { 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, @@ -266,9 +265,9 @@ local g=function (x,y,z) return bit_or(bit_and(x,z),bit_and(y,-z-1)) end local h=function (x,y,z) return bit_xor(x,bit_xor(y,z)) end local i=function (x,y,z) return bit_xor(y,bit_or(x,-z-1)) end local z=function (f,a,b,c,d,x,s,ac) - a=bit_and(a+f(b,c,d)+x+ac,FF) + a=bit_and(a+f(b,c,d)+x+ac,0xFFFFFFFF) -- be *very* careful that left shift does not cause rounding! - return bit_or(bit_lshift(bit_and(a,bit_rshift(FF,s)),s),bit_rshift(a,32-s))+b + return bit_or(bit_lshift(bit_and(a,bit_rshift(0xFFFFFFFF,s)),s),bit_rshift(a,32-s))+b end local function transform(A,B,C,D,X) From 4dfe7a0698d17429cf84c360a8f6a57a83d728dc Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 6 Apr 2015 18:47:51 +0200 Subject: [PATCH 33/60] bump version --- md5.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/md5.lua b/md5.lua index a97a41a..5f75fef 100644 --- a/md5.lua +++ b/md5.lua @@ -1,5 +1,5 @@ local md5 = { - _VERSION = "md5.lua 1.0.0", + _VERSION = "md5.lua 1.0.1", _DESCRIPTION = "MD5 computation in Lua (5.1-3, LuaJIT)", _URL = "https://github.com/kikito/md5.lua", _LICENSE = [[ From 686c18c49e92c2f5924918b618e7bd74b3c72633 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 6 Apr 2015 18:52:54 +0200 Subject: [PATCH 34/60] add rockspec for 1.0.1 --- rockspecs/md5-1.0-1.rockspec | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 rockspecs/md5-1.0-1.rockspec diff --git a/rockspecs/md5-1.0-1.rockspec b/rockspecs/md5-1.0-1.rockspec new file mode 100644 index 0000000..f9f5203 --- /dev/null +++ b/rockspecs/md5-1.0-1.rockspec @@ -0,0 +1,21 @@ +package = "md5" +version = "1.0-1" +source = { + url = "https://github.com/kikito/md5.lua/archive/v1.0.1.tar.gz", + dir = "md5.lua-1.0.1" +} +description = { + summary = "MD5 sum in pure Lua, with no C and no external dependencies", + detailed = "This pure-Lua module computes md5 in Lua 5.1, Lua 5.2 and LuaJIT, using native bit-manipulation libraries when available, and falling back to table-based manipulation of integers in 5.1", + homepage = "https://github.com/kikito/md5.lua", + license = "MIT" +} +dependencies = { + "lua >= 5.1" +} +build = { + type = "builtin", + modules = { + md5 = "md5.lua" + } +} From 9fcb45d9f2d3e51cfad169a6b63ff08ac514717a Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 6 Apr 2015 20:44:11 +0200 Subject: [PATCH 35/60] do not normalize luajit functions --- md5.lua | 288 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 146 insertions(+), 142 deletions(-) diff --git a/md5.lua b/md5.lua index 5f75fef..9a6381d 100644 --- a/md5.lua +++ b/md5.lua @@ -35,177 +35,181 @@ local char, byte, format, rep, sub = local bit_or, bit_and, bit_not, bit_xor, bit_rshift, bit_lshift local ok, bit = pcall(require, 'bit') -if not ok then ok, bit = pcall(require, 'bit32') end - if ok then - - bit_not = bit.bnot - - local tobit = function(n) - return n <= 0x7fffffff and n or -(bit_not(n) + 1) - end - - local normalize = function(f) - return function(a,b) return tobit(f(tobit(a), tobit(b))) end - end - - bit_or, bit_and, bit_xor = normalize(bit.bor), normalize(bit.band), normalize(bit.bxor) - bit_rshift, bit_lshift = normalize(bit.rshift), normalize(bit.lshift) - + bit_or, bit_and, bit_not, bit_xor, bit_rshift, bit_lshift = bit.bor, bit.band, bit.bnot, bit.xor, bit.rshift, bit.lshift else + ok, bit = pcall(require, 'bit32') - local function tbl2number(tbl) - local result = 0 - local power = 1 - for i = 1, #tbl do - result = result + tbl[i] * power - power = power * 2 + if ok then + + bit_not = bit.bnot + + local tobit = function(n) + return n <= 0x7fffffff and n or -(bit_not(n) + 1) end - return result - end - local function expand(t1, t2) - local big, small = t1, t2 - if(#big < #small) then - big, small = small, big + local normalize = function(f) + return function(a,b) return tobit(f(tobit(a), tobit(b))) end end - -- expand small - for i = #small + 1, #big do - small[i] = 0 - end - end - local to_bits -- needs to be declared before bit_not + bit_or, bit_and, bit_xor = normalize(bit.bor), normalize(bit.band), normalize(bit.bxor) + bit_rshift, bit_lshift = normalize(bit.rshift), normalize(bit.lshift) - bit_not = function(n) - local tbl = to_bits(n) - local size = math.max(#tbl, 32) - for i = 1, size do - if(tbl[i] == 1) then - tbl[i] = 0 - else - tbl[i] = 1 + else + + local function tbl2number(tbl) + local result = 0 + local power = 1 + for i = 1, #tbl do + result = result + tbl[i] * power + power = power * 2 end - end - return tbl2number(tbl) - end - - -- defined as local above - to_bits = function (n) - if(n < 0) then - -- negative - return to_bits(bit_not(math.abs(n)) + 1) - end - -- to bits table - local tbl = {} - local cnt = 1 - local last - while n > 0 do - last = n % 2 - tbl[cnt] = last - n = (n-last)/2 - cnt = cnt + 1 + return result end - return tbl - end - - bit_or = function(m, n) - local tbl_m = to_bits(m) - local tbl_n = to_bits(n) - expand(tbl_m, tbl_n) - - local tbl = {} - for i = 1, #tbl_m do - if(tbl_m[i]== 0 and tbl_n[i] == 0) then - tbl[i] = 0 - else - tbl[i] = 1 + local function expand(t1, t2) + local big, small = t1, t2 + if(#big < #small) then + big, small = small, big + end + -- expand small + for i = #small + 1, #big do + small[i] = 0 end end - return tbl2number(tbl) - end + local to_bits -- needs to be declared before bit_not - bit_and = function(m, n) - local tbl_m = to_bits(m) - local tbl_n = to_bits(n) - expand(tbl_m, tbl_n) - - local tbl = {} - for i = 1, #tbl_m do - if(tbl_m[i]== 0 or tbl_n[i] == 0) then - tbl[i] = 0 - else - tbl[i] = 1 + bit_not = function(n) + local tbl = to_bits(n) + local size = math.max(#tbl, 32) + for i = 1, size do + if(tbl[i] == 1) then + tbl[i] = 0 + else + tbl[i] = 1 + end end + return tbl2number(tbl) end - return tbl2number(tbl) - end - - bit_xor = function(m, n) - local tbl_m = to_bits(m) - local tbl_n = to_bits(n) - expand(tbl_m, tbl_n) - - local tbl = {} - for i = 1, #tbl_m do - if(tbl_m[i] ~= tbl_n[i]) then - tbl[i] = 1 - else - tbl[i] = 0 + -- defined as local above + to_bits = function (n) + if(n < 0) then + -- negative + return to_bits(bit_not(math.abs(n)) + 1) end + -- to bits table + local tbl = {} + local cnt = 1 + local last + while n > 0 do + last = n % 2 + tbl[cnt] = last + n = (n-last)/2 + cnt = cnt + 1 + end + + return tbl end - return tbl2number(tbl) + bit_or = function(m, n) + local tbl_m = to_bits(m) + local tbl_n = to_bits(n) + expand(tbl_m, tbl_n) + + local tbl = {} + for i = 1, #tbl_m do + if(tbl_m[i]== 0 and tbl_n[i] == 0) then + tbl[i] = 0 + else + tbl[i] = 1 + end + end + + return tbl2number(tbl) + end + + bit_and = function(m, n) + local tbl_m = to_bits(m) + local tbl_n = to_bits(n) + expand(tbl_m, tbl_n) + + local tbl = {} + for i = 1, #tbl_m do + if(tbl_m[i]== 0 or tbl_n[i] == 0) then + tbl[i] = 0 + else + tbl[i] = 1 + end + end + + return tbl2number(tbl) + end + + bit_xor = function(m, n) + local tbl_m = to_bits(m) + local tbl_n = to_bits(n) + expand(tbl_m, tbl_n) + + local tbl = {} + for i = 1, #tbl_m do + if(tbl_m[i] ~= tbl_n[i]) then + tbl[i] = 1 + else + tbl[i] = 0 + end + end + + return tbl2number(tbl) + end + + bit_rshift = function(n, bits) + local high_bit = 0 + if(n < 0) then + -- negative + n = bit_not(math.abs(n)) + 1 + high_bit = 0x80000000 + end + + local floor = math.floor + + for i=1, bits do + n = n/2 + n = bit_or(floor(n), high_bit) + end + return floor(n) + end + + bit_lshift = function(n, bits) + if(n < 0) then + -- negative + n = bit_not(math.abs(n)) + 1 + end + + for i=1, bits do + n = n*2 + end + return bit_and(n, 0xFFFFFFFF) + end end - bit_rshift = function(n, bits) - local high_bit = 0 - if(n < 0) then - -- negative - n = bit_not(math.abs(n)) + 1 - high_bit = 0x80000000 - end - - local floor = math.floor - - for i=1, bits do - n = n/2 - n = bit_or(floor(n), high_bit) - end - return floor(n) + -- convert little-endian 32-bit int to a 4-char string + local function lei2str(i) + local f=function (s) return char( bit_and( bit_rshift(i, s), 255)) end + return f(0)..f(8)..f(16)..f(24) end - bit_lshift = function(n, bits) - if(n < 0) then - -- negative - n = bit_not(math.abs(n)) + 1 + -- convert raw string to big-endian int + local function str2bei(s) + local v=0 + for i=1, #s do + v = v * 256 + byte(s, i) end - - for i=1, bits do - n = n*2 - end - return bit_and(n, 0xFFFFFFFF) + return v end end --- convert little-endian 32-bit int to a 4-char string -local function lei2str(i) - local f=function (s) return char( bit_and( bit_rshift(i, s), 255)) end - return f(0)..f(8)..f(16)..f(24) -end - --- convert raw string to big-endian int -local function str2bei(s) - local v=0 - for i=1, #s do - v = v * 256 + byte(s, i) - end - return v -end - -- convert raw string to little-endian int local function str2lei(s) local v=0 From d3c97f950f716f670660b5fcfc7eb02ddd1d62c3 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 6 Apr 2015 20:54:06 +0200 Subject: [PATCH 36/60] fix error when adding if --- md5.lua | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/md5.lua b/md5.lua index 9a6381d..1360889 100644 --- a/md5.lua +++ b/md5.lua @@ -193,21 +193,21 @@ else return bit_and(n, 0xFFFFFFFF) end end +end - -- convert little-endian 32-bit int to a 4-char string - local function lei2str(i) - local f=function (s) return char( bit_and( bit_rshift(i, s), 255)) end - return f(0)..f(8)..f(16)..f(24) - end +-- convert little-endian 32-bit int to a 4-char string +local function lei2str(i) + local f=function (s) return char( bit_and( bit_rshift(i, s), 255)) end + return f(0)..f(8)..f(16)..f(24) +end - -- convert raw string to big-endian int - local function str2bei(s) - local v=0 - for i=1, #s do - v = v * 256 + byte(s, i) - end - return v +-- convert raw string to big-endian int +local function str2bei(s) + local v=0 + for i=1, #s do + v = v * 256 + byte(s, i) end + return v end -- convert raw string to little-endian int From bd1dd4bd0f789a621dae9c43574be0a18ee5b536 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 6 Apr 2015 20:57:59 +0200 Subject: [PATCH 37/60] fix typo in LuaJIT --- md5.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/md5.lua b/md5.lua index 1360889..3c1f6aa 100644 --- a/md5.lua +++ b/md5.lua @@ -36,7 +36,7 @@ local bit_or, bit_and, bit_not, bit_xor, bit_rshift, bit_lshift local ok, bit = pcall(require, 'bit') if ok then - bit_or, bit_and, bit_not, bit_xor, bit_rshift, bit_lshift = bit.bor, bit.band, bit.bnot, bit.xor, bit.rshift, bit.lshift + bit_or, bit_and, bit_not, bit_xor, bit_rshift, bit_lshift = bit.bor, bit.band, bit.bnot, bit.bxor, bit.rshift, bit.lshift else ok, bit = pcall(require, 'bit32') From 42b3d5ae5abc3bd57af6ff776a89f2f07d4ea796 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 6 Apr 2015 21:11:54 +0200 Subject: [PATCH 38/60] bump version --- md5.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/md5.lua b/md5.lua index 3c1f6aa..4a9e721 100644 --- a/md5.lua +++ b/md5.lua @@ -1,5 +1,5 @@ local md5 = { - _VERSION = "md5.lua 1.0.1", + _VERSION = "md5.lua 1.0.2", _DESCRIPTION = "MD5 computation in Lua (5.1-3, LuaJIT)", _URL = "https://github.com/kikito/md5.lua", _LICENSE = [[ From 7fc429b519a5ae6b5edcd64f028035f72afb5ba9 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 6 Apr 2015 21:13:45 +0200 Subject: [PATCH 39/60] add rockspec --- rockspecs/md5-1.0-2.rockspec | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 rockspecs/md5-1.0-2.rockspec diff --git a/rockspecs/md5-1.0-2.rockspec b/rockspecs/md5-1.0-2.rockspec new file mode 100644 index 0000000..c8034f9 --- /dev/null +++ b/rockspecs/md5-1.0-2.rockspec @@ -0,0 +1,21 @@ +package = "md5" +version = "1.0-2" +source = { + url = "https://github.com/kikito/md5.lua/archive/v1.0.2.tar.gz", + dir = "md5.lua-1.0.2" +} +description = { + summary = "MD5 sum in pure Lua, with no C and no external dependencies", + detailed = "This pure-Lua module computes md5 in Lua 5.1, Lua 5.2 and LuaJIT, using native bit-manipulation libraries when available, and falling back to table-based manipulation of integers in 5.1", + homepage = "https://github.com/kikito/md5.lua", + license = "MIT" +} +dependencies = { + "lua >= 5.1" +} +build = { + type = "builtin", + modules = { + md5 = "md5.lua" + } +} From 467f30efb648eaf23bb604f00a087bbfb93254b5 Mon Sep 17 00:00:00 2001 From: Pedro Gimeno Date: Sat, 2 Jul 2016 10:42:54 +0200 Subject: [PATCH 40/60] Add large strings test to demonstrate failure in current version. --- spec/md5_spec.lua | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/spec/md5_spec.lua b/spec/md5_spec.lua index ed00a0e..929770d 100644 --- a/spec/md5_spec.lua +++ b/spec/md5_spec.lua @@ -15,16 +15,20 @@ describe('md5', function() assert.equal(md5.sumhexa('The quick brown fox jumps over the lazy dog.'), 'e4d909c290d0fb1ca068ffaddf22cbd0') assert.equal(md5.sumhexa(''), 'd41d8cd98f00b204e9800998ecf8427e') assert.equal(md5.sumhexa(('1'):rep(824)), 'a126fd3611ab8d9b7e8a3384e2fa78a0') + assert.equal(md5.sumhexa(('1'):rep(1528)), '3750b6a29d923b633e05d6ae76895664') + assert.equal(md5.sumhexa(('1'):rep(99999)), '3b527ec3aa350362ab2eca64c94cfc6d') end) end) describe('md5.sum', function() it('works', function() assert.equal(md5.sum("asdf"), hex2bin '912ec803b2ce49e4a541068d495ab570') - assert.equal(md5.sum('The quick brown fox jumps over the lazy dog'), hex2bin'9e107d9d372bb6826bd81d3542a419d6') - assert.equal(md5.sum('The quick brown fox jumps over the lazy dog.'), hex2bin'e4d909c290d0fb1ca068ffaddf22cbd0') - assert.equal(md5.sum(''), hex2bin'd41d8cd98f00b204e9800998ecf8427e') - assert.equal(md5.sum(('1'):rep(824)), hex2bin'a126fd3611ab8d9b7e8a3384e2fa78a0') + assert.equal(md5.sum('The quick brown fox jumps over the lazy dog'), hex2bin '9e107d9d372bb6826bd81d3542a419d6') + assert.equal(md5.sum('The quick brown fox jumps over the lazy dog.'), hex2bin 'e4d909c290d0fb1ca068ffaddf22cbd0') + assert.equal(md5.sum(''), hex2bin 'd41d8cd98f00b204e9800998ecf8427e') + assert.equal(md5.sum(('1'):rep(824)), hex2bin 'a126fd3611ab8d9b7e8a3384e2fa78a0') + assert.equal(md5.sum(('1'):rep(1528)), hex2bin '3750b6a29d923b633e05d6ae76895664') + assert.equal(md5.sum(('1'):rep(99999)), hex2bin '3b527ec3aa350362ab2eca64c94cfc6d') end) end) end) From dd10f1c9defd3d51cbda70a174145fd2ce66732f Mon Sep 17 00:00:00 2001 From: Pedro Gimeno Date: Sat, 2 Jul 2016 10:44:35 +0200 Subject: [PATCH 41/60] Fix #4 by limiting the sum output to 32 bits. --- md5.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/md5.lua b/md5.lua index 4a9e721..8ae63a1 100644 --- a/md5.lua +++ b/md5.lua @@ -346,7 +346,8 @@ local function transform(A,B,C,D,X) c=z(i,c,d,a,b,X[ 2],15,t[63]) b=z(i,b,c,d,a,X[ 9],21,t[64]) - return A+a,B+b,C+c,D+d + return bit_and(A+a,0xFFFFFFFF),bit_and(B+b,0xFFFFFFFF), + bit_and(C+c,0xFFFFFFFF),bit_and(D+d,0xFFFFFFFF) end ---------------------------------------------------------------- From 1f6eba9a7bd8360b49f78cca4326116a5203b9e9 Mon Sep 17 00:00:00 2001 From: kikito Date: Sat, 2 Jul 2016 18:03:28 +0200 Subject: [PATCH 42/60] updates travis --- .travis.yml | 48 +++++++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/.travis.yml b/.travis.yml index 06fbe8a..53998d6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,36 +1,34 @@ -language: erlang +language: python +sudo: false env: - global: - - LUAROCKS_BASE=luarocks-2.1.0 - matrix: - - LUA=lua5.1 LUA_DEV=liblua5.1-dev LUA_VER=5.1 LUA_SFX=5.1 LUA_INCDIR=/usr/include/lua5.1 - - LUA=lua5.2 LUA_DEV=liblua5.2-dev LUA_VER=5.2 LUA_SFX=5.2 LUA_INCDIR=/usr/include/lua5.2 - - LUA=luajit LUA_DEV=libluajit-5.1-dev LUA_VER=5.1 LUA_SFX=jit LUA_INCDIR=/usr/include/luajit-2.0 + - LUA="lua=5.1" + - LUA="lua=5.2" + - LUA="lua=5.3" + - LUA="luajit=2.0" + - LUA="luajit=2.1" before_install: - - if [ $LUA = "luajit" ]; then - sudo add-apt-repository ppa:mwild1/ppa -y && sudo apt-get update -y; - fi - - sudo apt-get install $LUA - - sudo apt-get install $LUA_DEV - - lua$LUA_SFX -v - # Install a recent luarocks release - - wget http://luarocks.org/releases/$LUAROCKS_BASE.tar.gz - - tar zxvpf $LUAROCKS_BASE.tar.gz - - cd $LUAROCKS_BASE - - ./configure - --lua-version=$LUA_VER --lua-suffix=$LUA_SFX --with-lua-include="$LUA_INCDIR" - - sudo make - - sudo make install - - cd $TRAVIS_BUILD_DIR + - pip install hererocks + - hererocks lua_install -r^ --$LUA + - export PATH=$PATH:$PWD/lua_install/bin # Add directory with all installed binaries to PATH install: - - sudo -E luarocks install lua_cliargs 2.3-3 - - sudo -E luarocks install busted 1.11.1-1 + - luarocks install luacheck + - luarocks install busted + - luarocks install luacov + - luarocks install luacov-coveralls script: - - sudo -E busted -v + - luacheck --no-unused-args --std max+busted *.lua spec + - busted --verbose --coverage + +after_success: + - luacov-coveralls --exclude $TRAVIS_BUILD_DIR/lua_install + +branches: + except: + - gh-pages notifications: email: From 51347acd887727fbc1212c83d30177ef9b07277b Mon Sep 17 00:00:00 2001 From: kikito Date: Sat, 2 Jul 2016 18:03:49 +0200 Subject: [PATCH 43/60] renames variables to avoid luacheck errors --- md5.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/md5.lua b/md5.lua index 4a9e721..f5d6402 100644 --- a/md5.lua +++ b/md5.lua @@ -268,8 +268,8 @@ local f=function (x,y,z) return bit_or(bit_and(x,y),bit_and(-x-1,z)) end local g=function (x,y,z) return bit_or(bit_and(x,z),bit_and(y,-z-1)) end local h=function (x,y,z) return bit_xor(x,bit_xor(y,z)) end local i=function (x,y,z) return bit_xor(y,bit_or(x,-z-1)) end -local z=function (f,a,b,c,d,x,s,ac) - a=bit_and(a+f(b,c,d)+x+ac,0xFFFFFFFF) +local z=function (ff,a,b,c,d,x,s,ac) + a=bit_and(a+ff(b,c,d)+x+ac,0xFFFFFFFF) -- be *very* careful that left shift does not cause rounding! return bit_or(bit_lshift(bit_and(a,bit_rshift(0xFFFFFFFF,s)),s),bit_rshift(a,32-s))+b end @@ -366,8 +366,8 @@ function md5.sumhexa(s) local t = CONSTS local a,b,c,d = t[65],t[66],t[67],t[68] - for i=1,#s,64 do - local X = cut_le_str(sub(s,i,i+63),4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4) + for ii=1,#s,64 do + local X = cut_le_str(sub(s,ii,ii+63),4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4) assert(#X == 16) X[0] = table.remove(X,1) -- zero based! a,b,c,d = transform(a,b,c,d,X) From 7e5623a206631183de8d99bb617d34c8cf26c9b0 Mon Sep 17 00:00:00 2001 From: kikito Date: Sat, 2 Jul 2016 18:22:53 +0200 Subject: [PATCH 44/60] luacheck install fix --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 53998d6..15b3875 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,7 +14,7 @@ before_install: - export PATH=$PATH:$PWD/lua_install/bin # Add directory with all installed binaries to PATH install: - - luarocks install luacheck + - luarocks install luacheck --deps-mode=none - luarocks install busted - luarocks install luacov - luarocks install luacov-coveralls From 8603af5e23497b97105c6d4d65f3c4c7b8fb8d81 Mon Sep 17 00:00:00 2001 From: kikito Date: Sat, 2 Jul 2016 18:32:44 +0200 Subject: [PATCH 45/60] delete long example which is too slow in lua 5.1 --- spec/md5_spec.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/spec/md5_spec.lua b/spec/md5_spec.lua index 929770d..8425c95 100644 --- a/spec/md5_spec.lua +++ b/spec/md5_spec.lua @@ -16,7 +16,6 @@ describe('md5', function() assert.equal(md5.sumhexa(''), 'd41d8cd98f00b204e9800998ecf8427e') assert.equal(md5.sumhexa(('1'):rep(824)), 'a126fd3611ab8d9b7e8a3384e2fa78a0') assert.equal(md5.sumhexa(('1'):rep(1528)), '3750b6a29d923b633e05d6ae76895664') - assert.equal(md5.sumhexa(('1'):rep(99999)), '3b527ec3aa350362ab2eca64c94cfc6d') end) end) @@ -28,7 +27,6 @@ describe('md5', function() assert.equal(md5.sum(''), hex2bin 'd41d8cd98f00b204e9800998ecf8427e') assert.equal(md5.sum(('1'):rep(824)), hex2bin 'a126fd3611ab8d9b7e8a3384e2fa78a0') assert.equal(md5.sum(('1'):rep(1528)), hex2bin '3750b6a29d923b633e05d6ae76895664') - assert.equal(md5.sum(('1'):rep(99999)), hex2bin '3b527ec3aa350362ab2eca64c94cfc6d') end) end) end) From 5297100c7cbd7b6609c487750e44fcf8707e99cc Mon Sep 17 00:00:00 2001 From: kikito Date: Sat, 2 Jul 2016 19:08:52 +0200 Subject: [PATCH 46/60] luacheck install fix remove luacheck --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 15b3875..90bd434 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,13 +14,11 @@ before_install: - export PATH=$PATH:$PWD/lua_install/bin # Add directory with all installed binaries to PATH install: - - luarocks install luacheck --deps-mode=none - luarocks install busted - luarocks install luacov - luarocks install luacov-coveralls script: - - luacheck --no-unused-args --std max+busted *.lua spec - busted --verbose --coverage after_success: From 51febea6c3b617ff80f8bbd4530cfc1998a50dbb Mon Sep 17 00:00:00 2001 From: Pedro Gimeno Date: Sat, 2 Jul 2016 17:31:10 +0200 Subject: [PATCH 47/60] Implement incremental MD5 Two new functions: md5.new(): returns a new MD5 state object with two methods: - MD5State:update(s) - adds the string s to the calculation - MD5State:finish() - returns the final MD5 as a binary string md5.tohex(): converts to hexadecimal the binary string returned by md5.sum() and MD5State:finish() --- md5.lua | 59 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/md5.lua b/md5.lua index 68de32a..4828b53 100644 --- a/md5.lua +++ b/md5.lua @@ -232,15 +232,6 @@ end local swap = function (w) return str2bei(lei2str(w)) end -local function hex2binaryaux(hexval) - return char(tonumber(hexval, 16)) -end - -local function hex2binary(hex) - local result, _ = hex:gsub('..', hex2binaryaux) - return result -end - -- An MD5 mplementation in Lua, requires bitlib (hacked to use LuaBit from above, ugh) -- 10/02/2001 jcw@equi4.com @@ -352,33 +343,53 @@ end ---------------------------------------------------------------- -function md5.sumhexa(s) - local msgLen = #s +local function md5_update(self, s) + self.pos = self.pos + #s + s = self.buf .. s + for ii = 1, #s - 63, 64 do + local X = cut_le_str(sub(s,ii,ii+63),4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4) + assert(#X == 16) + X[0] = table.remove(X,1) -- zero based! + self.a,self.b,self.c,self.d = transform(self.a,self.b,self.c,self.d,X) + end + self.buf = sub(s, math.floor(#s/64)*64 + 1, #s) +end + +local function md5_finish(self) + local msgLen = self.pos local padLen = 56 - msgLen % 64 if msgLen % 64 > 56 then padLen = padLen + 64 end if padLen == 0 then padLen = 64 end - s = s .. char(128) .. rep(char(0),padLen-1) .. lei2str(8*msgLen) .. lei2str(0) + local s = char(128) .. rep(char(0),padLen-1) .. lei2str(bit_and(8*msgLen, 0xFFFFFFFF)) .. lei2str(math.floor(msgLen/0x20000000)) + md5_update(self, s) - assert(#s % 64 == 0) + assert(self.pos % 64 == 0) + return lei2str(self.a) .. lei2str(self.b) .. lei2str(self.c) .. lei2str(self.d) +end - local t = CONSTS - local a,b,c,d = t[65],t[66],t[67],t[68] +function md5.new() + return { a = CONSTS[65], b = CONSTS[66], c = CONSTS[67], d = CONSTS[68], + pos = 0, + buf = '', + update = md5_update, + finish = md5_finish } +end - for ii=1,#s,64 do - local X = cut_le_str(sub(s,ii,ii+63),4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4) - assert(#X == 16) - X[0] = table.remove(X,1) -- zero based! - a,b,c,d = transform(a,b,c,d,X) - end - - return format("%08x%08x%08x%08x",swap(a),swap(b),swap(c),swap(d)) +function md5.tohex(s) + return format("%08x%08x%08x%08x", str2bei(sub(s, 1, 4)), str2bei(sub(s, 5, 8)), str2bei(sub(s, 9, 12)), str2bei(sub(s, 13, 16))) end function md5.sum(s) - return hex2binary(md5.sumhexa(s)) + local state = md5.new() + state:update(s) + return state:finish() +end + +function md5.sumhexa(s) + return md5.tohex(md5.sum(s)) end return md5 From 46b50003f811a70fea82997dd1aa25675225cf47 Mon Sep 17 00:00:00 2001 From: Pedro Gimeno Date: Sat, 2 Jul 2016 17:49:35 +0200 Subject: [PATCH 48/60] Add a simple unit test for the incremental version --- spec/md5_spec.lua | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/md5_spec.lua b/spec/md5_spec.lua index 8425c95..69dde39 100644 --- a/spec/md5_spec.lua +++ b/spec/md5_spec.lua @@ -16,6 +16,10 @@ describe('md5', function() assert.equal(md5.sumhexa(''), 'd41d8cd98f00b204e9800998ecf8427e') assert.equal(md5.sumhexa(('1'):rep(824)), 'a126fd3611ab8d9b7e8a3384e2fa78a0') assert.equal(md5.sumhexa(('1'):rep(1528)), '3750b6a29d923b633e05d6ae76895664') + local state = md5.new() + state:update('Hello') + state:update(', World!') + assert.equal(md5.tohex(state:finish()), '65a8e27d8879283831b664bd8b7f0ad4') end) end) @@ -27,6 +31,10 @@ describe('md5', function() assert.equal(md5.sum(''), hex2bin 'd41d8cd98f00b204e9800998ecf8427e') assert.equal(md5.sum(('1'):rep(824)), hex2bin 'a126fd3611ab8d9b7e8a3384e2fa78a0') assert.equal(md5.sum(('1'):rep(1528)), hex2bin '3750b6a29d923b633e05d6ae76895664') + local state = md5.new() + state:update('Hello') + state:update(', World!') + assert.equal(state:finish(), hex2bin '65a8e27d8879283831b664bd8b7f0ad4') end) end) end) From 86da43dbda40354830d629168c24aade004af2a9 Mon Sep 17 00:00:00 2001 From: kikito Date: Fri, 15 Jul 2016 01:16:54 +0200 Subject: [PATCH 49/60] makes update return self --- md5.lua | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/md5.lua b/md5.lua index 4828b53..93ae756 100644 --- a/md5.lua +++ b/md5.lua @@ -353,6 +353,7 @@ local function md5_update(self, s) self.a,self.b,self.c,self.d = transform(self.a,self.b,self.c,self.d,X) end self.buf = sub(s, math.floor(#s/64)*64 + 1, #s) + return self end local function md5_finish(self) @@ -370,6 +371,8 @@ local function md5_finish(self) return lei2str(self.a) .. lei2str(self.b) .. lei2str(self.c) .. lei2str(self.d) end +---------------------------------------------------------------- + function md5.new() return { a = CONSTS[65], b = CONSTS[66], c = CONSTS[67], d = CONSTS[68], pos = 0, @@ -383,9 +386,7 @@ function md5.tohex(s) end function md5.sum(s) - local state = md5.new() - state:update(s) - return state:finish() + return md5.new():update(s):finish() end function md5.sumhexa(s) From 58bfa546fe7c225028866366fb1a827c8308e0c2 Mon Sep 17 00:00:00 2001 From: kikito Date: Fri, 15 Jul 2016 01:21:56 +0200 Subject: [PATCH 50/60] bump version to 1.1.0. Add changelog --- CHANGELOG.md | 5 +++++ md5.lua | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..a48c47a --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ + +# 1.1.0 + +* Fixes error with long strings in Lua 5.1 (@pgimeno) +* Adds incremental mode (@pgimeno) diff --git a/md5.lua b/md5.lua index 93ae756..5e73ba6 100644 --- a/md5.lua +++ b/md5.lua @@ -1,5 +1,5 @@ local md5 = { - _VERSION = "md5.lua 1.0.2", + _VERSION = "md5.lua 1.1.0", _DESCRIPTION = "MD5 computation in Lua (5.1-3, LuaJIT)", _URL = "https://github.com/kikito/md5.lua", _LICENSE = [[ From 520f90795b08364f9151fa1b3cdbb72fa9f8731c Mon Sep 17 00:00:00 2001 From: kikito Date: Fri, 15 Jul 2016 01:29:04 +0200 Subject: [PATCH 51/60] adds description of iterative version to README --- README.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 38fd023..391b441 100644 --- a/README.md +++ b/README.md @@ -8,10 +8,21 @@ It implements md5.sum and md5.sumhex like the [kernel project md5 package](http: Usage ===== +Simple example: + local md5 = require 'md5' + local md5_as_data = md5.sum(message) -- returns raw bytes local md5_as_hex = md5.sumhexa(message) -- returns a hex string - local md5_as_data = md5.sum(message) -- returns raw bytes + local md5_as_hex2 = md5.tohex(md5_as_data) -- returns the same string as md5_as_hex + +Incremental example (for computing md5 of streams, or big files which have to be loaded in chunks - new since 1.1.0): + + local m = md5.new() + m:update('some bytes') + m:update('some more bytes') + m:update('etc') + return md5.tohex(m:finish()) Credits ======= @@ -23,6 +34,8 @@ and http://equi4.com/md5/md5calc.lua, by Equi 4 Software. Lua 5.2 and LuaJIT compatibility by [Positive07](https://github.com/kikito/md5.lua/pull/2) +A very important fix and the incremental variant by [pgimeno](https://github.com/kikito/md5.lua/pull/10) + License ======= From e8cd75ce397da6dd75ddb37cb07eb31663743223 Mon Sep 17 00:00:00 2001 From: kikito Date: Fri, 15 Jul 2016 01:30:59 +0200 Subject: [PATCH 52/60] add 1.1.0 rockspec --- rockspecs/md5-1.1-0.rockspec | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 rockspecs/md5-1.1-0.rockspec diff --git a/rockspecs/md5-1.1-0.rockspec b/rockspecs/md5-1.1-0.rockspec new file mode 100644 index 0000000..ef1b392 --- /dev/null +++ b/rockspecs/md5-1.1-0.rockspec @@ -0,0 +1,21 @@ +package = "md5" +version = "1.1-0" +source = { + url = "https://github.com/kikito/md5.lua/archive/v1.1.0.tar.gz", + dir = "md5.lua-1.1.0" +} +description = { + summary = "MD5 sum in pure Lua, with no C and no external dependencies", + detailed = "This pure-Lua module computes md5 in Lua 5.1, Lua 5.2 and LuaJIT, using native bit-manipulation libraries when available, and falling back to table-based manipulation of integers in 5.1", + homepage = "https://github.com/kikito/md5.lua", + license = "MIT" +} +dependencies = { + "lua >= 5.1" +} +build = { + type = "builtin", + modules = { + md5 = "md5.lua" + } +} From 3e327c01723b6d3cb992de941f9792623921439e Mon Sep 17 00:00:00 2001 From: AntumDeluge Date: Fri, 12 May 2017 15:30:10 -0700 Subject: [PATCH 53/60] Ignore Eclipse project files & directories --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 8b38e58..c8d945c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ ## Generic ignorable patterns and files *~ debug.txt + +## Eclipse project files & directories +.project +.settings From 17ddb23406e5770e74b1846eaf126ecbb91752c0 Mon Sep 17 00:00:00 2001 From: AntumDeluge Date: Fri, 12 May 2017 15:42:39 -0700 Subject: [PATCH 54/60] Add cornernote's 'craft_guide' mod as optional dependency: Ensures that craft recipes are registered. Added to the following mods with craft recipes: - adjustable_blinkyplant - adjustable_player_detector - commandblock - conductor_signalchanger - dual_delayer - entity_detector - igniter - injector_controller - jammer - playerkiller - sayer - signalchanger - switchtorch - teleporter - timegate - wireless --- moremesecons_adjustable_blinkyplant/depends.txt | 1 + moremesecons_adjustable_player_detector/depends.txt | 1 + moremesecons_commandblock/depends.txt | 1 + moremesecons_conductor_signalchanger/depends.txt | 1 + moremesecons_dual_delayer/depends.txt | 1 + moremesecons_entity_detector/depends.txt | 1 + moremesecons_igniter/depends.txt | 1 + moremesecons_injector_controller/depends.txt | 1 + moremesecons_jammer/depends.txt | 1 + moremesecons_playerkiller/depends.txt | 1 + moremesecons_sayer/depends.txt | 1 + moremesecons_signalchanger/depends.txt | 1 + moremesecons_switchtorch/depends.txt | 3 ++- moremesecons_teleporter/depends.txt | 1 + moremesecons_timegate/depends.txt | 1 + moremesecons_wireless/depends.txt | 1 + 16 files changed, 17 insertions(+), 1 deletion(-) diff --git a/moremesecons_adjustable_blinkyplant/depends.txt b/moremesecons_adjustable_blinkyplant/depends.txt index acaa924..0ddb4fb 100644 --- a/moremesecons_adjustable_blinkyplant/depends.txt +++ b/moremesecons_adjustable_blinkyplant/depends.txt @@ -1 +1,2 @@ mesecons +craft_guide? diff --git a/moremesecons_adjustable_player_detector/depends.txt b/moremesecons_adjustable_player_detector/depends.txt index acaa924..0ddb4fb 100644 --- a/moremesecons_adjustable_player_detector/depends.txt +++ b/moremesecons_adjustable_player_detector/depends.txt @@ -1 +1,2 @@ mesecons +craft_guide? diff --git a/moremesecons_commandblock/depends.txt b/moremesecons_commandblock/depends.txt index a259a42..5a7e5e6 100644 --- a/moremesecons_commandblock/depends.txt +++ b/moremesecons_commandblock/depends.txt @@ -1,2 +1,3 @@ mesecons moremesecons_utils +craft_guide? diff --git a/moremesecons_conductor_signalchanger/depends.txt b/moremesecons_conductor_signalchanger/depends.txt index acaa924..0ddb4fb 100644 --- a/moremesecons_conductor_signalchanger/depends.txt +++ b/moremesecons_conductor_signalchanger/depends.txt @@ -1 +1,2 @@ mesecons +craft_guide? diff --git a/moremesecons_dual_delayer/depends.txt b/moremesecons_dual_delayer/depends.txt index acaa924..0ddb4fb 100644 --- a/moremesecons_dual_delayer/depends.txt +++ b/moremesecons_dual_delayer/depends.txt @@ -1 +1,2 @@ mesecons +craft_guide? diff --git a/moremesecons_entity_detector/depends.txt b/moremesecons_entity_detector/depends.txt index acaa924..0ddb4fb 100644 --- a/moremesecons_entity_detector/depends.txt +++ b/moremesecons_entity_detector/depends.txt @@ -1 +1,2 @@ mesecons +craft_guide? diff --git a/moremesecons_igniter/depends.txt b/moremesecons_igniter/depends.txt index f95ba13..b684a5c 100644 --- a/moremesecons_igniter/depends.txt +++ b/moremesecons_igniter/depends.txt @@ -1,2 +1,3 @@ mesecons fire +craft_guide? diff --git a/moremesecons_injector_controller/depends.txt b/moremesecons_injector_controller/depends.txt index acaa924..0ddb4fb 100644 --- a/moremesecons_injector_controller/depends.txt +++ b/moremesecons_injector_controller/depends.txt @@ -1 +1,2 @@ mesecons +craft_guide? diff --git a/moremesecons_jammer/depends.txt b/moremesecons_jammer/depends.txt index a259a42..5a7e5e6 100644 --- a/moremesecons_jammer/depends.txt +++ b/moremesecons_jammer/depends.txt @@ -1,2 +1,3 @@ mesecons moremesecons_utils +craft_guide? diff --git a/moremesecons_playerkiller/depends.txt b/moremesecons_playerkiller/depends.txt index 886b035..d041f20 100644 --- a/moremesecons_playerkiller/depends.txt +++ b/moremesecons_playerkiller/depends.txt @@ -1,3 +1,4 @@ mesecons mesecons_materials moremesecons_utils +craft_guide? diff --git a/moremesecons_sayer/depends.txt b/moremesecons_sayer/depends.txt index 4c6f872..376102c 100644 --- a/moremesecons_sayer/depends.txt +++ b/moremesecons_sayer/depends.txt @@ -2,3 +2,4 @@ mesecons mesecons_noteblock moremesecons_utils default +craft_guide? diff --git a/moremesecons_signalchanger/depends.txt b/moremesecons_signalchanger/depends.txt index acaa924..0ddb4fb 100644 --- a/moremesecons_signalchanger/depends.txt +++ b/moremesecons_signalchanger/depends.txt @@ -1 +1,2 @@ mesecons +craft_guide? diff --git a/moremesecons_switchtorch/depends.txt b/moremesecons_switchtorch/depends.txt index 7ebf50c..0ddb4fb 100644 --- a/moremesecons_switchtorch/depends.txt +++ b/moremesecons_switchtorch/depends.txt @@ -1 +1,2 @@ -mesecons \ No newline at end of file +mesecons +craft_guide? diff --git a/moremesecons_teleporter/depends.txt b/moremesecons_teleporter/depends.txt index a259a42..5a7e5e6 100644 --- a/moremesecons_teleporter/depends.txt +++ b/moremesecons_teleporter/depends.txt @@ -1,2 +1,3 @@ mesecons moremesecons_utils +craft_guide? diff --git a/moremesecons_timegate/depends.txt b/moremesecons_timegate/depends.txt index acaa924..0ddb4fb 100644 --- a/moremesecons_timegate/depends.txt +++ b/moremesecons_timegate/depends.txt @@ -1 +1,2 @@ mesecons +craft_guide? diff --git a/moremesecons_wireless/depends.txt b/moremesecons_wireless/depends.txt index 79129cd..a26346f 100644 --- a/moremesecons_wireless/depends.txt +++ b/moremesecons_wireless/depends.txt @@ -1,3 +1,4 @@ mesecons moremesecons_utils digilines? +craft_guide? From c53ac5b660241c2f4167e7e7836497f1fa010083 Mon Sep 17 00:00:00 2001 From: upsilon Date: Sun, 21 May 2017 19:11:12 +0200 Subject: [PATCH 55/60] Adjustable Blinky Plant: fix very fast blinking before having set an interval + add a minimum interval setting --- moremesecons_adjustable_blinkyplant/depends.txt | 1 + moremesecons_adjustable_blinkyplant/init.lua | 10 ++++++++-- moremesecons_utils/init.lua | 5 ++++- settingtypes.txt | 5 +++++ 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/moremesecons_adjustable_blinkyplant/depends.txt b/moremesecons_adjustable_blinkyplant/depends.txt index 0ddb4fb..5a7e5e6 100644 --- a/moremesecons_adjustable_blinkyplant/depends.txt +++ b/moremesecons_adjustable_blinkyplant/depends.txt @@ -1,2 +1,3 @@ mesecons +moremesecons_utils craft_guide? diff --git a/moremesecons_adjustable_blinkyplant/init.lua b/moremesecons_adjustable_blinkyplant/init.lua index 0afaaf1..66c5e9d 100644 --- a/moremesecons_adjustable_blinkyplant/init.lua +++ b/moremesecons_adjustable_blinkyplant/init.lua @@ -4,7 +4,11 @@ local toggle_timer = function (pos, restart) and not restart then timer:stop() else - timer:start(tonumber(minetest.get_meta(pos):get_string("interval")) or 0) + local interval = tonumber(minetest.get_meta(pos):get_string("interval")) or 1 + if interval < moremesecons.setting("adjustable_blinky_plant", "min_interval", 0.5) then + interval = moremesecons.setting("adjustable_blinky_plant", "min_interval", 0.5) + end + timer:start(interval) end end @@ -30,7 +34,9 @@ mesecon.register_node("moremesecons_adjustable_blinkyplant:adjustable_blinky_pla }, on_timer = on_timer, on_construct = function(pos) - minetest.get_meta(pos):set_string("formspec", "field[interval;interval;${interval}]") + local meta = minetest.get_meta(pos) + meta:set_string("interval", "1") + meta:set_string("formspec", "field[interval;interval;${interval}]") toggle_timer(pos, true) end, on_receive_fields = function(pos, _, fields, player) diff --git a/moremesecons_utils/init.lua b/moremesecons_utils/init.lua index 0f83dab..2f6545b 100644 --- a/moremesecons_utils/init.lua +++ b/moremesecons_utils/init.lua @@ -13,7 +13,10 @@ function moremesecons.setting(modname, settingname, default, min) return minetest.setting_get(setting) or default elseif type(default) == "number" then local ret = tonumber(minetest.setting_get(setting)) or default - if ret ~= ret then -- NaN + if not ret then + minetest.log("warning", "[moremesecons_"..modname.."]: setting '"..setting.."' must be a number. Set to default value ("..tostring(default)..").") + ret = default + elseif ret ~= ret then -- NaN minetest.log("warning", "[moremesecons_"..modname.."]: setting '"..setting.."' is NaN. Set to default value ("..tostring(default)..").") ret = default end diff --git a/settingtypes.txt b/settingtypes.txt index 621f29b..7103b34 100644 --- a/settingtypes.txt +++ b/settingtypes.txt @@ -1,3 +1,8 @@ +[Adjustable Blinky Plant] + +# Minimal interval authorized. Any lower will be set to it. +moremesecons_adjustable_blinky_plant.min_interval (Minimum Interval) float 0.5 + [Craftable Commandblock] # Space-separated list of authorized commands From 482dd4df93d6de7e6ee9262adfae1cb1edba813b Mon Sep 17 00:00:00 2001 From: upsilon Date: Sun, 28 May 2017 15:14:33 +0200 Subject: [PATCH 56/60] Add an induction transmitter This node makes the signal goes two nodes forward instead of one --- .../depends.txt | 1 + moremesecons_induction_transmitter/init.lua | 96 +++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 moremesecons_induction_transmitter/depends.txt create mode 100644 moremesecons_induction_transmitter/init.lua diff --git a/moremesecons_induction_transmitter/depends.txt b/moremesecons_induction_transmitter/depends.txt new file mode 100644 index 0000000..acaa924 --- /dev/null +++ b/moremesecons_induction_transmitter/depends.txt @@ -0,0 +1 @@ +mesecons diff --git a/moremesecons_induction_transmitter/init.lua b/moremesecons_induction_transmitter/init.lua new file mode 100644 index 0000000..75f94d8 --- /dev/null +++ b/moremesecons_induction_transmitter/init.lua @@ -0,0 +1,96 @@ +local function induction_transmitter_get_input_rules(node) + -- All horizontal rules, except the output + local rules = { + {x=-1,y=0,z=0}, + {x=1,y=0,z=0}, + {x=0,y=0,z=-1}, + {x=0,y=0,z=1} + } + for i, r in ipairs(rules) do + if vector.equals(r, minetest.facedir_to_dir(node.param2)) then + table.remove(rules, i) + end + end + return rules +end + +local function induction_transmitter_get_output_rules(node) + return {vector.multiply(minetest.facedir_to_dir(node.param2), 2)} +end + +local function induction_transmitter_get_virtual_output_rules(node) + return {minetest.facedir_to_dir(node.param2)} +end + +local function act(pos, node, state) + minetest.swap_node(pos, {name = "moremesecons_induction_transmitter:induction_transmitter_"..state, param2 = node.param2}) + + local dir = minetest.facedir_to_dir(node.param2) + local target_pos = vector.add(pos, vector.multiply(dir, 2)) + local target_node = minetest.get_node(target_pos) + if mesecon.is_effector(target_node.name) then + -- Switch on an aside node, so it sends a signal to the target node + local aside_rule = mesecon.effector_get_rules(target_node)[1] + if not aside_rule then + return + end + mesecon["receptor_"..state](vector.add(target_pos, aside_rule), {vector.multiply(aside_rule, -1)}) + elseif mesecon.is_conductor(target_node.name) then + -- Switch on the conductor itself + mesecon["receptor_"..state](target_pos, mesecon.conductor_get_rules(target_node)) + end +end + +mesecon.register_node("moremesecons_induction_transmitter:induction_transmitter", { + description = "Induction Transmitter", + drawtype = "nodebox", + paramtype = "light", + paramtype2 = "facedir", + node_box = { + type = "fixed", + fixed = { + {-0.5, -0.5, 0.125, 0.5, 0.5, 0.5}, + {-0.375, -0.375, -0.1875, 0.375, 0.375, 0.125}, + {-0.25, -0.25, -0.5, 0.25, 0.25, -0.1875}, + } + }, + selection_box = { + type = "fixed", + fixed = { + {-0.5, -0.5, 0.125, 0.5, 0.5, 0.5}, + {-0.375, -0.375, -0.1875, 0.375, 0.375, 0.125}, + {-0.25, -0.25, -0.5, 0.25, 0.25, -0.1875}, + }, + }, +}, { + tiles = {"default_mese_block.png"}, + groups = {cracky = 3}, + mesecons = { + receptor = { + state = mesecon.state.off, + rules = induction_transmitter_get_output_rules + }, + effector = { + rules = induction_transmitter_get_input_rules, + action_on = function(pos, node) + act(pos, node, "on") + end + } + } +}, { + light_source = 5, + tiles = {"default_mese_block.png^[brighten"}, + groups = {cracky = 3, not_in_creative_inventory = 1}, + mesecons = { + receptor = { + state = mesecon.state.on, + rules = induction_transmitter_get_output_rules + }, + effector = { + rules = induction_transmitter_get_input_rules, + action_off = function(pos, node) + act(pos, node, "off") + end + } + } +}) From 8cac3f8f70c84b580dad5f29f4c554a5db5a8e5c Mon Sep 17 00:00:00 2001 From: upsilon Date: Sun, 28 May 2017 18:02:31 +0200 Subject: [PATCH 57/60] Add a craft for the induction transmitter --- moremesecons_induction_transmitter/depends.txt | 2 ++ moremesecons_induction_transmitter/init.lua | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/moremesecons_induction_transmitter/depends.txt b/moremesecons_induction_transmitter/depends.txt index acaa924..9e005d0 100644 --- a/moremesecons_induction_transmitter/depends.txt +++ b/moremesecons_induction_transmitter/depends.txt @@ -1 +1,3 @@ mesecons +mesecons_torch +default diff --git a/moremesecons_induction_transmitter/init.lua b/moremesecons_induction_transmitter/init.lua index 75f94d8..1ca1ba0 100644 --- a/moremesecons_induction_transmitter/init.lua +++ b/moremesecons_induction_transmitter/init.lua @@ -94,3 +94,11 @@ mesecon.register_node("moremesecons_induction_transmitter:induction_transmitter" } } }) + +minetest.register_craft({ + output = "moremesecons_induction_transmitter:induction_transmitter_off", + recipe = { + {"default:mese_crystal_fragment", "mesecons_torch:mesecon_torch_on", "default:mese_crystal_fragment"}, + {"", "default:mese_crystal_fragment", ""} + } +}) From 180167110f93a4ee0d6e97b7eff7ed317d451cce Mon Sep 17 00:00:00 2001 From: upsilon Date: Fri, 2 Jun 2017 14:51:10 +0200 Subject: [PATCH 58/60] LuaBlock: store md5 checksums This avoids changing the LuaBlock code by setting its metadata. --- .gitmodules | 3 +++ moremesecons_luablock/depends.txt | 1 + moremesecons_luablock/init.lua | 41 +++++++++++++++++++++++++++++++ moremesecons_luablock/md5_lua | 1 + 4 files changed, 46 insertions(+) create mode 100644 .gitmodules create mode 160000 moremesecons_luablock/md5_lua diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..516bd94 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "moremesecons_luablock/md5_lua"] + path = moremesecons_luablock/md5_lua + url = https://github.com/kikito/md5.lua.git diff --git a/moremesecons_luablock/depends.txt b/moremesecons_luablock/depends.txt index acaa924..a259a42 100644 --- a/moremesecons_luablock/depends.txt +++ b/moremesecons_luablock/depends.txt @@ -1 +1,2 @@ mesecons +moremesecons_utils diff --git a/moremesecons_luablock/init.lua b/moremesecons_luablock/init.lua index b501f41..53242f3 100644 --- a/moremesecons_luablock/init.lua +++ b/moremesecons_luablock/init.lua @@ -1,3 +1,39 @@ +local hash_table +local md5 +local storage +if minetest.get_mod_storage then + md5 = dofile(minetest.get_modpath(minetest.get_current_modname()).."/md5_lua/md5.lua") + storage = minetest.get_mod_storage() + hash_table = minetest.deserialize(storage:get_string("hash_table")) or {} +else + minetest.log("warning", "[moremesecons_luablock] Your version of Minetest does not provide a mod storage API. The mod storage allows moremesecons_luablock to store md5 checksums, which avoids some potential security breaches.") +end + +local function set_md5(pos, code) + if not hash_table then + return + end + vector.set_data_to_pos(hash_table, pos.z,pos.y,pos.x, md5.sum(code)) + storage:set_string("hash_table", minetest.serialize(hash_table)) +end + +local function check_md5(pos, code) + if not hash_table then + return true + end + local stored_sum = vector.get_data_from_pos(hash_table, pos.z,pos.y,pos.x) + if not stored_sum then + -- Legacy + set_md5(pos, code) + return true + end + if md5.sum(code) ~= stored_sum then + return false + end + return true +end + + local function make_formspec(meta, pos) local code = meta:get_string("code") local errmsg = minetest.formspec_escape(meta:get_string("errmsg")) @@ -74,6 +110,7 @@ minetest.register_node("moremesecons_luablock:luablock", { end meta:set_string("code", fields.code) + set_md5(pos, fields.code) make_formspec(meta, pos) end, can_dig = function(pos, player) @@ -87,6 +124,10 @@ minetest.register_node("moremesecons_luablock:luablock", { if code == "" then return end + if not check_md5(npos, code) then + minetest.log("warning", "[moremesecons_luablock] Code of LuaBlock at pos "..minetest.pos_to_string(npos).." does not match with its md5 checksum!") + return + end -- We do absolutely no check there. -- There is no limitation in the number of instruction the LuaBlock can execute -- or the usage it can make of loops. diff --git a/moremesecons_luablock/md5_lua b/moremesecons_luablock/md5_lua new file mode 160000 index 0000000..e8cd75c --- /dev/null +++ b/moremesecons_luablock/md5_lua @@ -0,0 +1 @@ +Subproject commit e8cd75ce397da6dd75ddb37cb07eb31663743223 From 79e856494a9960092f36e438d7e53524c3373501 Mon Sep 17 00:00:00 2001 From: upsilon Date: Sat, 3 Jun 2017 15:32:05 +0200 Subject: [PATCH 59/60] Remove md5_lua submodule This prepares re-adding it as a subtree. --- .gitmodules | 3 --- moremesecons_luablock/md5_lua | 1 - 2 files changed, 4 deletions(-) delete mode 160000 moremesecons_luablock/md5_lua diff --git a/.gitmodules b/.gitmodules index 516bd94..e69de29 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +0,0 @@ -[submodule "moremesecons_luablock/md5_lua"] - path = moremesecons_luablock/md5_lua - url = https://github.com/kikito/md5.lua.git diff --git a/moremesecons_luablock/md5_lua b/moremesecons_luablock/md5_lua deleted file mode 160000 index e8cd75c..0000000 --- a/moremesecons_luablock/md5_lua +++ /dev/null @@ -1 +0,0 @@ -Subproject commit e8cd75ce397da6dd75ddb37cb07eb31663743223 From 6662f92902688b327b92c61e074cd9b3432020ad Mon Sep 17 00:00:00 2001 From: upsilon Date: Sat, 3 Jun 2017 15:34:39 +0200 Subject: [PATCH 60/60] Remove empty file .gitmodules --- .gitmodules | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .gitmodules diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index e69de29..0000000