@ocap/mcrypto 1.27.16 → 1.28.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/esm/crypter/aes-legacy.d.ts +8 -6
  2. package/esm/crypter/aes-legacy.js +27 -24
  3. package/esm/crypter/aes.d.ts +9 -7
  4. package/esm/crypter/aes.js +27 -25
  5. package/esm/crypter/rsa-browserify.d.ts +7 -6
  6. package/esm/crypter/rsa-browserify.js +34 -31
  7. package/esm/crypter/rsa.d.ts +11 -9
  8. package/esm/crypter/rsa.js +31 -28
  9. package/esm/encode.d.ts +12 -8
  10. package/esm/encode.js +13 -18
  11. package/esm/hasher/keccak.d.ts +8 -5
  12. package/esm/hasher/keccak.js +37 -36
  13. package/esm/hasher/sha2.d.ts +8 -5
  14. package/esm/hasher/sha2.js +42 -40
  15. package/esm/hasher/sha3.d.ts +8 -5
  16. package/esm/hasher/sha3.js +37 -36
  17. package/esm/index.d.ts +148 -143
  18. package/esm/index.js +172 -209
  19. package/esm/protocols/crypter.d.ts +3 -1
  20. package/esm/protocols/crypter.js +7 -4
  21. package/esm/protocols/hasher.d.ts +3 -1
  22. package/esm/protocols/hasher.js +7 -4
  23. package/esm/protocols/signer.d.ts +3 -1
  24. package/esm/protocols/signer.js +7 -4
  25. package/esm/signer/ed25519.d.ts +49 -46
  26. package/esm/signer/ed25519.js +80 -80
  27. package/esm/signer/ethereum.d.ts +11 -8
  28. package/esm/signer/ethereum.js +34 -34
  29. package/esm/signer/passkey.d.ts +21 -18
  30. package/esm/signer/passkey.js +49 -58
  31. package/esm/signer/secp256k1.d.ts +35 -32
  32. package/esm/signer/secp256k1.js +80 -93
  33. package/lib/_virtual/rolldown_runtime.js +29 -0
  34. package/lib/crypter/aes-legacy.d.ts +8 -6
  35. package/lib/crypter/aes-legacy.js +35 -52
  36. package/lib/crypter/aes.d.ts +9 -7
  37. package/lib/crypter/aes.js +29 -29
  38. package/lib/crypter/rsa-browserify.d.ts +7 -6
  39. package/lib/crypter/rsa-browserify.js +36 -33
  40. package/lib/crypter/rsa.d.ts +11 -9
  41. package/lib/crypter/rsa.js +33 -32
  42. package/lib/encode.d.ts +12 -8
  43. package/lib/encode.js +14 -21
  44. package/lib/hasher/keccak.d.ts +8 -5
  45. package/lib/hasher/keccak.js +40 -41
  46. package/lib/hasher/sha2.d.ts +8 -5
  47. package/lib/hasher/sha2.js +43 -42
  48. package/lib/hasher/sha3.d.ts +8 -5
  49. package/lib/hasher/sha3.js +40 -41
  50. package/lib/index.d.ts +148 -143
  51. package/lib/index.js +177 -216
  52. package/lib/protocols/crypter.d.ts +3 -1
  53. package/lib/protocols/crypter.js +10 -9
  54. package/lib/protocols/hasher.d.ts +3 -1
  55. package/lib/protocols/hasher.js +10 -9
  56. package/lib/protocols/signer.d.ts +3 -1
  57. package/lib/protocols/signer.js +10 -9
  58. package/lib/signer/ed25519.d.ts +49 -46
  59. package/lib/signer/ed25519.js +85 -86
  60. package/lib/signer/ethereum.d.ts +11 -8
  61. package/lib/signer/ethereum.js +38 -39
  62. package/lib/signer/passkey.d.ts +21 -18
  63. package/lib/signer/passkey.js +51 -63
  64. package/lib/signer/secp256k1.d.ts +35 -32
  65. package/lib/signer/secp256k1.js +85 -99
  66. package/package.json +21 -10
