-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.ts
86 lines (75 loc) · 2.68 KB
/
index.ts
1
2
3
4
5
6
7
8
9
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
85
86
import { concatBytes } from "@noble/ciphers/utils";
import {
ephemeralKeySize,
isEphemeralKeyCompressed,
isHkdfKeyCompressed,
} from "./config";
import { PrivateKey, PublicKey } from "./keys";
import {
aesDecrypt,
aesEncrypt,
decodeHex,
getValidSecret,
remove0x,
symDecrypt,
symEncrypt,
} from "./utils";
/**
* Encrypts data with a receiver's public key.
* @description From version 0.5.0, `Uint8Array` will be returned instead of `Buffer`.
* To keep the same behavior, use `Buffer.from(encrypt(...))`.
*
* @param receiverRawPK - Raw public key of the receiver, either as a hex string or a Uint8Array.
* @param data - Data to encrypt.
* @returns Encrypted payload, format: `public key || encrypted`.
*/
export function encrypt(receiverRawPK: string | Uint8Array, data: Uint8Array): Buffer {
return Buffer.from(_encrypt(receiverRawPK, data));
}
function _encrypt(receiverRawPK: string | Uint8Array, data: Uint8Array): Uint8Array {
const ephemeralSK = new PrivateKey();
const receiverPK =
receiverRawPK instanceof Uint8Array
? new PublicKey(receiverRawPK)
: PublicKey.fromHex(receiverRawPK);
const sharedKey = ephemeralSK.encapsulate(receiverPK, isHkdfKeyCompressed());
const ephemeralPK = ephemeralSK.publicKey.toBytes(isEphemeralKeyCompressed());
const encrypted = symEncrypt(sharedKey, data);
return concatBytes(ephemeralPK, encrypted);
}
/**
* Decrypts data with a receiver's private key.
* @description From version 0.5.0, `Uint8Array` will be returned instead of `Buffer`.
* To keep the same behavior, use `Buffer.from(decrypt(...))`.
*
* @param receiverRawSK - Raw private key of the receiver, either as a hex string or a Uint8Array.
* @param data - Data to decrypt.
* @returns Decrypted plain text.
*/
export function decrypt(receiverRawSK: string | Uint8Array, data: Uint8Array): Buffer {
return Buffer.from(_decrypt(receiverRawSK, data));
}
function _decrypt(receiverRawSK: string | Uint8Array, data: Uint8Array): Uint8Array {
const receiverSK =
receiverRawSK instanceof Uint8Array
? new PrivateKey(receiverRawSK)
: PrivateKey.fromHex(receiverRawSK);
const keySize = ephemeralKeySize();
const ephemeralPK = new PublicKey(data.subarray(0, keySize));
const encrypted = data.subarray(keySize);
const sharedKey = ephemeralPK.decapsulate(receiverSK, isHkdfKeyCompressed());
return symDecrypt(sharedKey, encrypted);
}
export { ECIES_CONFIG } from "./config";
export { PrivateKey, PublicKey } from "./keys";
/** @deprecated - use `import utils from "eciesjs/utils"` instead. */
export const utils = {
// TODO: remove these after 0.5.0
aesEncrypt,
aesDecrypt,
symEncrypt,
symDecrypt,
decodeHex,
getValidSecret,
remove0x,
};