xMagic- 仓湖云函数数字应用平台
首页
🍓 函数引擎
更多产品
更多产品
  • 微澜组网
首页
🍓 函数引擎
更多产品
更多产品
  • 微澜组网
  1. xScript - x16.25.11
  • xMagic - 仓湖云函数数字应用平台
  • xScript - 仓湖云函数数字应用引擎
  • xMagic - 二开应用场景案例
  • 数据应用
    • 消息订阅(WSS)
    • 消息订阅(SSE)
    • 用户信息
    • 免密登陆(短信)
  • xScript - x16.25.11
    • assert
    • buffer
    • color
    • compress
    • console
    • crontab
    • crypto
    • encoding
    • events
    • evenloop
    • esm
    • fetch
    • fs
    • image
    • kv
    • mail
    • ml
    • mqtt
    • multipart
    • net
    • os
    • path
    • process
    • protobuf
    • querystring
    • redis
    • request
    • require
    • sql
    • tty
    • tui
    • ua
    • url
    • util
    • websocket
    • xml
    • yaml
    • mongo
    • http
    • stream
    • runtime
  • 小工具
    • APS - 代理转发
      • Any Proxy Service - 高级 HTTP/HTTPS/gRPC 代理转发工具
      • 配置指南 - 双向 Headers 和 Match/Replace
  • 数据模型
    • Schemas
      • AdminChangeAvatarReq
      • casbin.CasbinRule
      • AdminDelBatchReq
      • entity.DeptLable
      • AdminDelReq
      • entity.DevGenTable
      • AdminListReq
      • entity.DevGenTableColumn
      • AdminLoginReq
      • entity.FlowWorkClassify
      • AdminPostReq
      • entity.FlowWorkInfo
      • AdminReply
      • entity.FlowWorkTemplates
      • AdminUpdatePwdReq
      • entity.LogLogin
      • MenuDelBatchReq
      • entity.LogOper
      • MenuDelReq
      • entity.LoginM
      • MenuListReq
      • entity.MenuLable
      • MenuPostReq
      • entity.MenuPath
      • MenuReply
      • entity.ResEmail
      • RoleApisReq
      • entity.ResOss
      • SystemApiDelBatchReq
      • entity.ScreenGroupLabel
      • SystemApiDelReq
      • entity.SysApi
      • SystemApiListReq
      • entity.SysConfig
      • SystemApiPostReq
      • entity.SysDept
      • SystemApiReply
      • entity.SysDictData
      • SystemDepartmentDelBatchReq
      • entity.SysDictType
      • SystemDepartmentDelReq
      • entity.SysJob
      • SystemDepartmentListReq
      • entity.SysMenu
      • SystemDepartmentPostReq
      • entity.SysNotice
      • SystemDepartmentReply
      • entity.SysPost
      • SystemRoleApiDelBatchReq
      • entity.SysRole
      • SystemRoleApiDelReq
      • entity.SysTenants
      • SystemRoleApiListReq
      • entity.SysUser
      • SystemRoleApiPostReq
      • entity.SysUserB
      • SystemRoleApiReply
      • entity.SysUserId
      • SystemRoleDelBatchReq
      • entity.SysUserView
      • SystemRoleDelReq
      • entity.VisualRuleChain
      • SystemRoleListReq
      • entity.VisualScreen
      • SystemRoleMenuAddReq
      • entity.VisualScreenGroup
      • SystemRoleMenuDelBatchReq
      • form.Login
      • SystemRoleMenuDelReq
      • from.SendMail
      • SystemRoleMenuListReq
      • map[string]interface {}
      • SystemRoleMenuPostReq
      • map[string]string
      • SystemRoleMenuReply
      • model.BaseAutoModel
      • SystemRolePostReq
      • model.BaseModel
      • SystemRoleReply
      • model.ResultPage
      • model.ResultPage.data
      • vo.AuthVo
      • vo.DeptTreeVo
      • vo.MenuTreeVo
      • vo.MetaVo
      • vo.RouterVo
      • vo.TableInfoVo
      • vo.TokenVo
      • vo.UserRolePost
      • vo.UserVo
      • vo.UserVo.data
