-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.ts
64 lines (56 loc) · 1.61 KB
/
crypto.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
import init, {
generate_key,
generate_nonce,
encrypt,
decrypt,
} from "./aes-gcm/pkg/aes_gcm_wasm.js";
const encoder = new TextEncoder();
const decoder = new TextDecoder();
export { init };
export const fromUint8ArrayToHexString = (array: Uint8Array): string => {
let result = "";
for (let i = 0; i < array.length; i++) {
const value = array[i].toString(16);
result += value.length === 1 ? "0" + value : value;
}
return result;
};
export const fromHexToUint8Array = (hexStr: string): Uint8Array => {
const result = [];
for (let i = 0; i < hexStr.length; i += 2) {
result.push(parseInt(hexStr.substr(i, 2), 16));
}
return new Uint8Array(result);
};
export const generateKey = (): string => {
const buf = generate_key();
return fromUint8ArrayToHexString(buf);
};
export const generateNonce = (): string => {
const buf = generate_nonce();
return fromUint8ArrayToHexString(buf);
};
export const encryptString = (
plaintext: string,
key: string,
nonce: string
): string => {
const _key = fromHexToUint8Array(key);
const _nonce = fromHexToUint8Array(nonce);
const _plaintext = encoder.encode(plaintext);
const secret = encrypt(_plaintext, _key, _nonce);
const _secret = fromUint8ArrayToHexString(secret);
return _secret;
};
export const decryptString = (
ciphertext: string,
key: string,
nonce: string
): string => {
const _key = fromHexToUint8Array(key);
const _nonce = fromHexToUint8Array(nonce);
const _ciphertext = fromHexToUint8Array(ciphertext);
const secret = decrypt(_ciphertext, _key, _nonce);
const _secret = decoder.decode(secret);
return _secret;
};