加密

crypt.hash

使用所选算法对字符串进行哈希处理并返回摘要。

语法

crypt.hash(data: string, algorithm?: "sha256" | "sha1" | "sha224" | "sha384" | "sha512" | "md5" | "sha3-224" | "sha3-256" | "sha3-384" | "sha3-512"): string

参数

名称类型描述
datastring输入由函数处理的字节字符串或文本。
algorithm?"sha256" | "sha1" | "sha224" | "sha384" | "sha512" | "md5" | "sha3-224" | "sha3-256" | "sha3-384" | "sha3-512"哈希算法。默认为 sha256。

返回值

名称类型描述
digeststring小写十六进制哈希摘要。

说明

使用所选算法对字符串进行哈希处理并返回摘要。

使用 2 个参数调用: data, algorithm。参数表会说明哪些值必填,哪些值只用于细化行为。

返回 digest (string)。使用返回表区分成功值、nil 结果和可恢复错误。

示例

为字符串创建 SHA-256 摘要。

local digest = crypt.hash("hello world", "sha256")
print(digest)