首页
🍓 函数引擎
更多产品
更多产品
  • 微澜组网
首页
🍓 函数引擎
更多产品
更多产品
  • 微澜组网
  1. xScript - x16.25.11

encoding

Crypto 模块#

Crypto 模块为 XScript 提供全面的加密解密功能,支持国密算法(SM2/SM3/SM4)、RSA、AES、DES、RC4、ECDSA、ED25519 等多种加密标准和哈希算法。

功能特性#

国密算法 - SM2 非对称加密/签名/共享密钥、SM3 哈希、SM4 对称加密
对称加密 - AES-128/192/256(CBC/ECB/CFB/CTR/OFB/GCM)、3DES、RC4、SM4
非对称加密 - RSA、SM2、ECDSA、ED25519
哈希算法 - MD5、SHA-1/224/256/384/512、SHA3、SM3、Blake2b/Blake2s、Keccak256、RIPEMD160
消息认证 - HMAC(支持 MD5/SHA1/SHA2/SHA3)
密码哈希 - Bcrypt
证书解析 - X.509 证书解析
SSL 密钥对生成 - 支持 RSA/SM2/ECDSA/ED25519

基本使用#

引入模块#

哈希计算#

Node.js 兼容的 createHash 方法(推荐)#

xScript 提供与 Node.js 完全兼容的 crypto.createHash() API,支持链式调用和多种哈希算法。
支持的算法:
md5 - MD5 哈希
sha1 - SHA-1 哈希
sha224, sha256, sha384, sha512 - SHA-2 系列
sha3-224, sha3-256, sha3-384, sha3-512 - SHA-3 系列
sm3 - 国密 SM3 哈希
blake2b512, blake2b - Blake2b-512 哈希
blake2s256, blake2s - Blake2s-256 哈希
ripemd160, ripemd, rmd160 - RIPEMD-160 哈希
keccak256, keccak - Keccak-256 哈希

xScript 原生哈希方法#

哈希函数返回 Buffer 对象,支持多种转换方法:
.toString() - 转换为字符串
.toHEX - 转换为十六进制字符串(返回 Buffer 对象,需配合 .encoding() 使用)
.toBase64 - 转换为 Base64 字符串(返回 Buffer 对象,需配合 .encoding() 使用)

HMAC 消息认证码#

Bcrypt 密码哈希#

AES 对称加密#

AES 支持多种加密模式:CBC、ECB、CFB、CTR、OFB、GCM

3DES/DES 加密#

RC4 流加密#

SM4 国密对称加密#

RSA 非对称加密#

RSA 数字签名#

RSA SSL 密钥对生成#

SM2 国密非对称加密#

SM2 数字签名#

SM2 共享密钥生成#

SM2 SSL 密钥对生成#

ECDSA 签名验证#

ECDSA 共享密钥生成#

ECDSA SSL 密钥对生成#

ED25519 签名验证#

ED25519 SSL 密钥对生成#

X.509 证书解析#

API 参考#

Node.js 兼容 API#

crypto.createHash(algorithm)#

创建并返回一个 Hash 对象,可以使用给定的算法生成哈希摘要。此方法与 Node.js 的 crypto.createHash() 完全兼容。
参数:
algorithm (string) - 哈希算法名称,支持以下算法:
'md5' - MD5 哈希(128位)
'sha1' - SHA-1 哈希(160位)
'sha224' - SHA-224 哈希(224位)
'sha256' - SHA-256 哈希(256位)
'sha384' - SHA-384 哈希(384位)
'sha512' - SHA-512 哈希(512位)
'sha3-224' - SHA3-224 哈希(224位)
'sha3-256' - SHA3-256 哈希(256位)
'sha3-384' - SHA3-384 哈希(384位)
'sha3-512' - SHA3-512 哈希(512位)
'sm3' - 国密 SM3 哈希(256位)
'blake2b512', 'blake2b' - Blake2b-512 哈希(512位)
'blake2s256', 'blake2s' - Blake2s-256 哈希(256位)
'ripemd160', 'ripemd', 'rmd160' - RIPEMD-160 哈希(160位)
'keccak256', 'keccak' - Keccak-256 哈希(256位)
返回: (Hash) Hash 对象,具有以下方法:
hash.update(data[, inputEncoding]) - 使用给定的数据更新哈希内容。可以多次调用以处理流式数据。返回 hash 对象自身以支持链式调用。
data (string|Buffer) - 要哈希的数据
inputEncoding (string) - 可选,输入编码(当前版本自动处理)
hash.digest([encoding]) - 计算所有传入数据的哈希摘要。调用后不能再使用 update() 方法。
encoding (string) - 可选,输出编码:
'hex' - 十六进制字符串(默认)
'base64' - Base64 字符串
返回: (string) 编码后的哈希值
示例:

