| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- "use strict";
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
- 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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
- step((generator = generator.apply(thisArg, _arguments || [])).next());
- });
- };
- var __importDefault = (this && this.__importDefault) || function (mod) {
- return (mod && mod.__esModule) ? mod : { "default": mod };
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- const bs58_1 = __importDefault(require("bs58"));
- const node_forge_1 = require("node-forge");
- class CryptoForge {
- generateRsaKeyPair(bits) {
- return new Promise((resolve, reject) => {
- node_forge_1.pki.rsa.generateKeyPair({ bits, workers: 2 }, (err, { privateKey, publicKey }) => {
- if (err) {
- reject(err);
- return;
- }
- resolve({
- export: () => this.export(privateKey, publicKey),
- getPublicHash: () => this.getPublicHash(publicKey),
- getPublicKey: () => this.getPublicKey(publicKey),
- sign: (data) => this.sign(privateKey, data),
- });
- });
- });
- }
- sign(privateKeyObj, data) {
- return __awaiter(this, void 0, void 0, function* () {
- const sha256 = node_forge_1.md.sha256.create();
- const buf = node_forge_1.util.hexToBytes(data.toString('hex'));
- sha256.update(buf);
- const signature = privateKeyObj.sign(sha256);
- const hexed = node_forge_1.util.bytesToHex(signature);
- return hexed;
- });
- }
- importRsaKeyPair(serialized) {
- return __awaiter(this, void 0, void 0, function* () {
- const { privateKey, publicKey } = JSON.parse(bs58_1.default.decode(serialized).toString('utf-8'));
- const privateKeyObj = node_forge_1.pki.privateKeyFromPem(privateKey);
- const publicKeyObj = node_forge_1.pki.publicKeyFromPem(publicKey);
- return {
- export: () => this.export(privateKeyObj, publicKeyObj),
- getPublicHash: () => this.getPublicHash(publicKeyObj),
- getPublicKey: () => this.getPublicKey(publicKeyObj),
- sign: (data) => this.sign(privateKeyObj, data),
- };
- });
- }
- getPublicHash(publicKeyObj) {
- const publicKeyHexString = this.getPublicKey(publicKeyObj);
- const sha256 = node_forge_1.md.sha256.create();
- const buf = node_forge_1.util.hexToBytes(publicKeyHexString);
- sha256.update(buf);
- const digested = sha256.digest().bytes();
- const pubHash = bs58_1.default.encode(Buffer.from(digested, 'binary'));
- return pubHash;
- }
- getPublicKey(publicKeyObj) {
- const pk = node_forge_1.asn1.toDer(node_forge_1.pki.publicKeyToAsn1(publicKeyObj)).toHex();
- return pk;
- }
- export(privateKeyObj, publicKeyObj) {
- return __awaiter(this, void 0, void 0, function* () {
- const privateKey = node_forge_1.pki.privateKeyToPem(privateKeyObj);
- const publicKey = node_forge_1.pki.publicKeyToPem(publicKeyObj);
- const jsonString = JSON.stringify({ privateKey, publicKey });
- return bs58_1.default.encode(Buffer.from(jsonString, 'utf-8'));
- });
- }
- }
- exports.default = CryptoForge;
- //# sourceMappingURL=crypto-forge.js.map
|