兼容支持 uni-app/普通网页/微信小程序, 目前只测试了web和微信小程序, 其它平台有问题可以提issue
模块依赖 fucking-util 模块, 请预先安装
模块包含 uni-app 预编译代码
uni-app 开发者直接使用此模块
- 要使用
uni-app
模式开发, 使用此模块 - 不使用
uni-app
模式开发h5
或web
或mui
项目 (window自带crypto模块) 或node
环境使用fucking-util-signature
- 小程序使用
fucking-util-signature-uni
// 多选一
var Signature = require ( "fucking-util-signature-all" );
// var Signature = require ( "fucking-util-signature-uni" );
// var Signature = require ( "fucking-util-signature" );
// 生成公私钥 pkcs #8 (需要其它版本可以提issue)
let { publicKey, privateKey } = Signature.RSA.generateKeys ( );
console.log ( publicKey, privateKey );
// RSA对象
let rsa = new Signature.RSA ( );
// 设置公私钥
rsa.setPublicKey ( publicKey );
rsa.setPrivateKey ( privateKey );
let unsigned = 'hello world!';
// 字符串签名
let sign = rsa.sign ( unsigned, 'base64'/*默认值,可不传*/ );
console.log ( 'sign', sign );
// 签名验证
let verified = rsa.verify ( unsigned, sign, 'base64'/*default*/ );
console.log ( 'verified', verified );
let unencrypt = 'Unencrypt string';
// 字符串加密
let encrypted = rsa.encrypt ( unencrypt, 'base64'/*default*/ );
console.log ( 'encrypted', encrypted );
// 解密
let decrypted = rsa.decrypt ( encrypted, 'base64' );
console.log ( 'decrypted', decrypted );
let keys = Signature.RSA.generateKeys ( );
let signature = new Signature ( );
let rsa = signature.rsa ( );
// let md5 = signature.md5 ( );
// let sha256 = signature.sha256 ( );
let unsigned = 'hello world!';
let sign = rsa
.update ( unsigned ) // 更新源数据
.setPrivateKey ( keys.privateKey ) // 设置私钥
.digest ( 'base64'/*default*/ ); // 生成签名
console.log ( 'sign', sign );
let verified = rsa
.update ( unsigned ) // 更新源数据, 此处可以忽略
.setPublicKey ( keys.publicKey ) // 设置公钥
.verify ( sign, 'base64'/*default*/ ); // 验证签名
console.log ( 'verified', verified );
对象签名模式是先把对象转成querystring
, 对象内的json
内容会被转成JSONString
, 然后再加上签名类型字段, 有盐的话会加盐字段, 最后进行字符串签名.
let signature = new Signature ( );
// 默认的 formOptions, 可以不用设置
signature.formOptions = {
signKey: 'sign', // 放置签名的字段
signTypeKey: 'signType', // 放置签名类型的字段
signSaltKey: 'key', // 盐值字段
ignoreKeys: [ // 签名忽略的字段
'sign', 'key'
],
salt: "" // 盐
};
let signer = signature.sha256 ( );
// let signer = signature.rsa ( );
let form = { user: "1589235", userInfo: {
nickname: "Liping Ruan",
avatar: "@%FFOISJAFJZC"
} };
let { sign, querystring } = signer
.update ( form )
.form ( true ) // 设置 签名/验签 为对象签名模式
.digest ( 'hex' ); // 签名输出 16进制 字符串
console.log ( 'sign', sign );
let verified = sha256.verify ( sign, 'hex' );
console.log ( 'verified', verified );
let signature = new Signature ( );
let formOptions = signature.formOptions;
formOptions.signKey = 'SIGN';
formOptions.signTypeKey = 'SIGN_TYPE';
formOptions.signSaltKey = 'SIGN_SALT';
formOptions.ignoreKeys = [ 'SIGN', 'SIGN_SALT' ];
formOptions.salt = "Default salt"; // 默认盐, 可以不设置
let json = { a:1,b:2 };
let signer = signature.rsa ( )
.update ( json )
.formOptions ( {
salt: "This is salt", // &SIGN_SALT=This%20is%20salt
} );
let { sign, querystring } = signer.digest ( 'hex' );
console.log ( 'sign', sign );
console.log ( 'signed form', form );
console.log ( 'signed querystring', querystring );
let verified = signer.verify ( sign, 'hex' );
console.log ( 'verified', verified );