对称加密 API#

crypto.aes.encrypt(mode, key, plaintext, iv)#

AES 加密。
参数:
mode (string) - 加密模式: 'CBC', 'ECB', 'CFB', 'CTR', 'OFB', 'GCM'
key (string|Buffer) - 密钥(支持 16/24/32 字节,对应 AES-128/192/256)
plaintext (string) - 明文数据
iv (string|Buffer) - 初始化向量(16字节),某些模式下可选
返回: (Buffer) Buffer 对象,支持 .toString() 方法,以及 .toHEX, .toBase64 等编码方法(需配合 .encoding() 使用)

crypto.aes.decrypt(mode, key, ciphertext, iv)#

AES 解密。
参数:
mode (string) - 加密模式
key (string|Buffer) - 密钥
ciphertext (object|string) - 密文
iv (string|Buffer) - 初始化向量
返回: (Buffer) Buffer 对象

crypto.tripleDES.encrypt(key, plaintext, iv)#

3DES/DES 加密(CBC 模式)。
参数:
key (string) - 密钥(24字节用于3DES,8字节用于DES)
plaintext (string) - 明文数据
iv (string) - 初始化向量(8字节)
返回: (Buffer) Buffer 对象

crypto.tripleDES.decrypt(key, ciphertext, iv)#

3DES/DES 解密(CBC 模式)。
参数:
key (string) - 密钥
ciphertext (Buffer|string) - 密文
iv (string) - 初始化向量
返回: (Buffer) Buffer 对象

crypto.rc4(key, data)#

RC4 流加密/解密(加密和解密使用同一函数)。
参数:
key (string) - 密钥
data (Buffer|object) - 要加密/解密的数据
返回: (Buffer) Buffer 对象

crypto.sm4.encrypt(plaintext, key)#

SM4 国密加密。
参数:
plaintext (string) - 明文数据
key (string|Buffer) - 密钥(16字节/128位)
返回: (Buffer) Buffer 对象

crypto.sm4.decrypt(ciphertext, key)#

SM4 国密解密。
参数:
ciphertext (object|string) - 密文
key (string|Buffer) - 密钥(16字节/128位)
返回: (Buffer) Buffer 对象

非对称加密 API#

crypto.rsa.new()#

生成 RSA 密钥对。
返回: (object) { publicKey, privateKey } 密钥对对象,如果失败返回 null

crypto.rsa.encrypt(publicKey, data)#

RSA 公钥加密。
参数:
publicKey (string) - PEM 格式公钥
data (string) - 明文数据
返回: (string) 加密后的密文,如果失败返回 null

crypto.rsa.decrypt(privateKey, ciphertext)#

RSA 私钥解密。
参数:
privateKey (string) - PEM 格式私钥
ciphertext (string) - 密文
返回: (Buffer) 解密后的明文数据,如果失败返回 null

crypto.rsa.sign(privateKey, data)#

RSA 签名。
参数:
privateKey (string) - PEM 格式私钥
data (string) - 要签名的数据
返回: (string) 签名结果,如果失败返回 null

crypto.rsa.verify(publicKey, data, signature)#

RSA 验签。
参数:
publicKey (string) - PEM 格式公钥
data (string) - 原始数据
signature (string) - 签名
返回: (boolean) 验证结果,true 表示签名有效

crypto.rsa.ssl()#

生成 RSA SSL 密钥对(用于 TLS/SSL 证书)。
返回: (object) { publicKey, privateKey } SSL 密钥对对象,如果失败返回 null

