crypto-node.js 3.5 KB

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