安装 https://github.com/openresty/lua-resty-string

$ sudo opm get openresty/lua-resty-string
$ ls -al /usr/local/openresty/site/lualib/resty/
总用量 100
drwxr-xr-x 3 root root 4096 9月 16 20:47 .
drwxr-xr-x 3 root root 4096 9月 8 07:38 ..
-rw-r--r-- 1 root root 6129 9月 16 20:47 aes.lua
-rw-r--r-- 1 root root 1150 9月 11 19:10 http_headers.lua
-rw-r--r-- 1 root root 29686 9月 11 19:10 http.lua
drwxr-xr-x 2 root root 4096 9月 10 21:32 lrucache
-rw-r--r-- 1 root root 5727 9月 10 21:32 lrucache.lua
-rw-r--r-- 1 root root 1211 9月 16 20:47 md5.lua
-rw-r--r-- 1 root root 616 9月 16 20:47 random.lua
-rw-r--r-- 1 root root 1192 9月 16 20:47 sha1.lua
-rw-r--r-- 1 root root 1045 9月 16 20:47 sha224.lua
-rw-r--r-- 1 root root 1221 9月 16 20:47 sha256.lua
-rw-r--r-- 1 root root 1045 9月 16 20:47 sha384.lua
-rw-r--r-- 1 root root 1359 9月 16 20:47 sha512.lua
-rw-r--r-- 1 root root 236 9月 16 20:47 sha.lua
-rw-r--r-- 1 root root 698 9月 16 20:47 string.lua

可以看出目录下多出了这几个文件:aes.lua md5.lua random.lua sha.lua sha1.lua sha224.lua sha256.lua sha384.lua sha512.lua string.lua

测试文件 test.lua

local resty_sha1 = require "resty.sha1"

local sha1 = resty_sha1:new()
if not sha1 then
ngx.say("failed to create the sha1 object")
return
end local ok = sha1:update("hello, ")
if not ok then
ngx.say("failed to add data")
return
end ok = sha1:update("world")
if not ok then
ngx.say("failed to add data")
return
end local digest = sha1:final() -- binary digest local str = require "resty.string"
ngx.say("sha1: ", str.to_hex(digest))
-- output: "sha1: b7e23ec29af22b0b4e41da31e868d57226121c84" local resty_md5 = require "resty.md5"
local md5 = resty_md5:new()
if not md5 then
ngx.say("failed to create md5 object")
return
end local ok = md5:update("hel")
if not ok then
ngx.say("failed to add data")
return
end ok = md5:update("lo")
if not ok then
ngx.say("failed to add data")
return
end local digest = md5:final() local str = require "resty.string"
ngx.say("md5: ", str.to_hex(digest))
-- yield "md5: 5d41402abc4b2a76b9719d911017c592" local resty_sha224 = require "resty.sha224"
local str = require "resty.string"
local sha224 = resty_sha224:new()
ngx.say(sha224:update("hello"))
local digest = sha224:final()
ngx.say("sha224: ", str.to_hex(digest)) local resty_sha256 = require "resty.sha256"
local str = require "resty.string"
local sha256 = resty_sha256:new()
ngx.say(sha256:update("hello"))
local digest = sha256:final()
ngx.say("sha256: ", str.to_hex(digest)) local resty_sha512 = require "resty.sha512"
local str = require "resty.string"
local sha512 = resty_sha512:new()
ngx.say(sha512:update("hello"))
local digest = sha512:final()
ngx.say("sha512: ", str.to_hex(digest)) local resty_sha384 = require "resty.sha384"
local str = require "resty.string"
local sha384 = resty_sha384:new()
ngx.say(sha384:update("hel"))
ngx.say(sha384:update("lo"))
local digest = sha384:final()
ngx.say("sha384: ", str.to_hex(digest)) local resty_random = require "resty.random"
local str = require "resty.string"
local random = resty_random.bytes(16)
-- generate 16 bytes of pseudo-random data
ngx.say("pseudo-random: ", str.to_hex(random)) local resty_random = require "resty.random"
local str = require "resty.string"
local strong_random = resty_random.bytes(16,true)
-- attempt to generate 16 bytes of
-- cryptographically strong random data
while strong_random == nil do
strong_random = resty_random.bytes(16,true)
end
ngx.say("random: ", str.to_hex(strong_random)) local aes = require "resty.aes"
local str = require "resty.string"
local aes_128_cbc_md5 = aes:new("AKeyForAES")
-- the default cipher is AES 128 CBC with 1 round of MD5
-- for the key and a nil salt
local encrypted = aes_128_cbc_md5:encrypt("Secret message!")
ngx.say("AES 128 CBC (MD5) Encrypted HEX: ", str.to_hex(encrypted))
ngx.say("AES 128 CBC (MD5) Decrypted: ", aes_128_cbc_md5:decrypt(encrypted)) local aes = require "resty.aes"
local str = require "resty.string"
local aes_256_cbc_sha512x5 = aes:new("AKeyForAES-256-CBC",
"MySalt!!", aes.cipher(256,"cbc"), aes.hash.sha512, 5)
-- AES 256 CBC with 5 rounds of SHA-512 for the key
-- and a salt of "MySalt!!"
-- Note: salt can be either nil or exactly 8 characters long
local encrypted = aes_256_cbc_sha512x5:encrypt("Really secret message!")
ngx.say("AES 256 CBC (SHA-512, salted) Encrypted HEX: ", str.to_hex(encrypted))
ngx.say("AES 256 CBC (SHA-512, salted) Decrypted: ",
aes_256_cbc_sha512x5:decrypt(encrypted)) local aes = require "resty.aes"
local str = require "resty.string"
local aes_128_cbc_with_iv = assert(aes:new("1234567890123456", nil, aes.cipher(128,"cbc"), {iv="1234567890123456"}))
-- AES 128 CBC with IV and no SALT
local encrypted = aes_128_cbc_with_iv:encrypt("Really secret message!")
ngx.say("AES 128 CBC (WITH IV) Encrypted HEX: ", str.to_hex(encrypted))
ngx.say("AES 128 CBC (WITH IV) Decrypted: ", aes_128_cbc_with_iv:decrypt(encrypted))

