-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
132 lines (105 loc) · 3.97 KB
/
utils.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
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
123
124
125
126
127
128
129
130
131
132
import nacl, { secretbox, box, randomBytes, setPRNG } from 'tweetnacl';
import {getRandomBytes} from "expo-crypto";
import {
decode as decodeUTF8,
encode as encodeUTF8,
} from "@stablelib/utf8";
import {
decode as decodeBase64,
encode as encodeBase64,
} from "@stablelib/base64";
setPRNG((x, n) => {
const randomBytes = getRandomBytes(n);
for (let i = 0; i < n; i++) {
x[i] = randomBytes[i];
}
});
const newNonce = () => randomBytes(box.nonceLength);
export const generateKeyPair = () => box.keyPair();
export const generateKey = () => encodeBase64(randomBytes(secretbox.keyLength));
export const encryptSecretKey = (json:any, key:string) => {
const keyUint8Array = decodeBase64(key);
const nonce = newNonce();
const messageUint8 = encodeUTF8(JSON.stringify(json));
const box = secretbox(messageUint8, nonce, keyUint8Array);
const fullMessage = new Uint8Array(nonce.length + box.length);
fullMessage.set(nonce);
fullMessage.set(box, nonce.length);
const base64FullMessage = encodeBase64(fullMessage);
return base64FullMessage;
};
export const decryptSecretKey = (messageWithNonce:string, key:string) => {
const keyUint8Array = decodeBase64(key);
const messageWithNonceAsUint8Array = decodeBase64(messageWithNonce);
const nonce = messageWithNonceAsUint8Array.slice(0, secretbox.nonceLength);
const message = messageWithNonceAsUint8Array.slice(
secretbox.nonceLength,
messageWithNonce.length
);
const decrypted = secretbox.open(message, nonce, keyUint8Array);
if (!decrypted) {
throw new Error("Could not decrypt message");
}
const base64DecryptedMessage = decodeUTF8(decrypted);
return JSON.parse(base64DecryptedMessage);
};
export const encrypt = (
secretOrSharedKey: Uint8Array,
json: any,
key?: Uint8Array
) => {
const nonce = newNonce();
const messageUint8 = encodeUTF8(JSON.stringify(json));
const encrypted = key
? box(messageUint8, nonce, key, secretOrSharedKey)
: box.after(messageUint8, nonce, secretOrSharedKey);
const fullMessage = new Uint8Array(nonce.length + encrypted.length);
fullMessage.set(nonce);
fullMessage.set(encrypted, nonce.length);
const base64FullMessage = encodeBase64(fullMessage);
return base64FullMessage;
};
export const decrypt = (
secretOrSharedKey: Uint8Array,
messageWithNonce: string,
key?: Uint8Array
) => {
const messageWithNonceAsUint8Array = decodeBase64(messageWithNonce);
const nonce = messageWithNonceAsUint8Array.slice(0, box.nonceLength);
const message = messageWithNonceAsUint8Array.slice(
box.nonceLength,
messageWithNonce.length
);
const decrypted = key
? box.open(message, nonce, key, secretOrSharedKey)
: box.open.after(message, nonce, secretOrSharedKey);
if (!decrypted) {
throw new Error('Could not decrypt message');
}
const base64DecryptedMessage = decodeUTF8(decrypted);
return JSON.parse(base64DecryptedMessage);
};
export const encryptBinary = (secretOrSharedKey:any, binaryData:any, key:any) => {
const nonce = newNonce();
const encrypted = key
? nacl.box(binaryData, nonce, key, secretOrSharedKey)
: nacl.box.after(binaryData, nonce, secretOrSharedKey);
// Combine nonce and encrypted message
const fullMessage = new Uint8Array(nonce.length + encrypted.length);
fullMessage.set(nonce);
fullMessage.set(encrypted, nonce.length);
// Encode to base64 for easier storage and transfer
return encodeBase64(fullMessage);
};
export const decryptBinary = (secretOrSharedKey:any, encryptedMessage:any, key:any) => {
const messageWithNonceAsUint8Array = decodeBase64(encryptedMessage);
const nonce = messageWithNonceAsUint8Array.slice(0, nacl.box.nonceLength);
const message = messageWithNonceAsUint8Array.slice(nacl.box.nonceLength);
const decrypted = key
? nacl.box.open(message, nonce, key, secretOrSharedKey)
: nacl.box.open.after(message, nonce, secretOrSharedKey);
if (!decrypted) {
throw new Error("Could not decrypt binary data");
}
return decrypted; // Return decrypted binary data
};