@aayu-tech/json-sign 中文文档教程
JSON Sign
这是一个实用程序,可用于为 JSON 对象生成签名并根据 JSON 对象。
Installation
这个模块可以通过 NPM 安装。
npm install @aayu-tech/json-sign --save
Generating a key-pair
签名和验证操作需要公私钥对。 你可以生成这样的密钥对 使用 openssl
如下。
Generate Private Key
openssl genrsa -out private.pem 2048
这将生成一个私钥并将其存储到 private.pem
文件中。
Export the Public Key
openssl rsa -in private.pem -outform PEM -pubout -out public.pem
这会将之前创建的私钥的公钥导出到 public.pem
文件。
Generate a Signature
By providing Private Key content
const {getSignatureByKey} = require('@aayu-tech/json-sign');
const obj = {a: 123, b: "foo", c: true};
const signature = getSignatureByKey(obj, '-----BEGIN RSA PRIVATE KEY-----\nMIIEowIBAA....\n-----END RSA PRIVATE KEY-----\n');
By providing Private Key file path
const {getSignatureByKeyFile} = require('@aayu-tech/json-sign');
const obj = {a: 123, b: "foo", c: true};
const signature = getSignatureByKeyFile(obj, 'private.pem');
Verify a Signature
By providing Signature and Public Key content
const {verifySignatureByKey} = require('@aayu-tech/json-sign');
const obj = {a: 123, b: "foo", c: true};
const signature = 'kZNzGnfoFYqunxRo/CPoSep6TXfbhtkqACYX70D1leh/bRe+AAI.......';
const isValid = verifySignatureByKey(obj, signature, '-----BEGIN PUBLIC KEY-----\nMIIBIjAN......\n-----END PUBLIC KEY-----');
By providing Signature and Public Key file path
const {verifySignatureByKeyFile} = require('@aayu-tech/json-sign');
const obj = {a: 123, b: "foo", c: true};
const signature = 'kZNzGnfoFYqunxRo/CPoSep6TXfbhtkqACYX70D1leh/bRe+AAI.......';
const isValid = verifySignatureByKeyFile(obj, signature, 'public.pem');
JSON Sign
This is a utility that can be used to generate a signature for a JSON object and to verify such a signature against a JSON object.
Installation
This module can be installed via NPM.
npm install @aayu-tech/json-sign --save
Generating a key-pair
A private-public key pair is required for the signing and verification operations. You can generate such a key pair using openssl
as follows.
Generate Private Key
openssl genrsa -out private.pem 2048
This will generate a Private Key and store it in to the private.pem
file.
Export the Public Key
openssl rsa -in private.pem -outform PEM -pubout -out public.pem
This will export the Public key of the previously created Private Key it in to the public.pem
file.
Generate a Signature
By providing Private Key content
const {getSignatureByKey} = require('@aayu-tech/json-sign');
const obj = {a: 123, b: "foo", c: true};
const signature = getSignatureByKey(obj, '-----BEGIN RSA PRIVATE KEY-----\nMIIEowIBAA....\n-----END RSA PRIVATE KEY-----\n');
By providing Private Key file path
const {getSignatureByKeyFile} = require('@aayu-tech/json-sign');
const obj = {a: 123, b: "foo", c: true};
const signature = getSignatureByKeyFile(obj, 'private.pem');
Verify a Signature
By providing Signature and Public Key content
const {verifySignatureByKey} = require('@aayu-tech/json-sign');
const obj = {a: 123, b: "foo", c: true};
const signature = 'kZNzGnfoFYqunxRo/CPoSep6TXfbhtkqACYX70D1leh/bRe+AAI.......';
const isValid = verifySignatureByKey(obj, signature, '-----BEGIN PUBLIC KEY-----\nMIIBIjAN......\n-----END PUBLIC KEY-----');
By providing Signature and Public Key file path
const {verifySignatureByKeyFile} = require('@aayu-tech/json-sign');
const obj = {a: 123, b: "foo", c: true};
const signature = 'kZNzGnfoFYqunxRo/CPoSep6TXfbhtkqACYX70D1leh/bRe+AAI.......';
const isValid = verifySignatureByKeyFile(obj, signature, 'public.pem');