npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fucking-util-signature-uni

v1.2.1

Published

兼容支持 微信小程序

Downloads

10

Readme

fucking-util-signature-uni

兼容支持 微信小程序

模块依赖 fucking-util 模块, 请预先安装

模块选择

  1. 要使用 uni-app 模式开发, 使用 fucking-util-signature-all
  2. 不使用 uni-app 模式开发
    • h5webmui项目 (window自带crypto模块) 或 node环境使用 fucking-util-signature
    • 小程序使用此模块

使用方法


// 多选一
var Signature = require ( "fucking-util-signature-uni" );

// 生成公私钥 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 );

加盐和自定义formOptions


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 );