UNPKG

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