@ocap/mcrypto 1.18.39 → 1.18.40-beta.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.
- package/lib/crypter/aes.d.ts +3 -2
- package/lib/crypter/aes.js +18 -42
- package/lib/crypter/rsa.d.ts +12 -0
- package/lib/crypter/rsa.js +36 -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,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,36 @@
|
|
|
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
|
+
// 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
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
10
|
+
const util_1 = require("@ocap/util");
|
|
11
|
+
const crypter_1 = __importDefault(require("../protocols/crypter"));
|
|
12
|
+
const encode_1 = require("../encode");
|
|
13
|
+
// RSA-OAEP
|
|
14
|
+
class RSACrypter extends crypter_1.default {
|
|
15
|
+
genKeyPair(length = 2048) {
|
|
16
|
+
return crypto_1.default.generateKeyPairSync('rsa', {
|
|
17
|
+
modulusLength: length,
|
|
18
|
+
publicKeyEncoding: {
|
|
19
|
+
type: 'spki',
|
|
20
|
+
format: 'pem',
|
|
21
|
+
},
|
|
22
|
+
privateKeyEncoding: {
|
|
23
|
+
type: 'pkcs8',
|
|
24
|
+
format: 'pem',
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
encrypt(message, key, encoding = 'hex') {
|
|
29
|
+
return (0, encode_1.encode)(crypto_1.default.publicEncrypt(key, (0, util_1.toBuffer)(message)), encoding);
|
|
30
|
+
}
|
|
31
|
+
decrypt(message, key, encoding = 'hex') {
|
|
32
|
+
return (0, encode_1.encode)(crypto_1.default.privateDecrypt(key, (0, util_1.toBuffer)(message)), encoding);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports.RSACrypter = RSACrypter;
|
|
36
|
+
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.1",
|
|
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
|
}
|