Git Product home page Git Product logo

lua-hashings's Introduction

Lua-hashings

Pure Lua cryptographic hash library.

Supported hashes

  • adler32
  • blake2b
  • blake2s
  • crc32
  • md5
  • ripemd160
  • sha1
  • sha256
  • sha3_256
  • sha3_512
  • sha512
  • whirlpool

Additional hash functions

  • hmac
  • pbkdf2

API

All hash modules support the same API and any module implementing this API can be used by the additional hash functions.

  • new(data) or (data)
  • copy()
  • update(data)
  • digest()
  • hexdigest()

The digest and hexdigest functions will not change the internal state of the object. Multiple calls to these will return the same result.

All hash modules provide the following information about the hash.

  • digest_size
  • block_size

There are some functions, such as hexdigest, which are the same implementation in multiple modules. This is by design because modules are intended to be independent of the library itself. Each module can be copied into another project without needing the rest of the library. The hash functions are the same in that they can be dropped into another project and used independent of the library provided that anything using the hash functions implements the above API.

Dependencies

This library depends on lua-nums because many hashes rely on fixed width integers. It's also needed to support 64 bit hashes.

Example Use

LUA_PATH="../lua-nums/?/init.lua;../lua-nums/?.lua;../lua-hashings/?/init.lua;../lua-hashings/?.lua;./?.lua" lua hs.lua

local digest = require("hashings.sha256")
print(digest:new("Hello"):hexdigest())
local hashings = require("hashings")
local hs = hashings("sha256")
hs:update("Hello")
print(hs:hexdigest())
local mod = require("hashings").sha256
local hs = mod()
hs:update("Hello")
print(hs:hexdigest())

HMAC

local hashings = require("hashings")
local hs = hashings.hmac(hashings.sha256, 'secret123', "Hello")
print(hs:hexdigest())
local hashings = require("hashings")
local hs = hashings.hmac(hashings.sha256, 'secret123')
hs:update("Hello")
print(hs:hexdigest())

PBKDF2

local hashings = require("hashings")
print(hashings.pbkdf2(hashings.sha256, "password", 123, 256))

Data from file

local hs = require("hashings").sha256()
local f = io.open("hs.lua", "rb")
while true do
    local r = f:read(2) # You should use are more appropriately sized buffer
    if r == nuil then
        break
    end
    hs:update(r)
end
print(hs:hexdigest())

Performance

32 bit hashes have quite good performance. However, due to using lua-nums which provides fixed width unsigned 64 bit integers 64 bit hashes are very slow. It is not recommended to use 64 bit hashes in performance critical code.

lua-hashings's People

Contributors

jlnwntr avatar user-none avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

lua-hashings's Issues

Requires LUA 5.3

First, thank you for the software. it works well (at least sha256)
I had to upgrade Ubuntu 18.04 version of IMAPFILTER to accommodate LUA 5.3 but having done that it worked great.

not really a big deal as I figured it out but you should note that the code requires LUA 5.3 or I guess higher to work in the dependancies sections on the README.
Ubuntu 18.04 defaults to LUA 5.2 as preferred. don't know yet on Ubuntu 20.04

How to use Hmac in lua-hashing?

Hello i'am newbie in hashing and cryptographic methods, i very love lua and want to use only it for my needs, now i need to access some authenticated API of some sites through https requests, they demand to use SHA-512 and Hmac methods of hashings in theirs API, i found this library in github, it is clear how to use SHA-256, 512 and others from example of author but i totally cannot understand how to use Hmac method i tried to to so:

local hmacs = require ("hashings.hmac")

local api_secret_key = "My_secret_key";

print(hmacs:new(api_secret_key):new())

but it gives me error: hmac.lua:38: attempt to get length of a nil value (local 'key')
I totally cannot unerstand!!!!!!!!!!!!!!! PLEASE HELP ME!!!!!!!!!!!!!!!!!!!!!

Issue with hashings and imapfilter

I have installed both nums and hashings using luarocks 3.8 (as suggested by response to my nums comment) on both my laptop running ubuntu 20.04 desktop and on my server using ubuntu 20.04 server version. When I run imapfilter executing the command hs = require("hashings").sha256() on my laptop, it works fine. On the other hand when I run the same program on my server I get
imapfilter: /home/beckstein/.imapfilter/configtest.lua:1134: module 'hashings' not found:
no field package.preload['hashings']
no file '/usr/local/share/lua/5.2/hashings.lua'
no file '/usr/local/share/lua/5.2/hashings/init.lua'
no file '/usr/local/lib/lua/5.2/hashings.lua'
no file '/usr/local/lib/lua/5.2/hashings/init.lua'
no file '/usr/share/lua/5.2/hashings.lua'
no file '/usr/share/lua/5.2/hashings/init.lua'
no file './hashings.lua'
no file '/usr/local/lib/lua/5.2/hashings.so'
no file '/usr/lib/x86_64-linux-gnu/lua/5.2/hashings.so'
no file '/usr/lib/lua/5.2/hashings.so'
no file '/usr/local/lib/lua/5.2/loadall.so'
no file './hashings.so'
stack traceback:
[C]: in ?
[C]: in function 'require'
/home/xxxx/.imapfilter/configtest.lua:1134: in function 'configuredatafn'
/home/xxxx/.imapfilter/configtest.lua:1309: in main chunk

