Skip to content

Commit 89598b6

Browse files
feat(types): add checksum interface (#4216)
1 parent a27adec commit 89598b6

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

packages/types/src/checksum.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* An object that provides a checksum of data provided in chunks to `update`.
3+
* The checksum may be performed incrementally as chunks are received or all
4+
* at once when the checksum is finalized, depending on the underlying
5+
* implementation.
6+
*
7+
* It's recommended to compute checksum incrementally to avoid reading the
8+
* entire payload in memory.
9+
*
10+
* A class that implements this interface may accept an optional secret key in its
11+
* constructor while computing checksum value, when using HMAC. If provided,
12+
* this secret key would be used when computing checksum.
13+
*/
14+
export interface Checksum {
15+
/**
16+
* Constant length of the digest created by the algorithm in bytes.
17+
*/
18+
digestLength?: number;
19+
20+
/**
21+
* Creates a new checksum object that contains a deep copy of the internal
22+
* state of the current `Checksum` object.
23+
*/
24+
copy?(): Checksum;
25+
26+
/**
27+
* Returns the digest of all of the data passed.
28+
*/
29+
digest(): Promise<Uint8Array>;
30+
31+
/**
32+
* Allows marking a checksum for checksums that support the ability
33+
* to mark and reset.
34+
*
35+
* @param {number} 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+
/**
41+
* Resets the checksum to its initial value.
42+
*/
43+
reset(): void;
44+
45+
/**
46+
* Adds a chunk of data for which checksum needs to be computed.
47+
* This can be called many times with new data as it is streamed.
48+
*
49+
* Implementations may override this method which passes second param
50+
* which makes Checksum object stateless.
51+
*
52+
* @param {Uint8Array} chunk - The buffer to update checksum with.
53+
*/
54+
update(chunk: Uint8Array): void;
55+
}

packages/types/src/crypto.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
import { Checksum } from "./checksum";
2+
13
export type SourceData = string | ArrayBuffer | ArrayBufferView;
24

35
/**
46
* An object that provides a hash of data provided in chunks to `update`. The
57
* hash may be performed incrementally as chunks are received or all at once
68
* when the hash is finalized, depending on the underlying implementation.
9+
*
10+
* @deprecated use {@link Checksum}
711
*/
812
export interface Hash {
913
/**

packages/types/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from "./abort";
22
export * from "./auth";
3+
export * from "./checksum";
34
export * from "./client";
45
export * from "./command";
56
export * from "./credentials";

0 commit comments

Comments
 (0)