@ocap/mcrypto 1.18.40-beta.1 → 1.18.40-beta.3

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.
@@ -0,0 +1,9 @@
1
+ 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>;
6
+ }
7
+ declare const _default: RSABrowserCrypter;
8
+ export default _default;
9
+ export { RSABrowserCrypter };
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.RSABrowserCrypter = void 0;
13
+ // https://stackoverflow.com/questions/70056340/how-can-i-generate-an-rsa-pair-that-works-both-in-node-js-and-browser
14
+ // https://stackoverflow.com/questions/62948516/using-native-javascript-subtlecrypto-to-encrypt-using-rsa
15
+ const util_1 = require("@ocap/util");
16
+ const crypto = window.crypto.subtle;
17
+ // @ts-ignore
18
+ const ab2str = (buffer) => String.fromCharCode.apply(null, new Uint8Array(buffer));
19
+ const RSA_ALGORITHM = 'RSA-OAEP';
20
+ // RSA-OAEP
21
+ class RSABrowserCrypter {
22
+ genKeyPair(length = 2048) {
23
+ return __awaiter(this, void 0, void 0, function* () {
24
+ return crypto.generateKey({
25
+ name: RSA_ALGORITHM,
26
+ modulusLength: length,
27
+ publicExponent: new Uint8Array([1, 0, 1]),
28
+ hash: 'SHA-256',
29
+ }, true, ['encrypt', 'decrypt']);
30
+ });
31
+ }
32
+ formatPublicKey(key) {
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ const exported = yield crypto.exportKey('spki', key);
35
+ const base64 = window.btoa(ab2str(exported));
36
+ return `-----BEGIN PUBLIC KEY-----\n${base64}\n-----END PUBLIC KEY-----`;
37
+ });
38
+ }
39
+ encrypt(message, key) {
40
+ return __awaiter(this, void 0, void 0, function* () {
41
+ const encrypted = yield crypto.encrypt({ name: RSA_ALGORITHM }, key, new TextEncoder().encode(message));
42
+ return (0, util_1.toBase58)(new Uint8Array(encrypted));
43
+ });
44
+ }
45
+ decrypt(message, key) {
46
+ return __awaiter(this, void 0, void 0, function* () {
47
+ const decrypted = yield crypto.decrypt({ name: RSA_ALGORITHM }, key, (0, util_1.fromBase58)(message));
48
+ return Buffer.from(new Uint8Array(decrypted)).toString('utf8');
49
+ });
50
+ }
51
+ }
52
+ exports.RSABrowserCrypter = RSABrowserCrypter;
53
+ exports.default = new RSABrowserCrypter();
@@ -4,8 +4,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.RSACrypter = void 0;
7
- // For browsers, just use native supporto
8
- // https://stackoverflow.com/questions/70056340/how-can-i-generate-an-rsa-pair-that-works-both-in-node-js-and-browser
9
7
  const crypto_1 = __importDefault(require("crypto"));
10
8
  const util_1 = require("@ocap/util");
11
9
  const crypter_1 = __importDefault(require("../protocols/crypter"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ocap/mcrypto",
3
- "version": "1.18.40-beta.1",
3
+ "version": "1.18.40-beta.3",
4
4
  "description": "Crypto lib that provides signer,crypter,hasher interface",
5
5
  "keywords": [
6
6
  "crypto",