@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.
- package/esm/crypter/aes-legacy.d.ts +8 -6
- package/esm/crypter/aes-legacy.js +27 -24
- package/esm/crypter/aes.d.ts +9 -7
- package/esm/crypter/aes.js +27 -25
- package/esm/crypter/rsa-browserify.d.ts +7 -6
- package/esm/crypter/rsa-browserify.js +34 -31
- package/esm/crypter/rsa.d.ts +11 -9
- package/esm/crypter/rsa.js +31 -28
- package/esm/encode.d.ts +12 -8
- package/esm/encode.js +13 -18
- package/esm/hasher/keccak.d.ts +8 -5
- package/esm/hasher/keccak.js +37 -36
- package/esm/hasher/sha2.d.ts +8 -5
- package/esm/hasher/sha2.js +42 -40
- package/esm/hasher/sha3.d.ts +8 -5
- package/esm/hasher/sha3.js +37 -36
- package/esm/index.d.ts +148 -143
- package/esm/index.js +172 -209
- package/esm/protocols/crypter.d.ts +3 -1
- package/esm/protocols/crypter.js +7 -4
- package/esm/protocols/hasher.d.ts +3 -1
- package/esm/protocols/hasher.js +7 -4
- package/esm/protocols/signer.d.ts +3 -1
- package/esm/protocols/signer.js +7 -4
- package/esm/signer/ed25519.d.ts +49 -46
- package/esm/signer/ed25519.js +80 -80
- package/esm/signer/ethereum.d.ts +11 -8
- package/esm/signer/ethereum.js +34 -34
- package/esm/signer/passkey.d.ts +21 -18
- package/esm/signer/passkey.js +49 -58
- package/esm/signer/secp256k1.d.ts +35 -32
- package/esm/signer/secp256k1.js +80 -93
- package/lib/_virtual/rolldown_runtime.js +29 -0
- package/lib/crypter/aes-legacy.d.ts +8 -6
- package/lib/crypter/aes-legacy.js +35 -52
- package/lib/crypter/aes.d.ts +9 -7
- package/lib/crypter/aes.js +29 -29
- package/lib/crypter/rsa-browserify.d.ts +7 -6
- package/lib/crypter/rsa-browserify.js +36 -33
- package/lib/crypter/rsa.d.ts +11 -9
- package/lib/crypter/rsa.js +33 -32
- package/lib/encode.d.ts +12 -8
- package/lib/encode.js +14 -21
- package/lib/hasher/keccak.d.ts +8 -5
- package/lib/hasher/keccak.js +40 -41
- package/lib/hasher/sha2.d.ts +8 -5
- package/lib/hasher/sha2.js +43 -42
- package/lib/hasher/sha3.d.ts +8 -5
- package/lib/hasher/sha3.js +40 -41
- package/lib/index.d.ts +148 -143
- package/lib/index.js +177 -216
- package/lib/protocols/crypter.d.ts +3 -1
- package/lib/protocols/crypter.js +10 -9
- package/lib/protocols/hasher.d.ts +3 -1
- package/lib/protocols/hasher.js +10 -9
- package/lib/protocols/signer.d.ts +3 -1
- package/lib/protocols/signer.js +10 -9
- package/lib/signer/ed25519.d.ts +49 -46
- package/lib/signer/ed25519.js +85 -86
- package/lib/signer/ethereum.d.ts +11 -8
- package/lib/signer/ethereum.js +38 -39
- package/lib/signer/passkey.d.ts +21 -18
- package/lib/signer/passkey.js +51 -63
- package/lib/signer/secp256k1.d.ts +35 -32
- package/lib/signer/secp256k1.js +85 -99
- package/package.json +21 -10
package/esm/signer/secp256k1.js
CHANGED
|
@@ -1,95 +1,82 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
|
|
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(
|
|
9
|
+
const secp256k1 = new EC("secp256k1");
|
|
10
10
|
/**
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
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
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
-
|
|
8
|
-
export { AesCrypter };
|
|
9
|
+
//#endregion
|
|
10
|
+
export { AesCrypter, _default as default };
|
|
@@ -1,55 +1,38 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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 =
|
|
38
|
+
exports.default = aes_legacy_default;
|
package/lib/crypter/aes.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
|
|
9
|
-
export { AesCrypter };
|
|
10
|
+
//#endregion
|
|
11
|
+
export { AesCrypter, _default as default };
|
package/lib/crypter/aes.js
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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 =
|
|
31
|
+
exports.default = aes_default;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
//#region src/crypter/rsa-browserify.d.ts
|
|
1
2
|
declare class RSABrowserCrypter {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
|
|
9
|
-
export { RSABrowserCrypter };
|
|
9
|
+
//#endregion
|
|
10
|
+
export { RSABrowserCrypter, _default as default };
|
|
@@ -1,36 +1,39 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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 =
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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 =
|
|
39
|
+
exports.default = rsa_browserify_default;
|
package/lib/crypter/rsa.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { BytesType, EncodingType } from
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
|
|
11
|
-
export { RSACrypter };
|
|
12
|
+
//#endregion
|
|
13
|
+
export { RSACrypter, _default as default };
|
package/lib/crypter/rsa.js
CHANGED
|
@@ -1,34 +1,35 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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 =
|
|
35
|
+
exports.default = rsa_default;
|
package/lib/encode.d.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
import { BytesType, EncodingType } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
function encode(data, encoding =
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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;
|