app.crypto - 加密编码 API
哈希、加密、编码操作。
哈希
app.crypto.md5(data)
计算 MD5 哈希。
参数:
data(string) - 要哈希的数据
返回值: string - 十六进制哈希值
local hash = app.crypto.md5("hello world")
-- "5eb63bbbe01eeed093cb22bb8f5acdc3"
app.crypto.sha1(data)
计算 SHA-1 哈希。
local hash = app.crypto.sha1("hello world")
app.crypto.sha256(data)
计算 SHA-256 哈希。
local hash = app.crypto.sha256("hello world")
app.crypto.sha512(data)
计算 SHA-512 哈希。
local hash = app.crypto.sha512("hello world")
文件哈希
app.crypto.md5File(path)
计算文件 MD5 哈希。
参数:
path(string) - 文件路径
返回值: string|nil - 哈希值
local hash = app.crypto.md5File("/path/to/file.zip")
app.crypto.sha1File(path)
计算文件 SHA-1 哈希。
local hash = app.crypto.sha1File("/path/to/file.zip")
app.crypto.sha256File(path)
计算文件 SHA-256 哈希。
local hash = app.crypto.sha256File("/path/to/file.zip")
编码
app.crypto.base64Encode(data)
Base64 编码。
返回值: string - Base64 字符串
local encoded = app.crypto.base64Encode("hello world")
-- "aGVsbG8gd29ybGQ="
app.crypto.base64Decode(data)
Base64 解码。
返回值: string|nil - 解码后的数据
local decoded = app.crypto.base64Decode("aGVsbG8gd29ybGQ=")
-- "hello world"
app.crypto.hexEncode(data)
十六进制编码。
local hex = app.crypto.hexEncode("hello")
-- "68656c6c6f"
app.crypto.hexDecode(data)
十六进制解码。
local decoded = app.crypto.hexDecode("68656c6c6f")
-- "hello"
app.crypto.urlEncode(data)
URL 编码。
local encoded = app.crypto.urlEncode("hello world")
-- "hello%20world"
app.crypto.urlDecode(data)
URL 解码。
local decoded = app.crypto.urlDecode("hello%20world")
-- "hello world"
HMAC
app.crypto.hmacSHA256(data, key)
计算 HMAC-SHA256。
参数:
data(string) - 数据key(string) - 密钥
返回值: string - 十六进制 HMAC 值
local hmac = app.crypto.hmacSHA256("message", "secret_key")
app.crypto.hmacSHA512(data, key)
计算 HMAC-SHA512。
local hmac = app.crypto.hmacSHA512("message", "secret_key")
app.crypto.hmacSHA1(data, key)
计算 HMAC-SHA1(since 1.3.0)。常用于 AWS Signature V2、OAuth 1.0 等历史协议。
local hmac = app.crypto.hmacSHA1("message", "secret_key")
密钥派生
app.crypto.pbkdf2(password, salt, iterations, keyLength)
使用 PBKDF2-HMAC-SHA256 从密码派生密钥(since 1.3.0)。
参数:
password(string) - 明文密码salt(string) - 盐值iterations(number) - 迭代次数(建议 ≥ 100000)keyLength(number) - 输出密钥字节长度
返回值: string - 十六进制密钥
local key = app.crypto.pbkdf2("password", "salt", 100000, 32)
ECDSA 签名(P-256)
app.crypto.ecdsaSign(data, privateKeyPEM)
使用 P-256 私钥签名(since 1.3.0)。
参数:
data(string) - 待签名数据privateKeyPEM(string) - PEM 格式的 P-256 私钥
返回值: string - Base64 编码的签名
local sig = app.crypto.ecdsaSign("message", privateKeyPEM)
app.crypto.ecdsaVerify(data, signature, publicKeyPEM)
验证 P-256 签名(since 1.3.0)。
参数:
data(string) - 原始数据signature(string) - Base64 编码的签名publicKeyPEM(string) - PEM 格式的 P-256 公钥
返回值: boolean - 验签是否通过
local ok = app.crypto.ecdsaVerify("message", sig, publicKeyPEM)
HTML 实体
app.crypto.htmlEncode(s)
HTML 实体编码(since 1.3.0)。转义 < > & " '。
app.crypto.htmlEncode('<b>"hi"&friends</b>')
-- "<b>"hi"&friends</b>"
app.crypto.htmlDecode(s)
HTML 实体解码(since 1.3.0)。支持命名实体和数字实体。
app.crypto.htmlDecode("<b>hi</b>") -- "<b>hi</b>"
Base64 URL
app.crypto.base64UrlEncode(data)
URL 安全的 Base64 编码(since 1.3.0)。将 +/ 替换为 -_,并去掉 = 填充。常用于 JWT。
app.crypto.base64UrlEncode("??>>") -- 不含 +/=
app.crypto.base64UrlDecode(data)
URL 安全的 Base64 解码(since 1.3.0)。
local s = app.crypto.base64UrlDecode(encoded)
AES 加解密
app.crypto.aesEncrypt(data, key, options?)
AES 加密。
参数:
data(string) - 要加密的数据key(string) - 密钥(自动填充/截断到对应长度)options(table, 可选):mode(string) - 模式:cbc(默认)、ecb、gcmiv(string) - 初始化向量(不指定则自动生成)keySize(number) - 密钥长度:128、192、256(默认)
返回值: string|nil - 加密后的数据(Base64,CBC 模式包含 IV)
local encrypted = app.crypto.aesEncrypt("secret data", "my-secret-key")
-- 指定模式和 IV
local encrypted = app.crypto.aesEncrypt("secret data", "my-secret-key", {
mode = "cbc",
iv = "16-byte-iv-here!"
})
app.crypto.aesDecrypt(encrypted, key, options?)
AES 解密。
参数:
encrypted(string) - 加密数据(Base64)key(string) - 密钥options(table, 可选) - 同加密选项
返回值: string|nil - 解密后的数据
local decrypted = app.crypto.aesDecrypt(encrypted, "my-secret-key")
RSA 加解密
app.crypto.rsaGenerateKeyPair(bits?)
生成 RSA 密钥对。
参数:
bits(number, 可选) - 密钥长度(默认 2048)
返回值: table|nil - {publicKey, privateKey} PEM 格式
local keys = app.crypto.rsaGenerateKeyPair(2048)
if keys then
app.log.info("公钥:\n" .. keys.publicKey)
app.log.info("私钥:\n" .. keys.privateKey)
end
app.crypto.rsaEncrypt(data, publicKey)
RSA 公钥加密(OAEP-SHA256)。
返回值: string|nil - Base64 编码的密文
local encrypted = app.crypto.rsaEncrypt("secret", keys.publicKey)
app.crypto.rsaDecrypt(encrypted, privateKey)
RSA 私钥解密。
返回值: string|nil - 明文
local decrypted = app.crypto.rsaDecrypt(encrypted, keys.privateKey)
app.crypto.rsaSign(data, privateKey)
RSA 签名(PKCS1v15-SHA256)。
返回值: string|nil - Base64 编码的签名
local signature = app.crypto.rsaSign("message", keys.privateKey)
app.crypto.rsaVerify(data, signature, publicKey)
RSA 验签。
返回值: boolean - 是否验证通过
local valid = app.crypto.rsaVerify("message", signature, keys.publicKey)
示例
计算文件校验和
function MyPlugin:handleChecksum(context)
local results = {}
for _, file in ipairs(context.selectedFiles) do
local hash = app.crypto.sha256File(file)
if hash then
table.insert(results, hash .. " " .. app.path.basename(file))
end
end
local report = table.concat(results, "\n")
app.clipboard.setText(report)
app.notification.show("完成", "校验和已复制到剪贴板")
end
加密配置文件
function MyPlugin:saveEncryptedConfig(config)
local json = app.json.stringify(config)
local key = app.crypto.sha256("my-app-secret"):sub(1, 32)
local encrypted = app.crypto.aesEncrypt(json, key)
if encrypted then
app.file.write(self.configPath, encrypted)
end
end
function MyPlugin:loadEncryptedConfig()
local encrypted = app.file.read(self.configPath)
if not encrypted then return {} end
local key = app.crypto.sha256("my-app-secret"):sub(1, 32)
local json = app.crypto.aesDecrypt(encrypted, key)
if json then
return app.json.parse(json) or {}
end
return {}
end