basic navidrome implementation

This commit is contained in:
simojenki
2021-03-01 17:28:48 +11:00
parent 3b350c4402
commit 007db24713
17 changed files with 305 additions and 105 deletions

33
src/encryption.ts Normal file
View File

@@ -0,0 +1,33 @@
import { createCipheriv, createDecipheriv, randomBytes } from "crypto";
const ALGORITHM = "aes-256-cbc"
const IV = randomBytes(16);
const KEY = randomBytes(32);
export type Hash = {
iv: string,
encryptedData: string
}
export type Encryption = {
encrypt: (value:string) => Hash
decrypt: (hash: Hash) => string
}
const encryption = (): Encryption => {
return {
encrypt: (value: string) => {
const cipher = createCipheriv(ALGORITHM, KEY, IV);
return {
iv: IV.toString("hex"),
encryptedData: Buffer.concat([cipher.update(value), cipher.final()]).toString("hex")
};
},
decrypt: (hash: Hash) => {
const decipher = createDecipheriv(ALGORITHM, KEY, Buffer.from(hash.iv, 'hex'));
return Buffer.concat([decipher.update(Buffer.from(hash.encryptedData, 'hex')), decipher.final()]).toString();
}
}
}
export default encryption;