UNPKG

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