crypto-forge.js 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. function byteBufferToNumericArray(digested) {
  17. const count = digested.length();
  18. const arr = [];
  19. for (let i = 0; i < count; i++) {
  20. arr.push(digested.getByte());
  21. }
  22. return arr;
  23. }
  24. class CryptoForge {
  25. generateRsaKeyPair(bits) {
  26. return new Promise((resolve, reject) => {
  27. node_forge_1.pki.rsa.generateKeyPair({ bits, workers: 2 }, (err, { privateKey, publicKey }) => {
  28. if (err) {
  29. reject(err);
  30. return;
  31. }
  32. this.export(privateKey, publicKey).then(generated => {
  33. console.log('generated keypair', generated);
  34. });
  35. resolve({
  36. export: () => this.export(privateKey, publicKey),
  37. getPublicHash: () => this.getPublicHash(publicKey),
  38. getPublicKey: () => this.getPublicKey(publicKey),
  39. sign: (data) => this.sign(privateKey, data),
  40. });
  41. });
  42. });
  43. }
  44. sign(privateKeyObj, data) {
  45. return __awaiter(this, void 0, void 0, function* () {
  46. const sha256 = node_forge_1.md.sha256.create();
  47. const buf = node_forge_1.util.hexToBytes(data.toString('hex'));
  48. sha256.update(buf);
  49. const signature = privateKeyObj.sign(sha256);
  50. const hexed = node_forge_1.util.bytesToHex(signature);
  51. return hexed;
  52. });
  53. }
  54. importRsaKeyPair(serialized) {
  55. return __awaiter(this, void 0, void 0, function* () {
  56. const { privateKey, publicKey } = JSON.parse(bs58_1.default.decode(serialized).toString('utf-8'));
  57. const privateKeyObj = node_forge_1.pki.privateKeyFromPem(privateKey);
  58. const publicKeyObj = node_forge_1.pki.publicKeyFromPem(publicKey);
  59. return {
  60. export: () => this.export(privateKeyObj, publicKeyObj),
  61. getPublicHash: () => this.getPublicHash(publicKeyObj),
  62. getPublicKey: () => this.getPublicKey(publicKeyObj),
  63. sign: (data) => this.sign(privateKeyObj, data),
  64. };
  65. });
  66. }
  67. getPublicHash(publicKeyObj) {
  68. const publicKeyHexString = this.getPublicKey(publicKeyObj);
  69. const sha256 = node_forge_1.md.sha256.create();
  70. const buf = node_forge_1.util.hexToBytes(publicKeyHexString);
  71. sha256.update(buf);
  72. const digested = sha256.digest();
  73. const arr = byteBufferToNumericArray(digested);
  74. const pubHash = bs58_1.default.encode(arr);
  75. return pubHash;
  76. }
  77. getPublicKey(publicKeyObj) {
  78. const pk = node_forge_1.asn1.toDer(node_forge_1.pki.publicKeyToAsn1(publicKeyObj)).toHex();
  79. return pk;
  80. }
  81. export(privateKeyObj, publicKeyObj) {
  82. return __awaiter(this, void 0, void 0, function* () {
  83. const privateKey = node_forge_1.pki.privateKeyToPem(privateKeyObj);
  84. const publicKey = node_forge_1.pki.publicKeyToPem(publicKeyObj);
  85. const jsonString = JSON.stringify({ privateKey, publicKey });
  86. const uint8Array = node_forge_1.util.text.utf8.encode(jsonString);
  87. return bs58_1.default.encode(uint8Array);
  88. });
  89. }
  90. }
  91. exports.default = CryptoForge;
  92. //# sourceMappingURL=crypto-forge.js.map