crypto

小程序密码算法模块,支持摘要、Hmac、对称、非对称等常用算法。

数据类型`contentType`支持以下值:

  1. base64表示数据为base64串
  2. urlSafeBase64表示网络传输安全的base64串
  3. hex表示数据为十六进制串
  4. 默认为raw表示UTF-8编码的普通字符串

密钥数据类型`keyType`,除特殊声明外,支持以下值:

  1. base64表示数据为base64串
  2. urlSafeBase64表示网络传输安全的base64串
  3. 默认为hex表示数据为十六进制串

向量数据类型`ivType`支持以下值:

  1. base64表示数据为base64串
  2. urlSafeBase64表示网络传输安全的base64串
  3. 默认为hex表示数据为十六进制串

返回数据类型`resultType`支持以下值:

  1. base64表示要求返回数据为base64串
  2. urlSafeBase64表示网络传输安全的base64串
  3. 默认为hex表示十六进制串

API

Base64算法

base64(options, callback) since v0.28.2

  • @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

digest(options, callback) since v0.28.2

  • @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

symmetric(options, callback) since v0.28.2

  • @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

hmac(options, callback) since v0.28.2

  • @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算法

desExt(options, callback) since v0.28.2

  • @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密钥生成

genRSAKeypair(options, callback) since v0.28.2

  • @options {Object}: 算法参数
    • strength {number}:密钥长度,如1024
    • publicExponent {number}:公钥模,如0x03,默认为0x10001
    • pemType {string}:pem类型,支持PKCS#1及PKCS#8
  • @callback {Function}:执行完该操作后的回调函数。回调函数将收到一个Response对象
    • ok {boolean}:true表示成功,false表示发生了错误
    • pemPublic {string}:公钥
    • pemPrivate {string}:私钥
    • n {string}:modulus
    • e {string}:public exponent
    • d {string}:private exponent
    • p {string}:Prime P
    • q {string}:Prime Q
    • dp {string}:Prime Exponent P
    • dq {string}:Prime Exponent Q
    • qinv {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密钥生成

返回密钥为十六进制串

genSM2Keypair(options, callback) since v0.28.2

  • @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

rsa(options, callback) since v0.28.2

  • @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

公钥加密,私钥解密。

sm2(options, callback) since v0.28.2

  • @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

rsaSignOrVerify(options, callback) since v0.28.2

  • @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

私钥签名,公钥验签。

sm2SignOrVerify(options, callback) since v0.28.2

  • @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});
	}
});