SM2 国密 API#

crypto.sm2.new()#

生成 SM2 密钥对。
返回: (object) { publicKey, privateKey } 密钥对对象,如果失败返回 null

crypto.sm2.encrypt(publicKey, data)#

SM2 公钥加密。
参数:
publicKey (string) - SM2 公钥
data (string) - 明文数据
返回: (string) 加密后的密文,如果失败返回 null

crypto.sm2.decrypt(privateKey, ciphertext)#

SM2 私钥解密。
参数:
privateKey (string) - SM2 私钥
ciphertext (string) - 密文
返回: (Buffer) 解密后的明文数据,如果失败返回 null

crypto.sm2.sign(privateKey, data)#

SM2 签名。
参数:
privateKey (string) - SM2 私钥
data (string) - 要签名的数据
返回: (string) 签名结果,如果失败返回 null

crypto.sm2.verify(publicKey, data, signature)#

SM2 验签。
参数:
publicKey (string) - SM2 公钥
data (string) - 原始数据
signature (string) - 签名
返回: (boolean) 验证结果

crypto.sm2.shareKey(privateKey, publicKey)#

生成 SM2 共享密钥(用于密钥协商)。
参数:
privateKey (string) - 己方 SM2 私钥
publicKey (string) - 对方 SM2 公钥
返回: (string) 共享密钥

crypto.sm2.ssl()#

生成 SM2 SSL 密钥对。
返回: (object) { publicKey, privateKey } SSL 密钥对对象

ECDSA API#

crypto.ecdsa.new()#

生成 ECDSA 密钥对。
返回: (object) { publicKey, privateKey } 密钥对对象,如果失败返回 null

crypto.ecdsa.sign(privateKey, data)#

ECDSA 签名。
参数:
privateKey (string) - ECDSA 私钥
data (string) - 要签名的数据
返回: (string) 签名结果,如果失败返回 null

crypto.ecdsa.verify(publicKey, data, signature)#

ECDSA 验签。
参数:
publicKey (string) - ECDSA 公钥
data (string) - 原始数据
signature (string) - 签名
返回: (boolean) 验证结果

crypto.ecdsa.shareKey(privateKey, publicKey)#

生成 ECDSA 共享密钥。
参数:
privateKey (string) - 己方 ECDSA 私钥
publicKey (string) - 对方 ECDSA 公钥
返回: (string) 共享密钥

crypto.ecdsa.ssl()#

生成 ECDSA SSL 密钥对。
返回: (object) { publicKey, privateKey } SSL 密钥对对象

ED25519 API#

crypto.ed25519.new()#

生成 ED25519 密钥对。
返回: (object) { publicKey, privateKey } 密钥对对象,如果失败返回 null

crypto.ed25519.sign(privateKey, data)#

ED25519 签名。
参数:
privateKey (string) - ED25519 私钥
data (string) - 要签名的数据
返回: (string) 签名结果,如果失败返回 null

crypto.ed25519.verify(publicKey, data, signature)#

ED25519 验签。
参数:
publicKey (string) - ED25519 公钥
data (string) - 原始数据
signature (string) - 签名
返回: (boolean) 验证结果

crypto.ed25519.ssl()#

生成 ED25519 SSL 密钥对。
返回: (object) { publicKey, privateKey } SSL 密钥对对象

哈希 API#

crypto.hash.md5(data)#

计算 MD5 哈希值。
参数:
data (string|Buffer) - 要哈希的数据
返回: (Buffer) Buffer 对象,支持 .toString(), .toBase64, .toHEX 等方法

crypto.hash.sha1(bits, data)#

计算 SHA-1 哈希值。
参数:
bits (number) - 位数(通常为 160)
data (string|Buffer) - 要哈希的数据
返回: (Buffer) Buffer 对象

crypto.hash.sha2(bits, data)#

计算 SHA-2 系列哈希值。
参数:
bits (number) - 位数,支持 224, 256
data (string|Buffer) - 要哈希的数据
返回: (Buffer) Buffer 对象

crypto.hash.sha3(bits, data)#