测试

$ resty test.lua
sha1: b7e23ec29af22b0b4e41da31e868d57226121c84
md5: 5d41402abc4b2a76b9719d911017c592
true
sha224: ea09ae9cc6768c50fcee903ed054556e5bfc8347907f12598aa24193
true
sha256: 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
true
sha512: 9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043
true
true
sha384: 59e1748777448c69de6b800d7a33bbfb9ff1b463e44354c3553bcdb9c666fa90125a3c79f90397bdf5f6a13de828684f
pseudo-random: 4c1f6d82cba1d0441b8f96c6cc0a8183
random: 5558eeac062e63c4304c40532129d9de
AES 128 CBC (MD5) Encrypted HEX: 6d87d6b11c6bf0dcb76d1aa611520b3c
AES 128 CBC (MD5) Decrypted: Secret message!
AES 256 CBC (SHA-512, salted) Encrypted HEX: 02997c9c6eb26af1a335ed8c11d53df3bf127086ecbba02fb73391bcf385bd2c
AES 256 CBC (SHA-512, salted) Decrypted: Really secret message!
AES 128 CBC (WITH IV) Encrypted HEX: f9fa30df0325a7680327842c6a232e7838f7149d80cb8a669cf503a45afc2fa5
AES 128 CBC (WITH IV) Decrypted: Really secret message!

