-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
keystore.ts
28 lines (25 loc) · 1009 Bytes
/
keystore.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
import type { Keystore } from './aes-ctr';
import { encrypt as encNode, decrypt as decNode } from './aes-ctr-node';
import { encrypt as encWeb, decrypt as decWeb } from './aes-ctr-web';
import { strategy } from './universal-crypto';
export type { Keystore } from './aes-ctr';
export { keyFromPassword } from './aes-ctr';
export { randomBytes } from './randomBytes';
/**
* Encrypts a data object that can be any serializable value using
* a provided password.
*
* @returns Promise<Keystore> Keystore object
*/
export async function encrypt<T>(password: string, data: T): Promise<Keystore> {
return strategy === 'Node' ? encNode<T>(password, data) : encWeb<T>(password, data);
}
/**
* Given a password and a keystore object, decrypts the text and returns
* the resulting value
*
* @returns Promise<T> T object
*/
export async function decrypt<T>(password: string, keystore: Keystore): Promise<T> {
return strategy === 'Node' ? decNode<T>(password, keystore) : decWeb<T>(password, keystore);
}