crypto-forge.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. // tslint:disable-next-line:no-console
  34. console.log('generated keypair', generated);
  35. });
  36. resolve({
  37. export: () => this.export(privateKey, publicKey),
  38. getPublicHash: () => this.getPublicHash(publicKey),
  39. getPublicKey: () => this.getPublicKey(publicKey),
  40. sign: (data) => this.sign(privateKey, data),
  41. });
  42. });
  43. });
  44. }
  45. sign(privateKeyObj, dataHexBytes) {
  46. return __awaiter(this, void 0, void 0, function* () {
  47. const sha256 = node_forge_1.md.sha256.create();
  48. const buf = node_forge_1.util.hexToBytes(dataHexBytes);
  49. sha256.update(buf);
  50. const signature = privateKeyObj.sign(sha256);
  51. const hexed = node_forge_1.util.bytesToHex(signature);
  52. return hexed;
  53. });
  54. }
  55. importRsaKeyPair(serialized) {
  56. return __awaiter(this, void 0, void 0, function* () {
  57. const { privateKey, publicKey } = JSON.parse(bs58_1.default.decode(serialized).toString('utf-8'));
  58. const privateKeyObj = node_forge_1.pki.privateKeyFromPem(privateKey);
  59. const publicKeyObj = node_forge_1.pki.publicKeyFromPem(publicKey);
  60. return {
  61. export: () => this.export(privateKeyObj, publicKeyObj),
  62. getPublicHash: () => this.getPublicHash(publicKeyObj),
  63. getPublicKey: () => this.getPublicKey(publicKeyObj),
  64. sign: (data) => this.sign(privateKeyObj, data),
  65. };
  66. });
  67. }
  68. getPublicHash(publicKeyObj) {
  69. const publicKeyHexString = this.getPublicKey(publicKeyObj);
  70. const sha256 = node_forge_1.md.sha256.create();
  71. const buf = node_forge_1.util.hexToBytes(publicKeyHexString);
  72. sha256.update(buf);
  73. const digested = sha256.digest();
  74. const arr = byteBufferToNumericArray(digested);
  75. const pubHash = bs58_1.default.encode(arr);
  76. return pubHash;
  77. }
  78. getPublicKey(publicKeyObj) {
  79. const pk = node_forge_1.asn1.toDer(node_forge_1.pki.publicKeyToAsn1(publicKeyObj)).toHex();
  80. return pk;
  81. }
  82. export(privateKeyObj, publicKeyObj) {
  83. return __awaiter(this, void 0, void 0, function* () {
  84. const privateKey = node_forge_1.pki.privateKeyToPem(privateKeyObj);
  85. const publicKey = node_forge_1.pki.publicKeyToPem(publicKeyObj);
  86. const jsonString = JSON.stringify({ privateKey, publicKey });
  87. const uint8Array = node_forge_1.util.text.utf8.encode(jsonString);
  88. return bs58_1.default.encode(uint8Array);
  89. });
  90. }
  91. }
  92. exports.default = CryptoForge;
  93. //# sourceMappingURL=crypto-forge.js.map