openresty lua-resty-string md5 sha aes random string的更多相关文章

  1. Java http数据MD5、AES、DES加密

    一,数据加密 1.提供了,md5,Hex,Sha等不可逆算法加密 2.AES加密,此加密方式瘦平台影响较重,所以只适合同类平台加密解密 3.DES自定义加密,跨平台,兼容性好 1.org.apache ...

  2. Java Base64、HMAC、SHA1、MD5、AES DES 3DES加密算法

    ●  BASE64 严格地说,属于编码格式,而非加密算法    ●  MD5(Message Digest algorithm 5,信息摘要算法)    ●  SHA(Secure Hash Algo ...

  3. BASE64,MD5,SHA,HMAC加密與解密算法(java)

    package com.ice.webos.util.security; import java.io.UnsupportedEncodingException; import java.math.B ...

  4. 如何生成安全的密码 Hash:MD5, SHA, PBKDF2, BCrypt 示例

    密码 Hash 值的产生是将用户所提供的密码通过使用一定的算法计算后得到的加密字符序列.在 Java 中提供很多被证明能有效保证密码安全的 Hash 算法实现,我将在这篇文章中讨论其中的部分算法. 需 ...

  5. 微信小程序下可以使用的MD5以及AES加密(通用)

    两段代码都来自网络 ,在小程序下的加解密结果与CS/BS等算出的结果都一致,支持汉字. 一.MD5: var rotateLeft = function (lValue, iShiftBits) { ...

  6. openresty+lua+kafka方案与Tomcat接口并发度对比分析

    1.openresty+lua+kafka 1.1 openresty+lua+kafka方案 之前的项目基于nginx反向代理后转发到Tomcat的API接口进行业务处理,然后将json数据打入ka ...

  7. OpenResty Lua钩子调用完整流程

    前面一篇文章介绍了Openresty Lua协程调度机制,主要关心的是核心调度函数run_thread()内部发生的事情,而对于外部的事情我们并没有涉及.本篇作为其姊妹篇,准备补上剩余的部分.本篇将通 ...

  8. Java 消息摘要 散列 MD5 SHA

    package xxx.common.util; import java.math.BigInteger; import java.security.MessageDigest; import jav ...

  9. Java加密技术(一)——BASE64与单向加密算法MD5&SHA&MAC

    Java加密技术(一)——BASE64与单向加密算法MD5&SHA&MAC 博客分类: Java/Security Javabase64macmd5sha     加密解密,曾经是我一 ...

随机推荐

  1. 深入刨析tomcat 之---第10篇 how tomcat works 第13章,Response 发送错误信息 sendError

    writedby 张艳涛 在浏览器中发送一个错误应用url 那么tomcat是如何发送错误的呢? 基本上是发送http 的response协议,分为两部分一部分是response设置头信息, 那么先分 ...

  2. Linux[Manjaro] 小新15笔记本AMD ryzen锐龙4800U,在安装系统后出现的随即死机冻屏问题

    Linux[Manjaro] 小新15AMD ryzen锐龙4800U,在安装系统后出现的随即死机冻屏问题解决办法 年初尝试将manjaro安装在我的笔记本上就存在这个问题,也一度将我劝退.系统安装在 ...

  3. python算法(2)兔子产子(斐波那切数列)

    兔子产子 1.问题描述 有一对兔子,从出生后的第3个月起每个月都生一对兔子.小兔子长到第3个月后每个月又生一对兔子,假设所有的兔子都不死,问30个月内每个月的兔子总对数为多少? 2.问题分析 兔子产子 ...

  4. [WUSTCTF2020]朴实无华

    [WUSTCTF2020]朴实无华 考点:1.MD5碰撞 2.命令执行绕过 打开题发现编码有些问题,修改编码为utf-8 看了源码和请求包没发现什么东西,看到title想到了robots.txt文件, ...

  5. 异地远程访问群晖NAS中的文件

    异地远程访问群晖NAS中的文件   我以群晖DS720+网络存储服务器为例,介绍我是如何异地远程访问群晖NAS中的文件的.   此文章只介绍部署操作的大概步骤,具体的操作方法和技巧可以在西瓜视频.抖音 ...

  6. QML用Instantiator动态创建顶级窗口

    关键点 使用Model驱动Instantiator QML里面的hashmap: QQmlPropertyMap 上一次说到用 QQmlApplicationEngine 多次load的方式创建多个一 ...

  7. 关于表单重复提交之验证码 和谷歌Kaptcha图片验证码的使用

    表单重复提交之-----验证码 表单重复提交有三种常见的情况: 一:提交完表单.服务器使用请求转来进行页面跳转.这个时候,用户按下功能键 F5,就会发起最后一次的请求. 造成表单重复提交问题.解决方法 ...

  8. 用华为云cli,管理华为云服务器的,安全组端口

    ---[前言]--- 关键字 hcloud 华为 命令行 linux windows powershell 前些天,大家因为华为云,是否应该默认开启端口,大家吵起来了,所以我抽空写了此文.解决问题,缓 ...

  9. Eclipse中的项目红叉问题查找记录

    1.先查看项目的validation有没有都勾上,如果都勾上,一般项目下边的文件夹下会显示出来.可以解决. 2.如果项目文件夹下各个红叉都解决了,没有什么报错了.那么可以看下工具的problem标签下 ...

  10. MySQL-19-分布式架构MyCat

    MyCAT基础架构图(实验环境) MyCAT实验环境准备 1 环境准备 两台虚拟机: db01(10.0.0.51) db02(10.0.0.52) 每台创建四个mysql实例:3307 3308 3 ...