crypto
小程序密码算法模块,支持摘要、Hmac、对称、非对称等常用算法。
数据类型`contentType`支持以下值:
base64
表示数据为base64串urlSafeBase64
表示网络传输安全的base64串hex
表示数据为十六进制串- 默认为
raw
表示UTF-8编码的普通字符串
密钥数据类型`keyType`,除特殊声明外,支持以下值:
base64
表示数据为base64串urlSafeBase64
表示网络传输安全的base64串- 默认为
hex
表示数据为十六进制串
向量数据类型`ivType`支持以下值:
base64
表示数据为base64串urlSafeBase64
表示网络传输安全的base64串- 默认为
hex
表示数据为十六进制串
返回数据类型`resultType`支持以下值:
base64
表示要求返回数据为base64串urlSafeBase64
表示网络传输安全的base64串- 默认为
hex
表示十六进制串
API
Base64算法
since v0.28.2
base64(options, callback) - @options {Object}: 算法参数
table {string}
:base64置换表,非必填forEncode {boolean}
:true表示base64编码,false表示base64解码padding {boolean}
:true表示有填充,false表示无填充data {string}
: 待计算数据contentType {string}
:数据类型resultType {string}
:返回数据类型
- @callback {Function}:执行完该操作后的回调函数。回调函数将收到一个Response对象
ok {boolean}
:true表示成功,false表示发生了错误data {string}
:返回数据,失败时返回错误描述
Demo
var crypto = weex.requireModule('crypto');
crypto.base64({
data: 'FEF161626FFE11',
forEncode: true,
contentType: 'hex'
}, (e) => {
if(e.ok) {
// your code here
}else {
modal.toast({message: e.data});
}
});
摘要算法
支持的算法
MD5, SHA1, SHA224, SHA256, SHA384, SHA512, RIPEMD160, SM3, SHA3-224, SHA3-256, SHA3-384, SHA3-512, SHAKE128-128, SHAKE128-160, SHAKE128-224, SHAKE128-256, SHAKE128-384, SHAKE128-512, SHAKE256-128, SHAKE256-160, SHAKE256-224, SHAKE256-256, SHAKE256-384, SHAKE256-512
since v0.28.2
digest(options, callback) - @options {Object}: 算法参数
algorithm {string}
:算法名称data {string}
:待计算数据contentType {string}
:数据类型resultType {string}
:返回数据类型
- @callback {Function}:执行完该操作后的回调函数。回调函数将收到一个Response对象
ok {boolean}
:true表示成功,false表示发生了错误data {string}
:返回数据,失败时返回错误描述
Demo
var crypto = weex.requireModule('crypto');
crypto.digest({
algorithm: 'MD5',
data: 'abc',
contentType: 'raw',
resultType: 'hex'
}, (e) => {
if(e.ok) {
// your code here
}else {
modal.toast({message: e.data});
}
});
对称加解密算法
支持的算法
AES, DES, DESede, Blowfish, Camellia, Aria, SM4
支持的加密模式
ECB, CBC
支持的填充模式
NoPadding, PKCS7Padding
since v0.28.2
symmetric(options, callback) - @options {Object}: 算法参数
algorithm {string}
:算法名称forEncryption {boolean}
:true表示加密,false表示解密key {string}
:密钥data {string}
:待计算数据iv {string}
:向量数据keyType {string}
:密钥数据类型contentType {string}
:数据类型ivType {string}
:向量数据类型resultType {string}
:返回数据类型
- @callback {Function}:执行完该操作后的回调函数。回调函数将收到一个Response对象
ok {boolean}
:true表示成功,false表示发生了错误data {string}
:返回数据,失败时返回错误描述
Demo
var crypto = weex.requireModule('crypto');
crypto.symmetric({
algorithm: 'AES/CBC/PKCS7Padding',
key: '6B8B4567327B23C6643C986966334873',
iv: '56097B7E240371A01290986E00C171E2',
forEncryption: true,
data: '616263',
keyType: 'hex',
ivType: 'hex',
contentType: 'hex',
resultType: 'hex'
}, (e) => {
if(e.ok) {
// your code here
}else {
modal.toast({message: e.data});
}
});
Hmac算法
支持的算法
HmacMD5, HmacSHA1, HmacSHA224, HmacSHA256, HmacSHA384, HmacSHA512
since v0.28.2
hmac(options, callback) - @options {Object}: 算法参数
algorithm {string}
:算法名称key {string}
:密钥data {string}
:待计算数据keyType {string}
:密钥数据类型contentType {string}
:数据类型resultType {string}
:返回数据类型
- @callback {Function}:执行完该操作后的回调函数。回调函数将收到一个Response对象
ok {boolean}
:true表示成功,false表示发生了错误data {string}
:返回数据,失败时返回错误描述
Demo
var crypto = weex.requireModule('crypto');
crypto.hmac({
algorithm: 'HmacMD5',
key: '313233343536',
data: '313233343536',
keyType: 'hex',
contentType: 'hex',
resultType: 'hex'
}, (e) => {
if(e.ok) {
// your code here
}else {
modal.toast({message: e.data});
}
});
DES其它算法
支持的算法
PbocDESMac, AnsiX9.9Mac, AnsiX9.19Mac, Diversify
其中Diversify为分散算法,其它为常用MAC算法
since v0.28.2
desExt(options, callback) - @options {Object}: 算法参数
algorithm {string}
:算法名称key {string}
:密钥data {string}
:待计算数据iv {string}
:向量数据 PbocDESMac必填keyType {string}
:密钥数据类型ivType {string}
:向量数据类型 PbocDESMac必填contentType {string}
:数据类型resultType {string}
:返回数据类型
- @callback {Function}:执行完该操作后的回调函数。回调函数将收到一个Response对象
ok {boolean}
:true表示成功,false表示发生了错误data {string}
:返回数据,失败时返回错误描述
Demo
var crypto = weex.requireModule('crypto');
crypto.desExt({
algorithm: 'PbocDESMac',
key: '6B8B4567327B23C6',
iv: '643C986966334873',
data: '616263',
keyType: 'hex',
ivType: 'hex',
contentType: 'hex',
resultType: 'hex'
}, (e) => {
if(e.ok) {
// your code here
}else {
modal.toast({message: e.data});
}
});
RSA密钥生成
since v0.28.2
genRSAKeypair(options, callback) - @options {Object}: 算法参数
strength {number}
:密钥长度,如1024publicExponent {number}
:公钥模,如0x03,默认为0x10001pemType {string}
:pem类型,支持PKCS#1及PKCS#8
- @callback {Function}:执行完该操作后的回调函数。回调函数将收到一个Response对象
ok {boolean}
:true表示成功,false表示发生了错误pemPublic {string}
:公钥pemPrivate {string}
:私钥n {string}
:moduluse {string}
:public exponentd {string}
:private exponentp {string}
:Prime Pq {string}
:Prime Qdp {string}
:Prime Exponent Pdq {string}
:Prime Exponent Qqinv {string}
:Crt Coefficient
Demo
var crypto = weex.requireModule('crypto');
crypto.genRSAKeypair({
strength: 2048,
publicExponent: 3,
pemType: 'PKCS#1',
}, (e) => {
if(e.ok) {
// your code here
}else {
modal.toast({message: e.data});
}
});
SM2密钥生成
返回密钥为十六进制串
since v0.28.2
genSM2Keypair(options, callback) - @options {Object}: 算法参数
- @callback {Function}:执行完该操作后的回调函数。回调函数将收到一个Response对象
ok {boolean}
:true表示成功,false表示发生了错误privateKey {string}
:私钥publicKey {string}
:公钥compressedPublicKey {string}
:压缩公钥
Demo
var crypto = weex.requireModule('crypto');
crypto.genSM2Keypair({
}, (e) => {
if(e.ok) {
// your code here
}else {
modal.toast({message: e.data});
}
});
RSA加解密
支持的算法
RSA/None/NoPadding,
RSA/None/PKCS1Padding,
RSA/None/OAEPWithMD5AndMGF1Padding,
RSA/None/OAEPWithSHA1AndMGF1Padding,
RSA/None/OAEPWithSHA224AndMGF1Padding,
RSA/None/OAEPWithSHA256AndMGF1Padding,
RSA/None/OAEPWithSHA384AndMGF1Padding,
RSA/None/OAEPWithSHA512AndMGF1Padding,
RSA/None/OAEPWithSHA3-224AndMGF1Padding,
RSA/None/OAEPWithSHA3-256AndMGF1Padding,
RSA/None/OAEPWithSHA3-384AndMGF1Padding,
RSA/None/OAEPWithSHA3-512AndMGF1Padding,
RSA/None/OAEPWithRIPEMD160AndMGF1Padding,
RSA/None/OAEPWithSM3AndMGF1Padding
since v0.28.2
rsa(options, callback) - @options {Object}: 算法参数
algorithm {string}
:算法名称pemkey {string}
:PKCS#1或PKCS#8的密钥forEncryption {boolean}
:true表示加密,false表示解密data {string}
:待计算数据contentType {string}
:数据类型resultType {string}
:返回数据类型
- @callback {Function}:执行完该操作后的回调函数。回调函数将收到一个Response对象
ok {boolean}
:true表示成功,false表示发生了错误data {string}
:返回数据,失败时返回错误描述
Demo
var crypto = weex.requireModule('crypto');
let pemKey = "-----BEGIN RSA PRIVATE KEY-----\n" +
"MIICXQIBAAKBgQC1d8wAeHL1FaSOi7DVCOGmvTHDcjpZHnqv8GbWB5g1S0LJnymN\n" +
"xuLIhTWzszOxiAqGET4rw3sltai7lcAGaBB2PZpY0CU+P8ppmC+8uTlAI0TdPLhm\n" +
"+WqGyyUu3VwwJR48/WhK/0bsUOyjwZi3CQe80TRfnG/EcgehH4GxLbqz5wIDAQAB\n" +
"AoGBAKcf9lR0mcLXtN7HDguVC2Spl5wdplkPNgS1DbCN/AMRFihkGjwFcDUmYafn\n" +
"IXOeC7sfRDe/57l6DTT9nIUJ8CW3iyTWaYJJel0VNO0RQikYUyIQtBhttaisiszp\n" +
"aAQpAYZEavre570nKUCHbnnrmaC93PFbfwdKinQ5BdMmD+UZAkEA5gnNvrN2VCE7\n" +
"uMKEuLNQ2xQT7E+cgjp3eOjuzZGD0VVXme4lnNSc1QIgOAcU9MuNzoWzXeUhDLPv\n" +
"dZnNu/uBfQJBAMnytrkLCJgL+ggVvx/vKtV3p6AHXnEA3A3g4jgbVqimDBvdIIdS\n" +
"u5gjByeP58YeTAyp6tD2awvGQqEn0Z0kCDMCQA+tB1pBfITLJvi2OLklbxMe0SS/\n" +
"YBj3xwB0TyGvEt6HBEs3EVUYn/9b/7oRsXnlDSrPraNuY8wrztuiuYRf5TkCQQCd\n" +
"oxVYyjESJr8sknUXY2TnLritJTNmOEqNls5fB5AUo1DuayTaHQ2MS0NpcV51eu7Y\n" +
"L8a5CLE0hrU6ANARvq+bAkAYXxf1CpMKjJAoT5Sx0jvCcQKZlS2fmoMfJjv/qy0h\n" +
"kmZro0KkK1TAB3QWUk3TPOe44YLk1/F13XWYeRIeg5g+\n" +
"-----END RSA PRIVATE KEY-----";
crypto.rsa({
algorithm: 'RSA/None/PKCS1Padding',
pemkey: pemKey,
forEncryption: true,
data: '616263',
keyType: 'hex',
contentType: 'hex',
resultType: 'hex'
}, (e) => {
if(e.ok) {
// your code here
}else {
modal.toast({message: e.data});
}
});
SM2加解密
支持的算法
SM2WithSM3/C1C2C3,
SM2WithSM3/C1C3C2,
SM2WithSHA256/C1C2C3,
SM2WithSHA256/C1C3C2,
SM2WithSHA3-256/C1C2C3,
SM2WithSHA3-256/C1C3C2,
SM2WithSHAKE128-256/C1C2C3,
SM2WithSHAKE128-256/C1C3C2,
SM2WithSHAKE256-256/C1C2C3,
SM2WithSHAKE256-256/C1C3C2
公钥加密,私钥解密。
since v0.28.2
sm2(options, callback) - @options {Object}: 算法参数
algorithm {string}
:算法名称key {string}
:公私钥,支持hex、base64及urlSafeBase64格式, 如果是公钥传压缩或非压缩公钥都可以forEncryption {boolean}
:true表示加密,false表示解密data {string}
:待计算数据keyType {string}
:密钥数据类型contentType {string}
:数据类型resultType {string}
:返回数据类型
- @callback {Function}:执行完该操作后的回调函数。回调函数将收到一个Response对象
ok {boolean}
:true表示成功,false表示发生了错误data {string}
:返回数据,失败时返回错误描述
Demo
var crypto = weex.requireModule('crypto');
crypto.sm2({
algorithm: 'SM2WithSM3/C1C2C3',
key: '020148E6AF89A0E132E4E7CDA26DF2C2AEB53B741FD00AE85C78CF6EBA13E939B1',
forEncryption: true,
data: '616263',
keyType: 'hex',
contentType: 'hex',
resultType: 'hex'
}, (e) => {
if(e.ok) {
// your code here
}else {
modal.toast({message: e.data});
}
});
RSA签名及验签
支持的算法
MD5WithRSA, SHA1WithRSA, SHA224WithRSA, SHA256WithRSA, SHA384WithRSA, SHA512WithRSA, SHA3-224WithRSA, SHA3-256WithRSA, SHA3-384WithRSA, SHA3-512WithRSA, RIPEMD160WithRSA, SM3WithRSA
MD5WithRSA/PSS, SHA1WithRSA/PSS, SHA224WithRSA/PSS, SHA256WithRSA/PSS, SHA384WithRSA/PSS, SHA512WithRSA/PSS, SHA3-224WithRSA/PSS, SHA3-256WithRSA/PSS, SHA3-384WithRSA/PSS, SHA3-512WithRSA/PSS, RIPEMD160WithRSA/PSS, SM3WithRSA/PSS
since v0.28.2
rsaSignOrVerify(options, callback) - @options {Object}: 算法参数
algorithm {string}
:算法名称pemkey {string}
:PKCS#1或PKCS#8的密钥forSigning {boolean}
:true表示签名,false表示验签data {string}
:待计算数据signData {string}
:签名数据,验签时有signDataType {string}
:签名数据类型,仅支持hex、base64及urlSafeBase64格式,验签时有contentType {string}
:数据类型resultType {string}
:返回数据类型
- @callback {Function}:执行完该操作后的回调函数。回调函数将收到一个Response对象
ok {boolean}
:true表示成功,false表示发生了错误data {string}
:返回数据,失败时返回错误描述
Demo
var crypto = weex.requireModule('crypto');
crypto.rsaSignOrVerify({
algorithm: 'MD5WithRSA',
pemkey: pemKey,
forSigning: true,
data: data,
keyType: 'hex',
contentType: 'hex',
resultType: 'hex'
}, (e) => {
if(e.ok) {
// your code here
}else {
modal.toast({message: e.data});
}
});
SM2签名及验签
支持的算法
SHAKE256-256WithSM2, SHAKE128-256WithSM2, SHA3-256WithSM2, SHA256WithSM2, SM3WithSM2
私钥签名,公钥验签。
since v0.28.2
sm2SignOrVerify(options, callback) - @options {Object}: 算法参数
algorithm {string}
:算法名称key {string}
:公私钥,支持hex、base64及urlSafeBase64格式, 如果是公钥传压缩或非压缩公钥都可以forSigning {boolean}
:true表示签名,false表示验签data {string}
:待计算数据signData {string}
:签名数据,验签时有signDataType {string}
:签名数据类型,仅支持hex、base64及urlSafeBase64格式,验签时有keyType {string}
:密钥数据类型contentType {string}
:数据类型resultType {string}
:返回数据类型
- @callback {Function}:执行完该操作后的回调函数。回调函数将收到一个Response对象
ok {boolean}
:true表示成功,false表示发生了错误data {string}
:返回数据,失败时返回错误描述
Demo
var crypto = weex.requireModule('crypto');
crypto.sm2SignOrVerify({
algorithm: 'SM3WithSM2',
key: '6B8B4567327B23C6643C98696633487374B0DC5119495CFF2AE8944A625558EC',
forSigning: true,
data: '616263',
keyType: 'hex',
contentType: 'hex',
resultType: 'hex'
}, (e) => {
if(e.ok) {
// your code here
}else {
modal.toast({message: e.data});
}
});