跳转到内容

哈希函数

加密哈希函数和 HMAC 消息认证。

local hash = require("hash")
local hex = hash.md5("data")
local raw = hash.md5("data", true)
参数类型描述
datastring要哈希的数据
rawboolean?返回原始字节而非十六进制

返回值: string, error

local hex = hash.sha1("data")
local raw = hash.sha1("data", true)
参数类型描述
datastring要哈希的数据
rawboolean?返回原始字节而非十六进制

返回值: string, error

local hex = hash.sha256("data")
local raw = hash.sha256("data", true)
参数类型描述
datastring要哈希的数据
rawboolean?返回原始字节而非十六进制

返回值: string, error

local hex = hash.sha512("data")
local raw = hash.sha512("data", true)
参数类型描述
datastring要哈希的数据
rawboolean?返回原始字节而非十六进制

返回值: string, error

local hex = hash.hmac_md5("message", "secret")
local raw = hash.hmac_md5("message", "secret", true)
参数类型描述
datastring要认证的消息
secretstring密钥
rawboolean?返回原始字节而非十六进制

返回值: string, error

local hex = hash.hmac_sha1("message", "secret")
local raw = hash.hmac_sha1("message", "secret", true)
参数类型描述
datastring要认证的消息
secretstring密钥
rawboolean?返回原始字节而非十六进制

返回值: string, error

local hex = hash.hmac_sha256("message", "secret")
local raw = hash.hmac_sha256("message", "secret", true)
参数类型描述
datastring要认证的消息
secretstring密钥
rawboolean?返回原始字节而非十六进制

返回值: string, error

local hex = hash.hmac_sha512("message", "secret")
local raw = hash.hmac_sha512("message", "secret", true)
参数类型描述
datastring要认证的消息
secretstring密钥
rawboolean?返回原始字节而非十六进制

返回值: string, error

用于哈希表和分区的快速哈希。

local n = hash.fnv32("data")
参数类型描述
datastring要哈希的数据

返回值: number, error

具有更大输出以减少冲突的快速哈希。

local n = hash.fnv64("data")
参数类型描述
datastring要哈希的数据

返回值: number, error

条件类型可重试
输入不是字符串errors.INVALID
密钥不是字符串(HMAC)errors.INVALID

参见 错误处理 了解如何处理错误。