计算 SHA-3 系列哈希值。
参数:
bits (number) - 位数,支持 224, 256, 384, 512
data (string|Buffer) - 要哈希的数据
返回: (Buffer) Buffer 对象

crypto.hash.sm3(bits, data)#

计算国密 SM3 哈希值。
参数:
bits (number) - 位数(通常为 256)
data (string|Buffer) - 要哈希的数据
返回: (Buffer) Buffer 对象

crypto.hash.blake2b(data)#

计算 Blake2b 哈希值。
参数:
data (string|Buffer) - 要哈希的数据
返回: (Buffer) Buffer 对象

crypto.hash.blake2s(data)#

计算 Blake2s 哈希值。
参数:
data (string|Buffer) - 要哈希的数据
返回: (Buffer) Buffer 对象

crypto.hash.keccak256(bits, data)#

计算 Keccak256 哈希值。
参数:
bits (number) - 位数(通常为 256)
data (string|Buffer) - 要哈希的数据
返回: (Buffer) Buffer 对象

crypto.hash.ripemd160(bits, data)#

计算 RIPEMD160 哈希值。
参数:
bits (number) - 位数(通常为 160)
data (string|Buffer) - 要哈希的数据
返回: (Buffer) Buffer 对象

HMAC API#

crypto.hash.hmac.raw(data, key, hashFunc)#

通用 HMAC 计算函数。
参数:
data (string) - 要认证的数据
key (string) - 密钥
hashFunc (function) - 哈希函数
返回: (Buffer) Buffer 对象

crypto.hash.hmac.md5(data, key)#

计算 HMAC-MD5 消息认证码。
参数:
data (string) - 要认证的数据
key (string) - 密钥
返回: (Buffer) Buffer 对象

crypto.hash.hmac.sha1(data, key)#

计算 HMAC-SHA1 消息认证码。
参数:
data (string) - 要认证的数据
key (string) - 密钥
返回: (Buffer) Buffer 对象

crypto.hash.hmac.sha2(bits, data, key)#

计算 HMAC-SHA2 系列消息认证码。
参数:
bits (number) - 位数,支持 224, 256
data (string) - 要认证的数据
key (string) - 密钥
返回: (Buffer) Buffer 对象

crypto.hash.hmac.sha3(bits, data, key)#

计算 HMAC-SHA3 系列消息认证码。
参数:
bits (number) - 位数,支持 224, 256, 384, 512
data (string) - 要认证的数据
key (string) - 密钥
返回: (Buffer) Buffer 对象

Bcrypt API#

crypto.bcrypt.encrypt(password)#

使用 Bcrypt 算法对密码进行哈希加密。
参数:
password (string) - 要加密的密码
返回: (string) Bcrypt 哈希值

crypto.bcrypt.diff(password, hashedPassword)#

验证密码与 Bcrypt 哈希值是否匹配。
参数:
password (string) - 原始密码
hashedPassword (string) - Bcrypt 哈希值
返回: (boolean) 验证结果,true 表示匹配

X.509 证书 API#

crypto.x509.parse(certPEM)#

解析 X.509 证书。
参数:
certPEM (string) - PEM 格式的证书字符串
返回: (object) 证书信息对象,包含 subject、issuer、notBefore、notAfter 等字段

注意事项#

1.
密钥管理 - 妥善保管私钥,不要硬编码在代码中
2.
算法选择 - 金融行业优先使用国密算法,国际业务使用 RSA/AES
3.
密钥长度 - RSA 至少 2048 位,AES 至少 128 位
4.
IV 随机性 - 每次加密使用不同的初始化向量
5.
Bcrypt - 用于密码存储时优先使用 Bcrypt,不要使用 MD5 或 SHA1
6.
SSL 密钥对 - 用于 TLS/SSL 证书生成时使用对应的 ssl() 方法
7.
共享密钥 - SM2 和 ECDSA 支持共享密钥生成,用于密钥协商场景
8.
Buffer 对象 - 加密和哈希函数返回 Buffer 对象,支持 .toString() 直接转换,或使用 .toHEX.encoding() 等链式调用
修改于 2025-11-05 13:03:38
上一页
crypto
下一页
events
Built with