• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

bungle/lua-resty-nettle: LuaJIT FFI bindings for Nettle (a low-level cryptograph ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

bungle/lua-resty-nettle

开源软件地址(OpenSource Url):

https://github.com/bungle/lua-resty-nettle

开源编程语言(OpenSource Language):

Lua 100.0%

开源软件介绍(OpenSource Introduction):

lua-resty-nettle

LuaJIT FFI bindings for Nettle (a low-level cryptographic library)

Requirements

The bindings require libnettle, and in some cases also libhogweed (comes with libnettle) and gmp. Supported Nettle version is 3.7.x. Nettle can be downloaded from here: www.lysator.liu.se/~nisse/nettle/.

Bindings are tested with Linux and macOS on x64 architecture, but I don't see any reason why they wouldn't work with other platforms and architectures.

Synopsis

local require = require
local print = print
local gsub = string.gsub
local byte = string.byte
local format = string.format
local ipairs = ipairs
local concat = table.concat

local function hex(str,spacer)
  return (gsub(str,"(.)", function (c)
    return format("%02X%s", byte(c), spacer or "")
  end))
end

do
  local md2 = require "resty.nettle.md2"
  print("md2      ", #md2(""), hex(md2("")))
  local hash = md2.new()
  hash:update("")
  print("md2     ", #hash:digest(), hex(hash:digest()))
end

do
  local md4 = require "resty.nettle.md4"
  print("md4      ", #md4(""), hex(md4("")))
  local hash = md4.new()
  hash:update("")
  print("md4      ", #hash:digest(), hex(hash:digest()))
end

do
  local md5 = require "resty.nettle.md5"
  print("md5      ", #md5(""), hex(md5("")))
  local hash = md5.new()
  hash:update("")
  print("md5      ", #hash:digest(), hex(hash:digest()))
end

do
  local ripemd160 = require "resty.nettle.ripemd160"
  local hash = ripemd160.new()
  hash:update("")
  print("ripemd160", #hash:digest(), hex(hash:digest()))
end

do
  local gosthash94 = require "resty.nettle.gosthash94"
  local hash = gosthash94.new()
  hash:update("")
  print("gosthash94", #hash:digest(), hex(hash:digest()))
end

do
  local sha1 = require "resty.nettle.sha1"
  print("sha1      ", #sha1(""), hex(sha1("")))
  local hash = sha1.new()
  hash:update("")
  print("sha1     ", #hash:digest(), hex(hash:digest()))
end

do
  local sha2 = require "resty.nettle.sha2"

  local hash = sha2.sha224.new()
  hash:update("")
  print("sha224      ", #hash:digest(), hex(hash:digest()))
  print("sha224      ", #sha2.sha224(""), hex(sha2.sha224("")))

  local hash = sha2.sha256.new()
  hash:update("")
  print("sha256      ", #hash:digest(), hex(hash:digest()))
  print("sha256      ", #sha2.sha256(""), hex(sha2.sha256("")))

  local hash = sha2.sha384.new()
  hash:update("")
  print("sha384      ", #hash:digest(), hex(hash:digest()))
  print("sha384      ", #sha2.sha384(""), hex(sha2.sha384("")))

  local hash = sha2.sha512.new()
  hash:update("")
  print("sha512      ", #hash:digest(), hex(hash:digest()))
  print("sha512      ", #sha2.sha512(""), hex(sha2.sha512("")))

  local hash = sha2.sha512_224.new()
  hash:update("")
  print("sha512_224", #hash:digest(), hex(hash:digest()))
  print("sha512_224", #sha2.sha512_224(""), hex(sha2.sha512_224("")))

  local hash = sha2.sha512_256.new()
  hash:update("")
  print("sha512_256", #hash:digest(), hex(hash:digest()))
  print("sha512_256", #sha2.sha512_256(""), hex(sha2.sha512_256("")))
end

do
  local sha3 = require "resty.nettle.sha3"

  local hash = sha3.sha224.new()
  hash:update("")
  print("sha3 224", #hash:digest(), hex(hash:digest()))

  local hash = sha3.sha256.new()
  hash:update("")
  print("sha3 256", #hash:digest(), hex(hash:digest()))

  local hash = sha3.sha384.new()
  hash:update("")
  print("sha3 384", #hash:digest(), hex(hash:digest()))

  local hash = sha3.sha512.new()
  hash:update("")
  print("sha3 512", #hash:digest(), hex(hash:digest()))
end

do
  local hmac = require "resty.nettle.hmac"
  print("hmac md5", #hmac("md5", "a", "a"), hex(hmac("md5", "a", "a")))
  print("hmac md5", #hmac.md5("a", "a"), hex(hmac.md5("a", "a")))
  local hash = hmac.md5.new("a")
  hash:update("a")
  local dgst = hash:digest()
  print("hmac md5", #dgst, hex(dgst))

  local hash = hmac.ripemd160.new("a")
  hash:update("a")
  local dgst = hash:digest()
  print("hmac ripemd160", #dgst, hex(dgst))

  local hash = hmac.sha1.new("a")
  hash:update("a")
  local dgst = hash:digest()
  print("hmac sha1", #dgst, hex(dgst))

  local hash = hmac.sha224.new("a")
  hash:update("a")
  local dgst = hash:digest()
  print("hmac sha224", #dgst, hex(dgst))

  local hash = hmac.sha256.new("a")
  hash:update("a")
  local dgst = hash:digest()
  print("hmac sha256", #dgst, hex(dgst))

  local hash = hmac.sha384.new("a")
  hash:update("a")
  local dgst = hash:digest()
  print("hmac sha384", #dgst, hex(dgst))

  local hash = hmac.sha512.new("a")
  hash:update("a")
  local dgst = hash:digest()
  print("hmac sha512", #dgst, hex(dgst))
end

do
  local umac = require "resty.nettle.umac"
  local hash = umac.umac32.new("umac32")
  hash:update("")
  local dgst = hash:digest()
  print("umac32     ", #dgst, hex(dgst))

  local hash = umac.umac64.new("umac64")
  hash:update("")
  local dgst = hash:digest()
  print("umac64     ", #dgst, hex(dgst))

  local hash = umac.umac96.new("umac96")
  hash:update("")
  local dgst = hash:digest()
  print("umac96     ", #dgst, hex(dgst))

  local hash = umac.umac128.new("umac128")
  hash:update("")
  local dgst = hash:digest()
  print("umac128     ", #dgst, hex(dgst))
end

do
  local poly = require "resty.nettle.poly1305"
  local hash = poly.new("poly")
  hash:update("")
  local dgst = hash:digest()
  print("poly1305    ", #dgst, hex(dgst))
end

do
  local pbkdf2 = require "resty.nettle.pbkdf2"
  local hmac = pbkdf2.hmac_sha1("password", 1, "salt", 20)
  print("pbkdf2 sha1", #hmac, hex(hmac))
  local hmac = pbkdf2.hmac_sha256("pass\0word", 4096, "sa\0lt", 32)
  print("pbkdf2 sha256", #hmac, hex(hmac))
end

print()

do
  local aes = require "resty.nettle.aes"
  local aes128 = aes.new("testtesttesttest")
  local ciphertext = aes128:encrypt("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
  print("aes128 encrypt", #ciphertext, hex(ciphertext))
  local aes128 = aes.new("testtesttesttest")
  local plaintext = aes128:decrypt(ciphertext)
  print("aes128 decrypt", #plaintext, plaintext)

  print()

  local aes128 = aes.new("testtesttesttest", "cbc", "testtesttesttest")
  local ciphertext = aes128:encrypt("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
  print("aes128 cbc enc", #ciphertext, hex(ciphertext))
  local aes128 = aes.new("testtesttesttest", "cbc", "testtesttesttest")
  local plaintext = aes128:decrypt(ciphertext)
  print("aes128 cbc dec", #plaintext, plaintext)

  print()

  local aes128 = aes.new("testtesttesttest 
                       
                    
                    

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
SirAnthony/slpp: Simple lua-python parser发布时间:2022-08-16
下一篇:
ers35/luakernel: Lua + SQLite + musl libc running on x86.发布时间:2022-08-16
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap