-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflow.js
37 lines (31 loc) · 913 Bytes
/
flow.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
/**
* Flow data.
*/
/** @typedef {import('node:fs').ReadStream} ReadStream */
import * as crypto from 'node:crypto';
import { getKey, toKeyObject } from './rsa.js';
/**
* Random key (UUID)
* @return {Buffer}
*/
export const randomKey = () =>
crypto.scryptSync(crypto.randomUUID(), crypto.randomBytes(16), 32);
// Asymetric..
/**
* Encrypt the key using public key.
* @param {string} key The key want to encrypt.
* @return {Promise<Buffer>}
*/
export const encryptTheKey = async (key) => {
const publicKey = toKeyObject('public', await getKey('public'));
return crypto.publicEncrypt(publicKey, key);
};
/**
* Restore the encrypted key.
* @param {Buffer} encrypted Encrypted key by hex.
* @return {Promise<Buffer>}
*/
export const restoreKey = async (encrypted) => {
const privateKey = toKeyObject('private', await getKey('private'));
return crypto.privateDecrypt(privateKey, encrypted);
};