crypto-forge.js 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. return new (P || (P = Promise))(function (resolve, reject) {
  4. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  5. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  6. function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
  7. step((generator = generator.apply(thisArg, _arguments || [])).next());
  8. });
  9. };
  10. var __importDefault = (this && this.__importDefault) || function (mod) {
  11. return (mod && mod.__esModule) ? mod : { "default": mod };
  12. };
  13. Object.defineProperty(exports, "__esModule", { value: true });
  14. const bs58_1 = __importDefault(require("bs58"));
  15. const node_forge_1 = require("node-forge");
  16. class CryptoForge {
  17. generateRsaKeyPair(bits) {
  18. return new Promise((resolve, reject) => {
  19. node_forge_1.pki.rsa.generateKeyPair({ bits, workers: 2 }, (err, { privateKey, publicKey }) => {
  20. if (err) {
  21. reject(err);
  22. return;
  23. }
  24. resolve({
  25. export: () => this.export(privateKey, publicKey),
  26. getPublicHash: () => this.getPublicHash(publicKey),
  27. getPublicKey: () => this.getPublicKey(publicKey),
  28. sign: (data) => this.sign(privateKey, data),
  29. });
  30. });
  31. });
  32. }
  33. sign(privateKeyObj, data) {
  34. return __awaiter(this, void 0, void 0, function* () {
  35. const sha256 = node_forge_1.md.sha256.create();
  36. const buf = node_forge_1.util.hexToBytes(data.toString('hex'));
  37. sha256.update(buf);
  38. const signature = privateKeyObj.sign(sha256);
  39. const hexed = node_forge_1.util.bytesToHex(signature);
  40. return hexed;
  41. });
  42. }
  43. importRsaKeyPair(serialized) {
  44. return __awaiter(this, void 0, void 0, function* () {
  45. const { privateKey, publicKey } = JSON.parse(bs58_1.default.decode(serialized).toString('utf-8'));
  46. const privateKeyObj = node_forge_1.pki.privateKeyFromPem(privateKey);
  47. const publicKeyObj = node_forge_1.pki.publicKeyFromPem(publicKey);
  48. return {
  49. export: () => this.export(privateKeyObj, publicKeyObj),
  50. getPublicHash: () => this.getPublicHash(publicKeyObj),
  51. getPublicKey: () => this.getPublicKey(publicKeyObj),
  52. sign: (data) => this.sign(privateKeyObj, data),
  53. };
  54. });
  55. }
  56. getPublicHash(publicKeyObj) {
  57. const publicKeyHexString = this.getPublicKey(publicKeyObj);
  58. const sha256 = node_forge_1.md.sha256.create();
  59. const buf = node_forge_1.util.hexToBytes(publicKeyHexString);
  60. sha256.update(buf);
  61. const digested = sha256.digest().bytes();
  62. const pubHash = bs58_1.default.encode(Buffer.from(digested, 'binary'));
  63. return pubHash;
  64. }
  65. getPublicKey(publicKeyObj) {
  66. const pk = node_forge_1.asn1.toDer(node_forge_1.pki.publicKeyToAsn1(publicKeyObj)).toHex();
  67. return pk;
  68. }
  69. export(privateKeyObj, publicKeyObj) {
  70. return __awaiter(this, void 0, void 0, function* () {
  71. const privateKey = node_forge_1.pki.privateKeyToPem(privateKeyObj);
  72. const publicKey = node_forge_1.pki.publicKeyToPem(publicKeyObj);
  73. const jsonString = JSON.stringify({ privateKey, publicKey });
  74. return bs58_1.default.encode(Buffer.from(jsonString, 'utf-8'));
  75. });
  76. }
  77. }
  78. exports.default = CryptoForge;
  79. //# sourceMappingURL=crypto-forge.js.map