mksign
A lib project with ziu
Last updated a year ago by gary_zhou .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install mksign 
SYNC missed versions from official npm registry.

mksign


author: bugszhou | Email:bugszhou@outlook.com
description: A lib project with ziu

提供对API请求参数生成签名的基础库

Usage

npm install -S mksign

使用默认的签名方式

import { defaultSign } from 'mksign';

// 需要签名的数据
const secret = 'mock_secret';
const data = {
    param1: 'test',
    name: 'tom',
    sex: ''
};

const sign = defaultSign(data, [secret]);
  • 默认签名机制:
  1. 对参加签名的参数datakey值的ASCII码从小到大排序(字典排序),将指定的key值(即v1=value1&v2=value2&v3=value3)拼接成字符串stringA;
  2. 在 stringA 最后拼接上secret得到待签名字符串stringB,并对stringB进行sha256运算,得到sign值。
  1. 值为null/undefined/''的参数会直接忽略,不参与签名。

API

defaultGetSignData(data:object, lastData: array[string, object])

对参加签名的参数datakey值的ASCII码从小到大排序(字典排序),将指定的key值(即v1=value1&v2=value2&v3=value3)拼接成字符串stringA;在 stringA 最后拼接上lastData得到待签名字符串stringB

defaultSign(data:object, lastData: array[string, object])

内部使用defaultGetSignData得到待签名字符串stringB,使用sha256stringB运算得到签名值

getFilterData(data:object)

去除datanull/undefined/''的字段,并返回排序后的字段数组

let filterData = getFilterData({
    v1: 1,
    v2: '',
    v3: null
});

console.log(filterData); // ['v1']

sort(data:object)

对参数datakey值的ASCII码从小到大排序(字典排序),返回一个key数组

filterNull(data:object)

去除datanull/undefined/''的字段, 返回新的数据

let filterData = filterNull({
    v1: 1,
    v2: '',
    v3: null
});
console.log(filterData); // {v1: 1}

getSignStr(data:array)

获取待签名字符串,data为key-value组成的二维数组;如果没有二维数组中的元素的数组长度为1,将拼接在字符串最后

let signDataStr = getSignStr([['test'], ['v1', 1], ['v5', '2']]);

console.log(signDataStr); // v1=1&v5=2test

Current Tags

  • 2.0.1                                ...           latest (a year ago)

20 Versions

  • 2.0.1                                ...           a year ago
  • 2.0.0                                ...           a year ago
  • 1.2.0                                ...           a year ago
  • 1.1.10                                ...           a year ago
  • 1.1.9                                ...           2 years ago
  • 1.1.8                                ...           2 years ago
  • 1.1.7                                ...           2 years ago
  • 1.1.6                                ...           2 years ago
  • 1.1.5                                ...           2 years ago
  • 1.1.4                                ...           2 years ago
  • 1.1.3                                ...           2 years ago
  • 1.1.2                                ...           2 years ago
  • 1.1.1                                ...           2 years ago
  • 1.1.0                                ...           2 years ago
  • 1.0.5                                ...           2 years ago
  • 1.0.4                                ...           2 years ago
  • 1.0.3                                ...           2 years ago
  • 1.0.2                                ...           2 years ago
  • 1.0.1                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 7
This Month 54
Last Day 3
Last Week 20
Last Month 12
Dependencies (5)
Dependents (2)

Copyright 2014 - 2017 © taobao.org |