UNPKG

1.66 kBTypeScriptView Raw
1/// <reference types="node" />
2import { Optional } from '@salesforce/ts-types';
3/**
4 * Returns the intended type of the object to return. This is implementation specific.
5 * @param buffer A buffer containing the decrypted secret.
6 */
7export declare type DecipherCallback<T> = (buffer: Buffer) => T;
8/**
9 * Used to store and retrieve a sensitive information in memory. This is not meant for at rest encryption.
10 *
11 * ```
12 * const sString: SecureBuffer<string> = new SecureBuffer();
13 * sString.consume(secretTextBuffer);
14 * const value: string = sString.value((buffer: Buffer): string => {
15 * const password: string = buffer.toString('utf8');
16 * // doSomething with the password
17 * // returns something of type <T>
18 * return testReturnValue;
19 * });
20 * ```
21 */
22export declare class SecureBuffer<T> {
23 private key;
24 private iv;
25 private secret?;
26 /**
27 * Invokes a callback with a decrypted version of the buffer.
28 * @param cb The callback containing the decrypted buffer parameter that returns a desired.
29 * typed object. It's important to understand that once the callback goes out of scope the buffer parameters is
30 * overwritten with random data. Do not make a copy of this buffer and persist it!
31 */
32 value(cb: DecipherCallback<T>): Optional<T>;
33 /**
34 * Overwrites the value of the encrypted secret with random data.
35 */
36 clear(): void;
37 /**
38 * Consumes a buffer of data that's intended to be secret.
39 * @param buffer Data to encrypt. The input buffer is overwritten with random data after it's encrypted
40 * and assigned internally.
41 */
42 consume(buffer: Buffer): void;
43}