Can you give me a hint what you think is missing in my server installation that I get this error.

ps if I run lua interperter on the server and the command hs = require("hashings").sha256(), it does not have this error.
I do notice that the /usr/share/lua/5.3 directory on my server does not include either the nums or hashings directories that my laptop does. I also note that the file /usr/share/lua/5.3/re.lua (which I have no clue what created it and not clear it has anything to do with this problem) does not appear on my server but is on my laptop.
on my server and my laptop executing: ls /usr/local/share/lua/5.3 shows
hashings luarocks nums

Hmac SHA512 not working on EXMO

Hello again, so now i've tried to apply this hashing methods - Hmac and SHA512 to work with the authenticated API access my account on EXMO, https://exmo.me/en/api it seems i've done all right, but it is not working at all - EXMO server always gives me error:

1
200
connection close
access-control-allow-headers Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Key, Sign
content-type application/json; charset=utf-8
strict-transport-security max-age=604800
content-length 81
server nginx
access-control-allow-credentials true
access-control-allow-methods POST, GET, OPTIONS, PUT, DELETE
date Sun, 20 Oct 2019 09:48:24 GMT
Response body:
{"result":false,"error":"Error 40005: Authorization error, Incorrect signature"}

So my code looks like this:

ssl = require("ssl")
http = require("socket.http")
https = require("ssl.https")
socket = require("socket")
ltn12 = require("ltn12")
hashings = require("hashings")

api_key = "myopenkey"
api_secret = "mysecretkey"

x = tostring(math.modf(os.time()*1000))

hs = hashings.hmac(hashings.sha512, api_secret)
hs:update(x)
sign=hs:hexdigest()

local request_body = "nonce="..tostring(x)
local response_body = {}

local res, code, response_headers = https.request{
    url = "https://api.exmo.com/v1/user_info/",
    method = "POST", 
    headers = 
      {
          ["Content-Type"] = "application/x-www-form-urlencoded",
          ["Content-Length"] = #request_body,
		   Key=api_key,
		   Sign=sign
      },
      source = ltn12.source.string(request_body),
      sink = ltn12.sink.table(response_body),
}

print(res)
print(code)

if type(response_headers) == "table" then
  for k, v in pairs(response_headers) do 
    print(k, v)
  end
end

print("Response body:")
if type(response_body) == "table" then
  print(table.concat(response_body))
else
  print("Not a table:", type(response_body))
end

EXMO posted examples to access their API on many languages -
https://github.com/exmo-dev/exmo_api_lib
I've tried their example on python2:

import httplib
import urllib
import json
import hashlib
import hmac
import time
 
api_key = "your_key"
api_secret = "your_secret"

nonce = int(round(time.time()*1000))
 
params = {"nonce": nonce}
params = urllib.urlencode(params)
 
H = hmac.new(api_secret, digestmod=hashlib.sha512)
H.update(params)
sign = H.hexdigest()
 
headers = {"Content-type": "application/x-www-form-urlencoded",
           "Key":api_key,
           "Sign":sign}
conn = httplib.HTTPSConnection("api.exmo.com")
conn.request("POST", "/v1/user_info", params, headers)
response = conn.getresponse()
 
print response.status, response.reason
print json.load(response)
 
conn.close()

and python3 also, and it was working without any problem - no any error messages and quick access to account
So it seems that one of your algorithms - Hmac or SHA-512, or both, has some differences from implementation of this algorithms in many languages

Installation error

Installing http://luarocks.org/dev/lua-hashings-scm-1.rockspec
Cloning into 'lua-hashings'...
remote: Counting objects: 39, done.
remote: Compressing objects: 100% (39/39), done.
remote: Total 39 (delta 11), reused 7 (delta 0), pack-reused 0
Receiving objects: 100% (39/39), 54.88 KiB | 0 bytes/s, done.
Resolving deltas: 100% (11/11), done.
Checking connectivity... done.
env MACOSX_DEPLOYMENT_TARGET=10.8 gcc -O2 -fPIC -I/usr/local/include -c hashings.hmac -o hashings.o
clang: error: no such file or directory: 'hashings.hmac'
clang: error: no input files

Error: Build error: Failed compiling object hashings.o

SHA512 Test

A sha512-hash generated by openssl:

$ openssl dgst -sha512 <<< "Test" ffb360b65e823271ec9dc554a984bcb41793cbb91cba641c52a56b29d1d5dc5f219ad18fe03fd1eb6005da7d88af158369891115f1d6dbcfa0a23b8096b64a47

is different to the one generated by lua-hashings:

local Digest = require("sha512"); local test = Digest:new("Test"):hexdigest(); print(test)
(C6EE9E33CF5C6715A1D148FD73F7318884B41ADCB916021E2BC0E800A5C5DD97F5142178F6AE88C8FDD98E1AFB0CE4C8D2C54B5F37B30B7DA1997BB33B0B8A31)

How to get sha sum for a file?

As someone who's not experienced in cryptography - I've got a handle to a Lua file, how can I get the 256 sum of it?

I've looked through the examples but I'm not familiar with the terminology.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.