egor-skriptunoff / pure_lua_sha Goto Github PK
View Code? Open in Web Editor NEWSHA1, SHA2, SHA3, BLAKE2 and BLAKE3 functions written in pure Lua and optimized for speed
License: MIT License
SHA1, SHA2, SHA3, BLAKE2 and BLAKE3 functions written in pure Lua and optimized for speed
License: MIT License
Hi, I'm working on a side project for which I have to generate hashes for Ethereum, which uses the old padding of 1 instead of 6. Changing it on this line fixed it for me:
https://github.com/Egor-Skriptunoff/pure_lua_SHA/blob/master/sha2.lua#L4618
Is there any chance of making this configurable for a user of the module or would you accept a PR for this?
This is so one does not have to maintain one's own fork of this project.
(I use this project because of its speed, easy of use and also because I use LuaJit which a lot of other projects do not support (anymore).)
Reference: https://ethereum.stackexchange.com/questions/30369/difference-between-keccak256-and-sha3
First - thank you for your excellent work!.
I attach a short script showing a problem with bin2base64. The script generates a random 10 character string and attempts to convert it via base64 using sha.bin2base64 and compares it with another base64 algorithm. Mismatches are outputted.
I hope this helps you discover the bug in bin2base64.
Regards,
Tom
local sha = require 'sha2'
-- character table string
local b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
-- encoding
function encode_b64(data)
return ((data:gsub('.', function(x)
local r,b='',x:byte()
for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
return r;
end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
if (#x < 6) then return '' end
local c=0
for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
return b:sub(c+1,c+1)
end)..({ '', '==', '=' })[#data%3+1])
end
local charset = {}
for i = 0, 255 do table.insert(charset, string.char(i)) end
math.randomseed(os.time())
function string.random(length)
if length > 0 then
return string.random(length - 1) .. charset[math.random(1, #charset)]
else
return ''
end
end
for i=1,100 do
local s = string.random(10)
if encode_b64(s) ~= sha.bin2base64(sha.hex2bin(s)) then
print('error',encode_b64(s),sha.bin2base64(sha.hex2bin(s)))
end
end
For string "test"
your lib output is: 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08
openssl output is: f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2
Possible add mime encoding-decoding + other popular formats for your choice? So add mime & base64 to tags project. Your script like me - very fast, good job.
Hi, I think that your code isn't accurately detecting Lua 5.1.5 on OpenWrt. Using a simple sha.sha256("test")
generates the following error:
lua: ./sha2.lua:309: bad argument #2 to 'string_format' (integer expected, got number)
stack traceback:
[C]: in function 'string_format'
./sha2.lua:309: in function 'HEX'
./sha2.lua:2467: in function <./sha2.lua:2433>
(tail call): ?
(tail call): ?
shatest.lua:12: in function 'test'
shatest.lua:16: in main chunk
[C]: ?
Is there anything I can do to help debug?
something like this (for luajit 2.1):
local ffi=require 'ffi'
local C,ibuf,obuf = ffi.C,ffi.new("unsigned char[?]",buf_size),ffi.new("unsigned char[?]",buf_size*2)
while true do
local n = tonumber(C.fread(ibuf,1,ffi.sizeof(ibuf),f_in))
if n==0 then break end
for i=0,n-1 do
local low = ibuf[i]%16
local high = bit.rshift(ibuf[i],4)
obuf[i+i] = high<10 and high+48 or high+55
obuf[i+i+1] = low<10 and low+48 or low+55
end
C.fwrite(obuf,1,n+n,f_out)
end
Thanks for very good (fast!) checksum library
Example:
-- VERSION: 12 (2022-02-23)
-- AUTHOR: Egor Skriptunoff
-- LICENSE: MIT (the same license as Lua itself)
-- URL: https://github.com/Egor-Skriptunoff/pure_lua_SHA
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.