脚本引擎 API

加密与编码 API

runtime.crypto.* 提供 MD5、SHA-256、HMAC-SHA256 签名和 Base64 编解码工具。

当前:加密与编码 API
脚本引擎 API

加密与编码 API

runtime.crypto.* 提供 MD5、SHA-256、HMAC-SHA256 签名和 Base64 编解码工具。

概述

提供常见的加密哈希和编码工具,用于请求签名、数据校验、Base64 编解码等场景。所有方法均为同步调用。

方法列表

  • runtime.crypto.md5(text: string): string — 计算 MD5 哈希值(32 位十六进制小写字符串)。
  • runtime.crypto.sha256(text: string): string — 计算 SHA-256 哈希值(64 位十六进制小写字符串)。
  • runtime.crypto.hmacSha256(text: string, key: string): string — 计算 HMAC-SHA256 签名(64 位十六进制小写字符串)。
  • runtime.crypto.base64Encode(text: string): string — 将文本编码为 Base64 字符串。
  • runtime.crypto.base64Decode(text: string): string — 将 Base64 字符串解码为原始文本。

调用示例

// 请求签名
var timestamp = Date.now().toString();
var body = JSON.stringify({name: "test"});
var signStr = "POST\n/api/submit\n" + timestamp + "\n" + runtime.crypto.md5(body);
var signature = runtime.crypto.hmacSha256(signStr, "my_secret_key");
runtime.process.log("签名: " + signature);
// Base64 编解码
var encoded = runtime.crypto.base64Encode("你好世界");
runtime.process.log("Base64: " + encoded);
var decoded = runtime.crypto.base64Decode(encoded);
runtime.process.log("解码: " + decoded); // 输出: 你好世界
// 密码哈希
var password = "user_input_password";
var hash = runtime.crypto.sha256(password);
runtime.process.log("SHA-256: " + hash);
// 可用于与服务端比对密码哈希