/** * This module provides easy, yet strong, facilities for encrypting and decrypting serializable objects. The * ease-of-use comes from the fact that this module is opinionated in its (strong) choice of cryptographic algorithms, * lengths, and iterations that cannot be overriden by its users. * * The module exports a `makeCryptoWith` function that takes a single argument, an `opts` object. This object requires * a property named `encryptionKey` which is a passphrase used by the encryption and decryption algorithms within * this module. The `makeCryptoWith` function returns an object containing two functions, `encrypt` and `decrypt`. * * Both the `encrypt` and `decrypt` functions are inverses of each other and return Promises. That is: * someSerializableObj === await decrypt(await encrypt(someSerializableObj)). */ /// export interface CryptoOptions { encryptionKey: string | Buffer; } export type EncryptOutput = string | object | number | boolean; export interface Crypto { encrypt(input: Input, aad?: string): Promise; decrypt(encryptedOutput: string | Buffer, aad?: string): Promise; encryptSync(input: Input, aad?: string): string; decryptSync(encryptedOutput: string | Buffer, aad?: string): EncryptOutput | EncryptOutput[]; } /** * Implmenetation of encrypt() and decrypt() taken from https://gist.github.com/AndiDittrich/4629e7db04819244e843, * which was recommended by @jaymode */ export default function makeCryptoWith(opts: CryptoOptions): Crypto;