forked from fybx/zkl-crypto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto.js
122 lines (111 loc) · 3.71 KB
/
crypto.js
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import init, * as ecies from "ecies-wasm";
import { Key } from "./zkl-kds/key";
init();
/**
* Encrypts given data for the recipient
*
* @param {Key} publicKey The public key of the recipient
* @param {Uint8Array} data The plaintext data
* @returns {Uint8Array} The ciphertext data
* @throws {TypeError} If arguments are of incorrect types
*/
export function encryptFile(publicKey, data) {
if (!(publicKey instanceof Key)) {
throw new TypeError("publicKey must be an instance of Key");
}
if (!(data instanceof Uint8Array)) {
throw new TypeError("data must be an instance of Uint8Array");
}
return encrypt(publicKey, data);
}
/**
* Decrypts given data with recipient's private key
*
* @param {Key} privateKey The private key of the recipient
* @param {Uint8Array} data The ciphertext data
* @returns {Uint8Array} The plaintext data
* @throws {TypeError} If arguments are of incorrect types
*/
export function decryptFile(privateKey, data) {
if (!(privateKey instanceof Key)) {
throw new TypeError("privateKey must be an instance of Key");
}
if (!(data instanceof Uint8Array)) {
throw new TypeError("data must be an instance of Uint8Array");
}
return decrypt(privateKey, data);
}
/**
* Encrypts given string for the recipient
*
* @param {Key} publicKey The public key of the recipient
* @param {string} string The plaintext
* @returns {string} The ciphertext as hexadecimal string
* @throws {TypeError} If arguments are of incorrect types
*/
export function encryptString(publicKey, string) {
if (!(publicKey instanceof Key)) {
throw new TypeError("publicKey must be an instance of Key");
}
if (typeof string !== "string") {
throw new TypeError("string must be of type string");
}
const encoder = new TextEncoder();
const byteArray = encoder.encode(string);
const encryptedData = encrypt(publicKey, byteArray);
return encryptedData.asHexString();
}
/**
* Decrypts given string of ciphertext in hexadecimal
*
* @param {Key} privateKey The private key of the recipient
* @param {string} string The ciphertext string in hexadecimal
* @returns {string} The plaintext
* @throws {TypeError} If arguments are of incorrect types
*/
export function decryptString(privateKey, string) {
if (!(privateKey instanceof Key)) {
throw new TypeError("privateKey must be an instance of Key");
}
if (typeof string !== "string") {
throw new TypeError("string must be of type string");
}
if (!/^[0-9a-fA-F]+$/.test(string)) {
console.warn("string does not seem to be a valid hexadecimal string");
}
const byteArray = Uint8Array.from(
string.match(/.{1,2}/g).map((byte) => parseInt(byte, 16))
);
const decryptedData = decrypt(privateKey, byteArray);
const decoder = new TextDecoder();
return decoder.decode(decryptedData);
}
function encrypt(publicKey, plaintext) {
if (!(publicKey instanceof Key)) {
throw new TypeError("publicKey must be an instance of Key");
}
if (!(plaintext instanceof Uint8Array)) {
throw new TypeError("plaintext must be an instance of Uint8Array");
}
return ecies.encrypt(publicKey.asByteArray, plaintext);
}
function decrypt(privateKey, ciphertext) {
if (!(privateKey instanceof Key)) {
throw new TypeError("privateKey must be an instance of Key");
}
if (!(ciphertext instanceof Uint8Array)) {
throw new TypeError("ciphertext must be an instance of Uint8Array");
}
return ecies.decrypt(privateKey.asByteArray, ciphertext);
}
function asHexString() {
return this.reduce(
(str, byte) => str + byte.toString(16).padStart(2, "0"),
""
);
}
if (!Uint8Array.prototype.asHexString) {
Uint8Array.prototype.asHexString = asHexString;
} else {
console.warn("asHexString method already exists on Uint8Array.prototype");
}