crypto-node.js 3.7 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 crypto_1 = require("crypto");
  15. const bs58_1 = __importDefault(require("bs58"));
  16. class CryptoNode {
  17. generateRsaKeyPair(bits) {
  18. return new Promise((resolve, reject) => {
  19. crypto_1.generateKeyPair('rsa', {
  20. modulusLength: bits,
  21. privateKeyEncoding: {
  22. format: 'pem',
  23. type: 'pkcs1',
  24. },
  25. publicKeyEncoding: {
  26. format: 'pem',
  27. type: 'spki',
  28. },
  29. }, (err, publicKey, privateKey) => {
  30. if (err) {
  31. reject(err);
  32. return;
  33. }
  34. resolve({
  35. export: () => this.export(privateKey, publicKey),
  36. getPublicHash: () => this.getPublicHash(publicKey),
  37. getPublicKey: () => this.getPublicKey(publicKey),
  38. sign: (data) => this.sign(privateKey, data),
  39. });
  40. });
  41. });
  42. }
  43. sign(privateKeyText, dataHexBytes) {
  44. return __awaiter(this, void 0, void 0, function* () {
  45. const privateKey = crypto_1.createPrivateKey(privateKeyText);
  46. const sign = crypto_1.createSign('SHA256');
  47. sign.write(Buffer.from(dataHexBytes, 'hex'));
  48. sign.end();
  49. const signature = sign.sign(privateKey, 'hex');
  50. return signature;
  51. });
  52. }
  53. importRsaKeyPair(serialized) {
  54. return __awaiter(this, void 0, void 0, function* () {
  55. const decoded = bs58_1.default.decode(serialized);
  56. const { privateKey, publicKey } = JSON.parse(decoded.toString('utf-8'));
  57. return {
  58. export: () => this.export(privateKey, publicKey),
  59. getPublicHash: () => this.getPublicHash(publicKey),
  60. getPublicKey: () => this.getPublicKey(publicKey),
  61. sign: (data) => this.sign(privateKey, data),
  62. };
  63. });
  64. }
  65. getPublicHash(publicKeyText) {
  66. const publicKey = crypto_1.createPublicKey(publicKeyText);
  67. const derBytes = publicKey.export({
  68. format: 'der',
  69. type: 'spki',
  70. });
  71. const sha256 = crypto_1.createHash('sha256');
  72. sha256.update(derBytes);
  73. const pubHash = bs58_1.default.encode(sha256.digest());
  74. return pubHash;
  75. }
  76. getPublicKey(publicKeyText) {
  77. const publicKey = crypto_1.createPublicKey(publicKeyText);
  78. const derBytes = publicKey.export({
  79. format: 'der',
  80. type: 'spki',
  81. });
  82. return derBytes.toString('hex');
  83. }
  84. export(privateKey, publicKey) {
  85. return __awaiter(this, void 0, void 0, function* () {
  86. const jsonString = JSON.stringify({ privateKey, publicKey });
  87. return bs58_1.default.encode(Buffer.from(jsonString, 'utf-8'));
  88. });
  89. }
  90. }
  91. exports.default = CryptoNode;
  92. //# sourceMappingURL=crypto-node.js.map