@diyaner/ding
Version:
dingiyan常用ts/js工具
168 lines • 6.14 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DingRSACrypto = void 0;
const crypto = require("crypto");
const fs = require("fs");
class DingRSACrypto {
constructor(option) {
this.option = option;
this.utf_8 = "utf-8";
if (option) {
this._privateKey = option.privateKey;
this._publicKey = option.publicKey;
}
}
/**
* 生成rsa公私钥 ,默认为pem格式,长度4096
*
* @return {Promise<{privateKey:string;publicKey:string}>}
* @memberof DingRSACrypto
*/
static generateKeys(length = 4096) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
crypto.generateKeyPair("rsa", {
modulusLength: length,
publicKeyEncoding: {
type: "spki",
format: "pem",
},
privateKeyEncoding: {
type: "pkcs8",
format: "pem",
// cipher: "aes-256-cbc",
// passphrase: "",
},
}, (err, publicKey, privateKey) => {
if (err) {
reject(err);
}
else {
resolve({ publicKey, privateKey });
}
});
});
});
}
/** 读取私钥文件 */
readPrivateKeyFile(path) {
const pemStr = fs.readFileSync(path, this.utf_8);
this._privateKey = pemStr;
}
/** 读取公钥文件 */
readPublicKeyFile(path) {
const pemStr = fs.readFileSync(path, this.utf_8);
this._publicKey = pemStr;
}
setPrivateKey(privateKey) {
this._privateKey = privateKey;
}
setPublicKey(publicKey) {
this._publicKey = publicKey;
}
get privateKey() {
if (!this._privateKey)
throw new Error(`could not find privateKey, please provide it.`);
return this._privateKey;
}
get publicKey() {
if (!this._publicKey)
throw new Error(`could not find publicKey, please provide it.`);
return this._publicKey;
}
/**
*
* 私钥签名,用于对原文内容生成签名,对原文base64编码后,再对base64字符串签名
*
* @param {(string | Record<string, any>)} str 要签名的字符串或一个json对象
* @param {("RSA-SHA1" | "RSA-SHA256")} [algorithm="RSA-SHA1"] 算法
* @return {{base64:string;sign:string}} 返回签名和base64字符串
* @memberof RSACryptoUtils
*/
base64Sign(str, algorithm = "RSA-SHA1") {
if (typeof str !== "string") {
str = JSON.stringify(str);
}
const base64Str = Buffer.from(str, this.utf_8).toString("base64");
const signedStr = this.sign(base64Str, algorithm);
return {
base64: base64Str,
sign: signedStr,
};
}
/**
*
* 私钥签名,用于对原文内容生成签名
*
* @param {(string | Record<string, any>)} str 要签名的字符串或一个json对象
* @param {("RSA-SHA1" | "RSA-SHA256")} [algorithm="RSA-SHA1"] 算法
* @return {{base64:string;sign:string}} 返回签名的base64编码字符串
* @memberof DingRSACrypto
*/
sign(str, algorithm = "RSA-SHA1") {
if (typeof str !== "string") {
str = JSON.stringify(str);
}
const buf = Buffer.from(str, this.utf_8);
const sha = crypto.createSign(algorithm);
const privateKey = this.privateKey;
sha.update(buf);
return sha.sign(privateKey, "base64");
}
/**
*
* 公钥验证 字符串str公钥验证签名是否正确
*
* @param {string} str 要验证的字符串
* @param {string} sign 签名, base64编码
* @param {("RSA-SHA1" | "RSA-SHA256")} [algorithm="RSA-SHA1"] 算法
* @memberof AbcPaySignAndVerify
*/
verify(str, sign, algorithm = "RSA-SHA1") {
const verify = crypto.createVerify(algorithm);
const publicKey = this.publicKey;
verify.update(str, "utf8");
const result = verify.verify(publicKey, sign, "base64");
return result;
}
/** 公钥加密 */
publicEncrypt(str) {
const buffer = Buffer.from(str, "utf8");
return crypto.publicEncrypt(this.publicKey, buffer).toString("base64");
}
/** 私钥加密 */
privateEncrypt(str) {
const buffer = Buffer.from(str, "utf8");
return crypto.privateEncrypt(this.privateKey, buffer).toString("base64");
}
/** 公钥解密 */
publicDecrypt(base64Cipher) {
const buffer = Buffer.from(base64Cipher, "base64");
const plain = crypto.publicDecrypt({
key: this.publicKey,
passphrase: "",
}, buffer);
return plain.toString("utf8");
}
/** 私钥解密 */
privateDecrypt(base64Cipher) {
const buffer = Buffer.from(base64Cipher, "base64");
const plain = crypto.privateDecrypt({
key: this.privateKey,
passphrase: "",
}, buffer);
return plain.toString("utf8");
}
}
exports.DingRSACrypto = DingRSACrypto;
exports.default = DingRSACrypto;
//# sourceMappingURL=rsa.js.map