| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- "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());
- });
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- const { generateKeyPair, createPublicKey, createPrivateKey, createHash, createSign } = require('crypto');
- const bs58 = require('bs58');
- class CryptoNode {
- generateRsaKeyPair(bits) {
- return new Promise((resolve, reject) => {
- generateKeyPair('rsa', {
- modulusLength: bits,
- privateKeyEncoding: {
- format: 'pem',
- type: 'pkcs1',
- },
- publicKeyEncoding: {
- format: 'pem',
- type: 'spki',
- },
- }, (err, publicKey, privateKey) => {
- 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(privateKeyText, data) {
- return __awaiter(this, void 0, void 0, function* () {
- const privateKey = createPrivateKey(privateKeyText);
- const sign = createSign('SHA256');
- sign.write(data);
- sign.end();
- const signature = sign.sign(privateKey, 'hex');
- return signature;
- });
- }
- importRsaKeyPair(serialized) {
- return __awaiter(this, void 0, void 0, function* () {
- const { privateKey, publicKey } = JSON.parse(bs58.decode(serialized));
- return {
- export: () => this.export(privateKey, publicKey),
- getPublicHash: () => this.getPublicHash(publicKey),
- getPublicKey: () => this.getPublicKey(publicKey),
- sign: (data) => this.sign(privateKey, data),
- };
- });
- }
- getPublicHash(publicKeyText) {
- const publicKey = createPublicKey(publicKeyText);
- const derBytes = publicKey.export({
- type: 'spki',
- format: 'der'
- });
- const sha256 = createHash('sha256');
- sha256.update(derBytes);
- const pubHash = bs58.encode(sha256.digest());
- return pubHash;
- }
- getPublicKey(publicKeyText) {
- const publicKey = createPublicKey(publicKeyText);
- const derBytes = publicKey.export({
- type: 'spki',
- format: 'der'
- });
- return derBytes.toString('hex');
- }
- export(privateKey, publicKey) {
- return __awaiter(this, void 0, void 0, function* () {
- const jsonString = JSON.stringify({ privateKey, publicKey });
- return bs58.encode(Buffer.from(jsonString, 'utf-8'));
- });
- }
- }
- exports.default = CryptoNode;
- //# sourceMappingURL=crypto-node.js.map
|