@ocap/mcrypto 1.18.39 → 1.18.40-beta.2
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.
- package/lib/crypter/aes.d.ts +3 -2
- package/lib/crypter/aes.js +18 -42
- package/lib/crypter/rsa-browserify.d.ts +9 -0
- package/lib/crypter/rsa-browserify.js +53 -0
- package/lib/crypter/rsa.d.ts +12 -0
- package/lib/crypter/rsa.js +34 -0
- package/package.json +2 -3
package/lib/crypter/aes.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { BytesType, EncodingType } from '@ocap/util';
|
|
1
2
|
import Crypter from '../protocols/crypter';
|
|
2
3
|
declare class AesCrypter extends Crypter {
|
|
3
|
-
encrypt(message:
|
|
4
|
-
decrypt(
|
|
4
|
+
encrypt(message: BytesType, secret: BytesType, encoding?: EncodingType): BytesType;
|
|
5
|
+
decrypt(message: BytesType, secret: BytesType, encoding?: EncodingType): BytesType;
|
|
5
6
|
}
|
|
6
7
|
declare const _default: AesCrypter;
|
|
7
8
|
export default _default;
|
package/lib/crypter/aes.js
CHANGED
|
@@ -1,54 +1,30 @@
|
|
|
1
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
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
4
|
};
|
|
28
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
6
|
exports.AesCrypter = void 0;
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
const
|
|
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"));
|
|
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");
|
|
36
10
|
const crypter_1 = __importDefault(require("../protocols/crypter"));
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
hex: enc_hex_1.default,
|
|
41
|
-
utf16: enc_utf16_1.default,
|
|
42
|
-
base64: enc_base64_1.default,
|
|
43
|
-
};
|
|
44
|
-
// AES-CBC-256
|
|
11
|
+
const sha3_1 = __importDefault(require("../hasher/sha3"));
|
|
12
|
+
const encode_1 = require("../encode");
|
|
13
|
+
// AES-ECB-256
|
|
45
14
|
class AesCrypter extends crypter_1.default {
|
|
46
|
-
encrypt(message, secret) {
|
|
47
|
-
const
|
|
48
|
-
|
|
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);
|
|
49
21
|
}
|
|
50
|
-
decrypt(
|
|
51
|
-
|
|
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);
|
|
52
28
|
}
|
|
53
29
|
}
|
|
54
30
|
exports.AesCrypter = AesCrypter;
|
|
@@ -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();
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import crypto from 'crypto';
|
|
3
|
+
import { BytesType, EncodingType } from '@ocap/util';
|
|
4
|
+
import Crypter from '../protocols/crypter';
|
|
5
|
+
declare class RSACrypter extends Crypter {
|
|
6
|
+
genKeyPair(length?: number): crypto.KeyPairSyncResult<string, string>;
|
|
7
|
+
encrypt(message: BytesType, key: string, encoding?: EncodingType): BytesType;
|
|
8
|
+
decrypt(message: BytesType, key: string, encoding?: EncodingType): BytesType;
|
|
9
|
+
}
|
|
10
|
+
declare const _default: RSACrypter;
|
|
11
|
+
export default _default;
|
|
12
|
+
export { RSACrypter };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
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
|
+
}
|
|
33
|
+
exports.RSACrypter = RSACrypter;
|
|
34
|
+
exports.default = new RSACrypter();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ocap/mcrypto",
|
|
3
|
-
"version": "1.18.
|
|
3
|
+
"version": "1.18.40-beta.2",
|
|
4
4
|
"description": "Crypto lib that provides signer,crypter,hasher interface",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"crypto",
|
|
@@ -71,6 +71,5 @@
|
|
|
71
71
|
"resolutions": {
|
|
72
72
|
"bn.js": "5.2.1",
|
|
73
73
|
"elliptic": "6.5.3"
|
|
74
|
-
}
|
|
75
|
-
"gitHead": "95f7a60030ed923ac83fed697f208a9b8d555f59"
|
|
74
|
+
}
|
|
76
75
|
}
|