This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
475 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import {cloneDeep, forEach} from 'lodash'; | ||
import {Colour} from '../lib/Colour'; | ||
import {Crypt} from '../lib/Crypt'; | ||
import {PromptableConfig} from '../lib/PromptableConfig'; | ||
|
||
const enum Conf { | ||
MAX_ATTEMPTS = 3 | ||
} | ||
|
||
function setRegular(cfg: any, k: string, value: any): void { | ||
Object.defineProperty(cfg, k, { | ||
configurable: true, | ||
enumerable: true, | ||
value, | ||
writable: true | ||
}); | ||
} | ||
|
||
function setVirtual(pcfg: PromptableConfig<any>, cfg: any, k: string, v: any): void { | ||
let attempt = 1; | ||
const colourFns = [<any>undefined, Colour.green, Colour.yellow, Colour.red]; | ||
|
||
Object.defineProperty(cfg, k, { | ||
configurable: true, | ||
enumerable: true, | ||
get: function getter(): any { | ||
const pwd = pcfg.promptedEncryptionPassword(); | ||
try { | ||
const decrypted = Crypt.decryptVar(v, pwd); | ||
setRegular(cfg, k, decrypted); | ||
attempt = 1; | ||
|
||
return decrypted; | ||
} catch (e) { | ||
const msg = [ | ||
Colour.red(e.message), | ||
` [${colourFns[attempt].call(Colour, attempt)}/`, | ||
`${Colour.red(Conf.MAX_ATTEMPTS.toString())}]\n` | ||
].join(''); | ||
process.stderr.write(msg); | ||
|
||
if (attempt++ < Conf.MAX_ATTEMPTS) { | ||
delete pcfg['data'].password; | ||
pcfg.promptedEncryptionPassword['cache'].clear(); | ||
|
||
return getter(); | ||
} else { | ||
process.exit(1); | ||
} | ||
} | ||
}, | ||
set(v$: any) { | ||
if (Crypt.isEncrypted(v$)) { | ||
setVirtual(pcfg, cfg, k, v$); | ||
} else { | ||
setRegular(cfg, k, v$); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
export function parseJointCfgForEncryption<T>(cfg: T): T { | ||
cfg = cloneDeep(cfg); | ||
const pcfg = new PromptableConfig(cfg); | ||
|
||
forEach(<any>cfg, (v: any, k: string) => { | ||
if (Crypt.isEncrypted(v)) { | ||
setVirtual(pcfg, cfg, k, v); | ||
} | ||
}); | ||
|
||
return cfg; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type Nil = undefined | null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import * as crypto from 'crypto'; | ||
import {isObject} from 'lodash'; | ||
|
||
const enum Conf { | ||
IV_RANDOM_BYTES = 16, | ||
SALT_RANDOM_BYTES = 64, | ||
KEY_ITERATIONS = 16384, | ||
KEY_LEN = 32, | ||
CIPHER = 'aes-256-gcm', | ||
HASH = 'sha512', | ||
IV_START = 64, | ||
TAG_START = 80, | ||
TEXT_START = 96 | ||
} | ||
|
||
export interface Encrypted { | ||
__encrypted: string; | ||
} | ||
|
||
export class Crypt { | ||
public static decrypt(encrypted: string, password: string): string { | ||
const bData = Buffer.from(encrypted, 'base64'); | ||
|
||
const salt = bData.slice(0, Conf.IV_START); | ||
const iv = bData.slice(Conf.IV_START, Conf.TAG_START); | ||
const tag = bData.slice(Conf.TAG_START, Conf.TEXT_START); | ||
const text = bData.slice(Conf.TEXT_START); | ||
|
||
const key = crypto.pbkdf2Sync(password, salt, Conf.KEY_ITERATIONS, Conf.KEY_LEN, Conf.HASH); | ||
const decipher = crypto.createDecipheriv(Conf.CIPHER, key, iv); | ||
decipher.setAuthTag(tag); | ||
|
||
const out = decipher.update(text, 'binary', 'utf8') + decipher.final('utf8'); | ||
|
||
if (!out) { | ||
throw new Error('Unable to decrypt'); | ||
} | ||
|
||
return out; | ||
} | ||
|
||
public static decryptVar(v: Encrypted, password: string): string { | ||
return Crypt.decrypt(v.__encrypted, password); | ||
} | ||
|
||
public static encrypt(text: string, password: string): string { | ||
const iv = crypto.randomBytes(Conf.IV_RANDOM_BYTES); | ||
const salt = crypto.randomBytes(Conf.SALT_RANDOM_BYTES); | ||
const key = crypto.pbkdf2Sync(password, salt, Conf.KEY_ITERATIONS, Conf.KEY_LEN, Conf.HASH); | ||
const cipher = crypto.createCipheriv(Conf.CIPHER, key, iv); | ||
const encrypted = Buffer.concat([cipher.update(text, 'utf8'), cipher.final()]); | ||
const authTag = cipher.getAuthTag(); | ||
|
||
return Buffer.concat([salt, iv, authTag, encrypted]).toString('base64'); | ||
} | ||
|
||
public static encryptVar(text: string, password: string): Encrypted { | ||
return {__encrypted: Crypt.encrypt(text, password)}; | ||
} | ||
|
||
public static isEncrypted(v: any): v is Encrypted { | ||
return !!v && isObject(v) && typeof v.__encrypted === 'string'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.