lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Sun, Apr 14, 2013 at 8:00 AM, Choonster TheMage
<choonster.2010@gmail.com> wrote:
> I'm currently modifying a pure Lua sha1 and hmac_sha1 library I found
> a while back[1] to use existing bitwise library functions instead of
> its pure Lua versions when possible.
>
> All of the bitwise arithmetic tests at the end of the file pass on all
> of the platforms I tested[2], except for the ones that print "nullop"
> followed by a series of zeroes (those tests aren't supported by all
> three bitwise libraries).
>
> Three of the four platforms pass all the validation tests, but for
> some reason Lua 5.1.5 with bitlib fails on the first one if bitlib's
> functions are used instead of the pure Lua equivalents (which pass all
> the tests).
>
> Would anyone with 5.1.5 and bitlib be willing to test the code and/or
> point out any obvious errors on my part? It can be run directly from
> the command line or with dofile.
>
> Thanks,
> Choonster
>
> [1] https://github.com/Choonster/luabnet_tools/blob/master/lua/bnet/tools/external/sha1.lua
> [2]  I've tested it on Lua 5.1.4 with Lua BitOp from LuaForWindows,
> Lua 5.1.5 with bitlib from the LuaDist repo[3], LuaJIT 2.0.1 with
> built-in Lua BitOp and Lua 5.2.2 with built-in bit32
> [3] https://github.com/LuaDist/bitlib

It turns out the problem was with the w32_rot function (left bitwise
rotation), which was giving a value off by 1 when used with bitlib's
XOR function. Re-implementing w32_rot using left and right shifts with
XOR according to the first example in [1] solved the problem.

The new w32_rot function can be found at [2] if anyone's interested.

Regards,
Choonster

[1] https://en.wikipedia.org/wiki/Circular_shift#Implementing_circular_shifts
[2] https://github.com/Choonster/luabnet_tools/blob/master/lua/bnet/tools/external/sha1.lua#L114-L123