@@ -1,95 +1,82 @@
1
- import elliptic from 'elliptic';
2
- import { BN, stripHexPrefix, toHex, toBuffer, toUint8Array } from '@ocap/util';
3
- import randomBytes from 'randombytes';
4
- import Signer from '../protocols/signer';
5
- import { encode } from '../encode';
6
- /* eslint-disable class-methods-use-this */
7
- /* eslint-disable no-useless-constructor */
1
+ import { encode } from "../encode.js";
2
+ import signer_default from "../protocols/signer.js";
3
+ import { BN, stripHexPrefix, toBuffer, toHex, toUint8Array } from "@ocap/util";
4
+ import randomBytes from "randombytes";
5
+ import elliptic from "elliptic";
6
+
7
+ //#region src/signer/secp256k1.ts
8
8
  const EC = elliptic.ec;
9
- const secp256k1 = new EC('secp256k1');
9
+ const secp256k1 = new EC("secp256k1");
10
10
  /**
11
- * Signer implementation for secp256k1, based on `elliptic`
12
- *
13
- * @class Secp256k1Signer
14
- */
15
- class Secp256k1Signer extends Signer {
16
- constructor() {
17
- super();
18
- this.pkHasFormatPrefix = true;
19
- this.pkCompressed = false;
20
- }
21
- isValidSK(sk) {
22
- if (sk.byteLength !== 32) {
23
- return false;
24
- }
25
- const bn = new BN(sk);
26
- return bn.cmp(secp256k1.curve.n) < 0 && !bn.isZero();
27
- }
28
- /**
29
- * @public
30
- * @typedefKeyPairType
31
- * @prop {string} publicKey - publicKey in hex format
32
- * @prop {string} secretKey - secretKey in hex format
33
- * @memberof Secp256k1Signer
34
- */
35
- /**
36
- * Generate random secret/public key pair
37
- */
38
- genKeyPair(encoding = 'hex') {
39
- let sk = null;
40
- do {
41
- sk = new Uint8Array(randomBytes(32));
42
- } while (!this.isValidSK(sk));
43
- const pk = this.getPublicKey(toHex(sk));
44
- return { secretKey: encode(sk, encoding), publicKey: encode(pk, encoding) };
45
- }
46
- /**
47
- * Get publicKey from secretKey
48
- */
49
- getPublicKey(sk, encoding = 'hex') {
50
- if (!this.isValidSK(toUint8Array(sk))) {
51
- throw new Error('Invalid secret key');
52
- }
53
- let pk = secp256k1.keyFromPrivate(toBuffer(sk)).getPublic(this.pkCompressed, 'hex');
54
- if (this.pkHasFormatPrefix === false) {
55
- pk = pk.slice(2);
56
- }
57
- return encode(`0x${pk}`, encoding);
58
- }
59
- /**
60
- * Sign a message and get the signature hex
61
- */
62
- sign(message, sk, encoding = 'hex') {
63
- let msg = message;
64
- try {
65
- msg = toUint8Array(message);
66
- }
67
- catch (err) {
68
- // Do nothing;
69
- }
70
- const signature = secp256k1
71
- .keyFromPrivate(toBuffer(sk))
72
- .sign(stripHexPrefix(msg), { canonical: true })
73
- .toDER('hex');
74
- return encode(`0x${signature}`, encoding);
75
- }
76
- /**
77
- * Verify if a signature is valid
78
- */
79
- verify(message, signature, pk) {
80
- let msg = message;
81
- try {
82
- msg = toUint8Array(message);
83
- }
84
- catch (err) {
85
- // Do nothing;
86
- }
87
- let pkBuffer = toBuffer(pk);
88
- if (this.pkHasFormatPrefix === false && pkBuffer[0] !== 0x04 && pkBuffer.byteLength === 64) {
89
- pkBuffer = Buffer.concat([Buffer.from([0x04]), pkBuffer]);
90
- }
91
- return secp256k1.keyFromPublic(pkBuffer).verify(stripHexPrefix(msg), stripHexPrefix(toHex(signature)));
92
- }
93
- }
94
- export default new Secp256k1Signer();
95
- export { Secp256k1Signer };
11
+ * Signer implementation for secp256k1, based on `elliptic`
12
+ *
13
+ * @class Secp256k1Signer
14
+ */
15
+ var Secp256k1Signer = class extends signer_default {
16
+ constructor() {
17
+ super();
18
+ this.pkHasFormatPrefix = true;
19
+ this.pkCompressed = false;
20
+ }
21
+ isValidSK(sk) {
22
+ if (sk.byteLength !== 32) return false;
23
+ const bn = new BN(sk);
24
+ return bn.cmp(secp256k1.curve.n) < 0 && !bn.isZero();
25
+ }
26
+ /**
27
+ * @public
28
+ * @typedefKeyPairType
29
+ * @prop {string} publicKey - publicKey in hex format
30
+ * @prop {string} secretKey - secretKey in hex format
31
+ * @memberof Secp256k1Signer
32
+ */
33
+ /**
34
+ * Generate random secret/public key pair
35
+ */
36
+ genKeyPair(encoding = "hex") {
37
+ let sk = null;
38
+ do
39
+ sk = new Uint8Array(randomBytes(32));
40
+ while (!this.isValidSK(sk));
41
+ const pk = this.getPublicKey(toHex(sk));
42
+ return {
43
+ secretKey: encode(sk, encoding),
44
+ publicKey: encode(pk, encoding)
45
+ };
46
+ }
47
+ /**
48
+ * Get publicKey from secretKey
49
+ */
50
+ getPublicKey(sk, encoding = "hex") {
51
+ if (!this.isValidSK(toUint8Array(sk))) throw new Error("Invalid secret key");
52
+ let pk = secp256k1.keyFromPrivate(toBuffer(sk)).getPublic(this.pkCompressed, "hex");
53
+ if (this.pkHasFormatPrefix === false) pk = pk.slice(2);
54
+ return encode(`0x${pk}`, encoding);
55
+ }
56
+ /**
57
+ * Sign a message and get the signature hex
58
+ */
59
+ sign(message, sk, encoding = "hex") {
60
+ let msg = message;
61
+ try {
62
+ msg = toUint8Array(message);
63
+ } catch (err) {}
64
+ return encode(`0x${secp256k1.keyFromPrivate(toBuffer(sk)).sign(stripHexPrefix(msg), { canonical: true }).toDER("hex")}`, encoding);
65
+ }
66
+ /**
67
+ * Verify if a signature is valid
68
+ */
69
+ verify(message, signature, pk) {
70
+ let msg = message;
71
+ try {
72
+ msg = toUint8Array(message);
73
+ } catch (err) {}
74
+ let pkBuffer = toBuffer(pk);
75
+ if (this.pkHasFormatPrefix === false && pkBuffer[0] !== 4 && pkBuffer.byteLength === 64) pkBuffer = Buffer.concat([Buffer.from([4]), pkBuffer]);
76
+ return secp256k1.keyFromPublic(pkBuffer).verify(stripHexPrefix(msg), stripHexPrefix(toHex(signature)));
77
+ }
78
+ };
79
+ var secp256k1_default = new Secp256k1Signer();
80
+
81
+ //#endregion
82
+ export { Secp256k1Signer, secp256k1_default as default };
@@ -0,0 +1,29 @@
1
+ //#region rolldown:runtime
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
+ key = keys[i];
12
+ if (!__hasOwnProp.call(to, key) && key !== except) {
13
+ __defProp(to, key, {
14
+ get: ((k) => from[k]).bind(null, key),
15
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
16
+ });
17
+ }
18
+ }
19
+ }
20
+ return to;
21
+ };
22
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
23
+ value: mod,
24
+ enumerable: true
25
+ }) : target, mod));
26
+
27
+ //#endregion
28
+
29
+ exports.__toESM = __toESM;
@@ -1,8 +1,10 @@
1
- import Crypter from '../protocols/crypter';
2
- declare class AesCrypter extends Crypter {
3
- encrypt(message: string | object, secret: string): string;
4
- decrypt(cipher: string, secret: string, outputEncoding?: string): string;
1
+ import _default$1 from "../protocols/crypter.js";
2
+
3
+ //#region src/crypter/aes-legacy.d.ts
4
+ declare class AesCrypter extends _default$1 {
5
+ encrypt(message: string | object, secret: string): string;
6
+ decrypt(cipher: string, secret: string, outputEncoding?: string): string;
5
7
  }
6
8
  declare const _default: AesCrypter;
7
- export default _default;
8
- export { AesCrypter };
9
+ //#endregion
10
+ export { AesCrypter, _default as default };
@@ -1,55 +1,38 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- var __importDefault = (this && this.__importDefault) || function (mod) {
26
- return (mod && mod.__esModule) ? mod : { "default": mod };
27
- };
28
- Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.AesCrypter = void 0;
30
- const AES = __importStar(require("crypto-js/aes"));
31
- const enc_latin1_1 = __importDefault(require("crypto-js/enc-latin1"));
32
- const enc_utf8_1 = __importDefault(require("crypto-js/enc-utf8"));
33
- const enc_utf16_1 = __importDefault(require("crypto-js/enc-utf16"));
34
- const enc_base64_1 = __importDefault(require("crypto-js/enc-base64"));
35
- const enc_hex_1 = __importDefault(require("crypto-js/enc-hex"));
36
- const crypter_1 = __importDefault(require("../protocols/crypter"));
1
+ Object.defineProperty(exports, '__esModule', { value: true });
2
+ const require_rolldown_runtime = require('../_virtual/rolldown_runtime.js');
3
+ const require_protocols_crypter = require('../protocols/crypter.js');
4
+ let crypto_js_aes = require("crypto-js/aes");
5
+ crypto_js_aes = require_rolldown_runtime.__toESM(crypto_js_aes);
6
+ let crypto_js_enc_latin1 = require("crypto-js/enc-latin1");
7
+ crypto_js_enc_latin1 = require_rolldown_runtime.__toESM(crypto_js_enc_latin1);
8
+ let crypto_js_enc_utf8 = require("crypto-js/enc-utf8");
9
+ crypto_js_enc_utf8 = require_rolldown_runtime.__toESM(crypto_js_enc_utf8);
10
+ let crypto_js_enc_utf16 = require("crypto-js/enc-utf16");
11
+ crypto_js_enc_utf16 = require_rolldown_runtime.__toESM(crypto_js_enc_utf16);
12
+ let crypto_js_enc_base64 = require("crypto-js/enc-base64");
13
+ crypto_js_enc_base64 = require_rolldown_runtime.__toESM(crypto_js_enc_base64);
14
+ let crypto_js_enc_hex = require("crypto-js/enc-hex");
15
+ crypto_js_enc_hex = require_rolldown_runtime.__toESM(crypto_js_enc_hex);
16
+
17
+ //#region src/crypter/aes-legacy.ts
37
18
  const encoders = {
38
- latin1: enc_latin1_1.default,
39
- utf8: enc_utf8_1.default,
40
- hex: enc_hex_1.default,
41
- utf16: enc_utf16_1.default,
42
- base64: enc_base64_1.default,
19
+ latin1: crypto_js_enc_latin1.default,
20
+ utf8: crypto_js_enc_utf8.default,
21
+ hex: crypto_js_enc_hex.default,
22
+ utf16: crypto_js_enc_utf16.default,
23
+ base64: crypto_js_enc_base64.default
24
+ };
25
+ var AesCrypter = class extends require_protocols_crypter.default {
26
+ encrypt(message, secret) {
27
+ const text = typeof message === "string" ? message : JSON.stringify(message);
28
+ return crypto_js_aes.encrypt(text, secret).toString();
29
+ }
30
+ decrypt(cipher, secret, outputEncoding = "utf8") {
31
+ return crypto_js_aes.decrypt(cipher, secret).toString(encoders[outputEncoding]);
32
+ }
43
33
  };
44
- // AES-CBC-256
45
- class AesCrypter extends crypter_1.default {
46
- encrypt(message, secret) {
47
- const text = typeof message === 'string' ? message : JSON.stringify(message);
48
- return AES.encrypt(text, secret).toString();
49
- }
50
- decrypt(cipher, secret, outputEncoding = 'utf8') {
51
- return AES.decrypt(cipher, secret).toString(encoders[outputEncoding]);
52
- }
53
- }
34
+ var aes_legacy_default = new AesCrypter();
35
+
36
+ //#endregion
54
37
  exports.AesCrypter = AesCrypter;
55
- exports.default = new AesCrypter();
38
+ exports.default = aes_legacy_default;
@@ -1,9 +1,11 @@
1
- import { BytesType, EncodingType } from '@ocap/util';
2
- import Crypter from '../protocols/crypter';
3
- declare class AesCrypter extends Crypter {
4
- encrypt(message: BytesType, secret: BytesType, encoding?: EncodingType): BytesType;
5
- decrypt(message: BytesType, secret: BytesType, encoding?: EncodingType): BytesType;
1
+ import _default$1 from "../protocols/crypter.js";
2
+ import { BytesType, EncodingType } from "@ocap/util";
3
+
4
+ //#region src/crypter/aes.d.ts
5
+ declare class AesCrypter extends _default$1 {
6
+ encrypt(message: BytesType, secret: BytesType, encoding?: EncodingType): BytesType;
7
+ decrypt(message: BytesType, secret: BytesType, encoding?: EncodingType): BytesType;
6
8
  }
7
9
  declare const _default: AesCrypter;
8
- export default _default;
9
- export { AesCrypter };
10
+ //#endregion
11
+ export { AesCrypter, _default as default };
@@ -1,31 +1,31 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
1
+ Object.defineProperty(exports, '__esModule', { value: true });
2
+ const require_rolldown_runtime = require('../_virtual/rolldown_runtime.js');
3
+ const require_encode = require('../encode.js');
4
+ const require_hasher_sha3 = require('../hasher/sha3.js');
5
+ const require_protocols_crypter = require('../protocols/crypter.js');
6
+ let _ocap_util = require("@ocap/util");
7
+ let crypto = require("crypto");
8
+ crypto = require_rolldown_runtime.__toESM(crypto);
9
+
10
+ //#region src/crypter/aes.ts
11
+ var AesCrypter = class extends require_protocols_crypter.default {
12
+ encrypt(message, secret, encoding = "hex") {
13
+ const key = require_hasher_sha3.default.hash256(secret, 1, "buffer");
14
+ const cipher = crypto.default.createCipheriv("aes-256-ecb", key, "");
15
+ cipher.setAutoPadding(true);
16
+ const output = cipher.update((0, _ocap_util.toBuffer)(message));
17
+ return require_encode.encode(Buffer.concat([output, cipher.final()]), encoding);
18
+ }
19
+ decrypt(message, secret, encoding = "hex") {
20
+ const key = require_hasher_sha3.default.hash256(secret, 1, "buffer");
21
+ const decipher = crypto.default.createDecipheriv("aes-256-ecb", key, "");
22
+ decipher.setAutoPadding(true);
23
+ const output = decipher.update((0, _ocap_util.toBuffer)(message));
24
+ return require_encode.encode(Buffer.concat([output, decipher.final()]), encoding);
25
+ }
4
26
  };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.AesCrypter = void 0;
7
- // For browsers, may need: https://www.npmjs.com/package/crypto-browserify
8
- const crypto_1 = __importDefault(require("crypto"));
9
- const util_1 = require("@ocap/util");
10
- const crypter_1 = __importDefault(require("../protocols/crypter"));
11
- const sha3_1 = __importDefault(require("../hasher/sha3"));
12
- const encode_1 = require("../encode");
13
- // AES-ECB-256
14
- class AesCrypter extends crypter_1.default {
15
- encrypt(message, secret, encoding = 'hex') {
16
- const key = sha3_1.default.hash256(secret, 1, 'buffer');
17
- const cipher = crypto_1.default.createCipheriv('aes-256-ecb', key, '');
18
- cipher.setAutoPadding(true);
19
- const output = cipher.update((0, util_1.toBuffer)(message));
20
- return (0, encode_1.encode)(Buffer.concat([output, cipher.final()]), encoding);
21
- }
22
- decrypt(message, secret, encoding = 'hex') {
23
- const key = sha3_1.default.hash256(secret, 1, 'buffer');
24
- const decipher = crypto_1.default.createDecipheriv('aes-256-ecb', key, '');
25
- decipher.setAutoPadding(true);
26
- const output = decipher.update((0, util_1.toBuffer)(message));
27
- return (0, encode_1.encode)(Buffer.concat([output, decipher.final()]), encoding);
28
- }
29
- }
27
+ var aes_default = new AesCrypter();
28
+
29
+ //#endregion
30
30
  exports.AesCrypter = AesCrypter;
31
- exports.default = new AesCrypter();
31
+ exports.default = aes_default;
@@ -1,9 +1,10 @@
1
+ //#region src/crypter/rsa-browserify.d.ts
1
2
  declare class RSABrowserCrypter {
2
- genKeyPair(length?: number): Promise<CryptoKeyPair>;
3
- formatPublicKey(key: CryptoKey): Promise<string>;
4
- encrypt(message: string, key: CryptoKey): Promise<string>;
5
- decrypt(message: string, key: CryptoKey): Promise<string>;
3
+ genKeyPair(length?: number): Promise<CryptoKeyPair>;
4
+ formatPublicKey(key: CryptoKey): Promise<string>;
5
+ encrypt(message: string, key: CryptoKey): Promise<string>;
6
+ decrypt(message: string, key: CryptoKey): Promise<string>;
6
7
  }
7
8
  declare const _default: RSABrowserCrypter;
8
- export default _default;
9
- export { RSABrowserCrypter };
9
+ //#endregion
10
+ export { RSABrowserCrypter, _default as default };
@@ -1,36 +1,39 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RSABrowserCrypter = void 0;
4
- // https://stackoverflow.com/questions/70056340/how-can-i-generate-an-rsa-pair-that-works-both-in-node-js-and-browser
5
- // https://stackoverflow.com/questions/62948516/using-native-javascript-subtlecrypto-to-encrypt-using-rsa
6
- const util_1 = require("@ocap/util");
1
+ Object.defineProperty(exports, '__esModule', { value: true });
2
+ const require_rolldown_runtime = require('../_virtual/rolldown_runtime.js');
3
+ let _ocap_util = require("@ocap/util");
4
+
5
+ //#region src/crypter/rsa-browserify.ts
7
6
  const crypto = window.crypto.subtle;
8
- // @ts-ignore
9
7
  const ab2str = (buffer) => String.fromCharCode.apply(null, new Uint8Array(buffer));
10
- const RSA_ALGORITHM = 'RSA-OAEP';
11
- // RSA-OAEP
12
- class RSABrowserCrypter {
13
- genKeyPair(length = 2048) {
14
- return crypto.generateKey({
15
- name: RSA_ALGORITHM,
16
- modulusLength: length,
17
- publicExponent: new Uint8Array([1, 0, 1]),
18
- hash: 'SHA-256',
19
- }, true, ['encrypt', 'decrypt']);
20
- }
21
- async formatPublicKey(key) {
22
- const exported = await crypto.exportKey('spki', key);
23
- const base64 = window.btoa(ab2str(exported));
24
- return `-----BEGIN PUBLIC KEY-----\n${base64}\n-----END PUBLIC KEY-----`;
25
- }
26
- async encrypt(message, key) {
27
- const encrypted = await crypto.encrypt({ name: RSA_ALGORITHM }, key, new TextEncoder().encode(message));
28
- return (0, util_1.toBase58)(new Uint8Array(encrypted));
29
- }
30
- async decrypt(message, key) {
31
- const decrypted = await crypto.decrypt({ name: RSA_ALGORITHM }, key, (0, util_1.fromBase58)(message));
32
- return Buffer.from(new Uint8Array(decrypted)).toString('utf8');
33
- }
34
- }
8
+ const RSA_ALGORITHM = "RSA-OAEP";
9
+ var RSABrowserCrypter = class {
10
+ genKeyPair(length = 2048) {
11
+ return crypto.generateKey({
12
+ name: RSA_ALGORITHM,
13
+ modulusLength: length,
14
+ publicExponent: new Uint8Array([
15
+ 1,
16
+ 0,
17
+ 1
18
+ ]),
19
+ hash: "SHA-256"
20
+ }, true, ["encrypt", "decrypt"]);
21
+ }
22
+ async formatPublicKey(key) {
23
+ const exported = await crypto.exportKey("spki", key);
24
+ return `-----BEGIN PUBLIC KEY-----\n${window.btoa(ab2str(exported))}\n-----END PUBLIC KEY-----`;
25
+ }
26
+ async encrypt(message, key) {
27
+ const encrypted = await crypto.encrypt({ name: RSA_ALGORITHM }, key, new TextEncoder().encode(message));
28
+ return (0, _ocap_util.toBase58)(new Uint8Array(encrypted));
29
+ }
30
+ async decrypt(message, key) {
31
+ const decrypted = await crypto.decrypt({ name: RSA_ALGORITHM }, key, (0, _ocap_util.fromBase58)(message));
32
+ return Buffer.from(new Uint8Array(decrypted)).toString("utf8");
33
+ }
34
+ };
35
+ var rsa_browserify_default = new RSABrowserCrypter();
36
+
37
+ //#endregion
35
38
  exports.RSABrowserCrypter = RSABrowserCrypter;
36
- exports.default = new RSABrowserCrypter();
39
+ exports.default = rsa_browserify_default;
@@ -1,11 +1,13 @@
1
- import crypto from 'crypto';
2
- import { BytesType, EncodingType } from '@ocap/util';
3
- import Crypter from '../protocols/crypter';
4
- declare class RSACrypter extends Crypter {
5
- genKeyPair(length?: number): crypto.KeyPairSyncResult<string, string>;
6
- encrypt(message: BytesType, key: string, encoding?: EncodingType): BytesType;
7
- decrypt(message: BytesType, key: string, encoding?: EncodingType): BytesType;
1
+ import _default$1 from "../protocols/crypter.js";
2
+ import { BytesType, EncodingType } from "@ocap/util";
3
+ import crypto from "crypto";
4
+
5
+ //#region src/crypter/rsa.d.ts
6
+ declare class RSACrypter extends _default$1 {
7
+ genKeyPair(length?: number): crypto.KeyPairSyncResult<string, string>;
8
+ encrypt(message: BytesType, key: string, encoding?: EncodingType): BytesType;
9
+ decrypt(message: BytesType, key: string, encoding?: EncodingType): BytesType;
8
10
  }
9
11
  declare const _default: RSACrypter;
10
- export default _default;
11
- export { RSACrypter };
12
+ //#endregion
13
+ export { RSACrypter, _default as default };
@@ -1,34 +1,35 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
1
+ Object.defineProperty(exports, '__esModule', { value: true });
2
+ const require_rolldown_runtime = require('../_virtual/rolldown_runtime.js');
3
+ const require_encode = require('../encode.js');
4
+ const require_protocols_crypter = require('../protocols/crypter.js');
5
+ let _ocap_util = require("@ocap/util");
6
+ let crypto = require("crypto");
7
+ crypto = require_rolldown_runtime.__toESM(crypto);
8
+
9
+ //#region src/crypter/rsa.ts
10
+ var RSACrypter = class extends require_protocols_crypter.default {
11
+ genKeyPair(length = 2048) {
12
+ return crypto.default.generateKeyPairSync("rsa", {
13
+ modulusLength: length,
14
+ publicKeyEncoding: {
15
+ type: "spki",
16
+ format: "pem"
17
+ },
18
+ privateKeyEncoding: {
19
+ type: "pkcs8",
20
+ format: "pem"
21
+ }
22
+ });
23
+ }
24
+ encrypt(message, key, encoding = "hex") {
25
+ return require_encode.encode(crypto.default.publicEncrypt(key, (0, _ocap_util.toBuffer)(message)), encoding);
26
+ }
27
+ decrypt(message, key, encoding = "hex") {
28
+ return require_encode.encode(crypto.default.privateDecrypt(key, (0, _ocap_util.toBuffer)(message)), encoding);
29
+ }
4
30
  };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.RSACrypter = void 0;
7
- const crypto_1 = __importDefault(require("crypto"));
8
- const util_1 = require("@ocap/util");
9
- const crypter_1 = __importDefault(require("../protocols/crypter"));
10
- const encode_1 = require("../encode");
11
- // RSA-OAEP
12
- class RSACrypter extends crypter_1.default {
13
- genKeyPair(length = 2048) {
14
- return crypto_1.default.generateKeyPairSync('rsa', {
15
- modulusLength: length,
16
- publicKeyEncoding: {
17
- type: 'spki',
18
- format: 'pem',
19
- },
20
- privateKeyEncoding: {
21
- type: 'pkcs8',
22
- format: 'pem',
23
- },
24
- });
25
- }
26
- encrypt(message, key, encoding = 'hex') {
27
- return (0, encode_1.encode)(crypto_1.default.publicEncrypt(key, (0, util_1.toBuffer)(message)), encoding);
28
- }
29
- decrypt(message, key, encoding = 'hex') {
30
- return (0, encode_1.encode)(crypto_1.default.privateDecrypt(key, (0, util_1.toBuffer)(message)), encoding);
31
- }
32
- }
31
+ var rsa_default = new RSACrypter();
32
+
33
+ //#endregion
33
34
  exports.RSACrypter = RSACrypter;
34
- exports.default = new RSACrypter();
35
+ exports.default = rsa_default;
package/lib/encode.d.ts CHANGED
@@ -1,8 +1,12 @@
1
- import { BytesType, EncodingType } from '@ocap/util';
2
- export declare function encode(data: BytesType, encoding?: 'hex'): string;
3
- export declare function encode(data: BytesType, encoding?: 'base16'): string;
4
- export declare function encode(data: BytesType, encoding?: 'base58'): string;
5
- export declare function encode(data: BytesType, encoding?: 'base64'): string;
6
- export declare function encode(data: BytesType, encoding?: 'buffer'): Buffer;
7
- export declare function encode(data: BytesType, encoding?: 'Uint8Array'): Uint8Array;
8
- export declare function encode(data: BytesType, encoding?: EncodingType): BytesType;
1
+ import { BytesType, EncodingType } from "@ocap/util";
2
+
3
+ //#region src/encode.d.ts
4
+ declare function encode(data: BytesType, encoding?: 'hex'): string;
5
+ declare function encode(data: BytesType, encoding?: 'base16'): string;
6
+ declare function encode(data: BytesType, encoding?: 'base58'): string;
7
+ declare function encode(data: BytesType, encoding?: 'base64'): string;
8
+ declare function encode(data: BytesType, encoding?: 'buffer'): Buffer;
9
+ declare function encode(data: BytesType, encoding?: 'Uint8Array'): Uint8Array;
10
+ declare function encode(data: BytesType, encoding?: EncodingType): BytesType;
11
+ //#endregion
12
+ export { encode };
package/lib/encode.js CHANGED
@@ -1,22 +1,15 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.encode = encode;
4
- const util_1 = require("@ocap/util");
5
- function encode(data, encoding = 'hex') {
6
- if (['hex', 'base16'].includes(encoding)) {
7
- return (0, util_1.toHex)(data);
8
- }
9
- if (encoding === 'base58') {
10
- return (0, util_1.toBase58)(data);
11
- }
12
- if (encoding === 'base64') {
13
- return (0, util_1.toBase64)(data);
14
- }
15
- if (encoding === 'Uint8Array') {
16
- return (0, util_1.toUint8Array)(data);
17
- }
18
- if (encoding === 'buffer') {
19
- return (0, util_1.toBuffer)(data);
20
- }
21
- return data;
1
+ const require_rolldown_runtime = require('./_virtual/rolldown_runtime.js');
2
+ let _ocap_util = require("@ocap/util");
3
+
4
+ //#region src/encode.ts
5
+ function encode(data, encoding = "hex") {
6
+ if (["hex", "base16"].includes(encoding)) return (0, _ocap_util.toHex)(data);
7
+ if (encoding === "base58") return (0, _ocap_util.toBase58)(data);
8
+ if (encoding === "base64") return (0, _ocap_util.toBase64)(data);
9
+ if (encoding === "Uint8Array") return (0, _ocap_util.toUint8Array)(data);
10
+ if (encoding === "buffer") return (0, _ocap_util.toBuffer)(data);
11
+ return data;
22
12
  }
13
+
14
+ //#endregion
15
+ exports.encode = encode;