/
crypt_storage.ts
46 lines (41 loc) · 1.55 KB
/
crypt_storage.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
import { AES, Buffer, getMachineId } from "../deps.ts";
import { getPathInStorage, mkdir } from "./storage.ts";
/** turns any string with any length to a string with 32 chars */
function normalizeKey(key: string) {
let newKey = key.replace(/-/g, "");
newKey = newKey.substr(0, 32);
if (newKey.length < 32) {
const requiredCharsCount = 32 - newKey.length;
for (let i = 0; i < requiredCharsCount; i++) {
newKey = "0" + newKey;
}
}
return newKey;
}
/**
* For using this class you need to give proper permissions to deno. It encrypts your data with this machine's id.
*
* Automatically calls JSON.stringify on your data on save and JSON.parse on load.
*
* on windows (not sure): --allow-env --allow-run --allow-read=%cd%\\storage --allow-write=%cd%\\storage
*
* on linux: --allow-read=/var/lib/dbus/machine-id,/etc/machine-id --allow-read=$PWD/storage --allow-write=$PWD/storage
*/
export class CryptStorage {
static async saveData(path: string, data: unknown) {
const resolvedPath = getPathInStorage(path);
await mkdir(resolvedPath);
const key = normalizeKey(await getMachineId());
const aes = new AES(key);
await Deno.writeFile(resolvedPath, await aes.encrypt(JSON.stringify(data)));
}
static async loadData(path: string): Promise<unknown> {
const fileData = await Deno.readFile(getPathInStorage(path));
const key = normalizeKey(await getMachineId());
const aes = new AES(key);
const data = Buffer.from(await aes.decrypt(fileData)).toString(
"utf-8",
);
return JSON.parse(data);
}
}