UNPKG

2.09 kBTypeScriptView Raw
1import { SourceData } from "./crypto";
2/**
3 * @public
4 *
5 * An object that provides a checksum of data provided in chunks to `update`.
6 * The checksum may be performed incrementally as chunks are received or all
7 * at once when the checksum is finalized, depending on the underlying
8 * implementation.
9 *
10 * It's recommended to compute checksum incrementally to avoid reading the
11 * entire payload in memory.
12 *
13 * A class that implements this interface may accept an optional secret key in its
14 * constructor while computing checksum value, when using HMAC. If provided,
15 * this secret key would be used when computing checksum.
16 */
17export interface Checksum {
18 /**
19 * Constant length of the digest created by the algorithm in bytes.
20 */
21 digestLength?: number;
22 /**
23 * Creates a new checksum object that contains a deep copy of the internal
24 * state of the current `Checksum` object.
25 */
26 copy?(): Checksum;
27 /**
28 * Returns the digest of all of the data passed.
29 */
30 digest(): Promise<Uint8Array>;
31 /**
32 * Allows marking a checksum for checksums that support the ability
33 * to mark and reset.
34 *
35 * @param readLimit - The maximum limit of bytes that can be read
36 * before the mark position becomes invalid.
37 */
38 mark?(readLimit: number): void;
39 /**
40 * Resets the checksum to its initial value.
41 */
42 reset(): void;
43 /**
44 * Adds a chunk of data for which checksum needs to be computed.
45 * This can be called many times with new data as it is streamed.
46 *
47 * Implementations may override this method which passes second param
48 * which makes Checksum object stateless.
49 *
50 * @param chunk - The buffer to update checksum with.
51 */
52 update(chunk: Uint8Array): void;
53}
54/**
55 * @public
56 *
57 * A constructor for a Checksum that may be used to calculate an HMAC. Implementing
58 * classes should not directly hold the provided key in memory beyond the
59 * lexical scope of the constructor.
60 */
61export interface ChecksumConstructor {
62 new (secret?: SourceData): Checksum;
63}