Skip to content

Commit 80f7d3c

Browse files
committed
update Hash implementations with Checksum interface
1 parent dc83f76 commit 80f7d3c

26 files changed

+217
-92
lines changed

package-lock.json

Lines changed: 27 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"packages/*"
4040
],
4141
"devDependencies": {
42-
"@aws-sdk/types": "^3.110.0",
42+
"@aws-sdk/types": "^3.222.0",
4343
"@aws-sdk/util-buffer-from": "^3.29.0",
4444
"@aws-sdk/util-hex-encoding": "^3.29.0",
4545
"@aws-sdk/util-utf8-browser": "^3.29.0",

packages/crc32/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"license": "Apache-2.0",
2222
"dependencies": {
2323
"@aws-crypto/util": "file:../util",
24-
"@aws-sdk/types": "^3.110.0",
24+
"@aws-sdk/types": "^3.222.0",
2525
"tslib": "^1.11.1"
2626
}
2727
}

packages/crc32/src/aws_crc32.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
import { Hash, SourceData } from "@aws-sdk/types";
4+
import { SourceData, Checksum } from "@aws-sdk/types";
55
import { convertToBuffer, isEmptyData, numToUint8 } from "@aws-crypto/util";
66
import { Crc32 } from "./index";
77

8-
export class AwsCrc32 implements Hash {
9-
private readonly crc32 = new Crc32();
8+
export class AwsCrc32 implements Checksum {
9+
private crc32 = new Crc32();
1010

1111
update(toHash: SourceData) {
1212
if (isEmptyData(toHash)) return;
@@ -17,4 +17,8 @@ export class AwsCrc32 implements Hash {
1717
async digest(): Promise<Uint8Array> {
1818
return numToUint8(this.crc32.digest());
1919
}
20+
21+
reset(): void {
22+
this.crc32 = new Crc32();
23+
}
2024
}

packages/crc32c/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"license": "Apache-2.0",
2222
"dependencies": {
2323
"@aws-crypto/util": "file:../util",
24-
"@aws-sdk/types": "^3.110.0",
24+
"@aws-sdk/types": "^3.222.0",
2525
"tslib": "^1.11.1"
2626
},
2727
"publishConfig": {

packages/crc32c/src/aws_crc32c.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
import { Hash, SourceData } from "@aws-sdk/types";
4+
import { Checksum, SourceData } from "@aws-sdk/types";
55
import { convertToBuffer, isEmptyData, numToUint8 } from "@aws-crypto/util";
66
import { Crc32c } from "./index";
77

8-
export class AwsCrc32c implements Hash {
9-
private readonly crc32c = new Crc32c();
8+
export class AwsCrc32c implements Checksum {
9+
private crc32c = new Crc32c();
1010

1111
update(toHash: SourceData) {
1212
if (isEmptyData(toHash)) return;
@@ -17,4 +17,8 @@ export class AwsCrc32c implements Hash {
1717
async digest(): Promise<Uint8Array> {
1818
return numToUint8(this.crc32c.digest());
1919
}
20+
21+
reset(): void {
22+
this.crc32c = new Crc32c();
23+
}
2024
}

packages/random-source-node/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"homepage": "https://github.com/aws/aws-sdk-js-crypto-helpers/tree/master/packages/random-source-node",
2020
"license": "Apache-2.0",
2121
"dependencies": {
22-
"@aws-sdk/types": "^3.110.0",
22+
"@aws-sdk/types": "^3.222.0",
2323
"tslib": "^1.11.1"
2424
},
2525
"types": "./build/index.d.ts"

packages/random-source-universal/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"dependencies": {
2121
"@aws-crypto/random-source-browser": "file:../random-source-browser",
2222
"@aws-crypto/random-source-node": "file:../random-source-node",
23-
"@aws-sdk/types": "^3.110.0",
23+
"@aws-sdk/types": "^3.222.0",
2424
"tslib": "^1.11.1"
2525
},
2626
"browser": {

packages/sha1-browser/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
"homepage": "https://github.com/aws/aws-sdk-js-crypto-helpers/tree/master/packages/sha1-browser",
1919
"license": "Apache-2.0",
2020
"dependencies": {
21+
"@aws-crypto/util": "file:../util",
2122
"@aws-crypto/ie11-detection": "file:../ie11-detection",
2223
"@aws-crypto/supports-web-crypto": "file:../supports-web-crypto",
23-
"@aws-sdk/types": "^3.110.0",
24+
"@aws-sdk/types": "^3.222.0",
2425
"@aws-sdk/util-locate-window": "^3.0.0",
2526
"@aws-sdk/util-utf8-browser": "^3.0.0",
2627
"tslib": "^1.11.1"
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { Sha1 as Ie11Sha1 } from "./ie11Sha1";
22
import { Sha1 as WebCryptoSha1 } from "./webCryptoSha1";
3-
import { Hash, SourceData } from "@aws-sdk/types";
3+
import { Checksum, SourceData } from "@aws-sdk/types";
44
import { supportsWebCrypto } from "@aws-crypto/supports-web-crypto";
55
import { isMsWindow } from "@aws-crypto/ie11-detection";
66
import { locateWindow } from "@aws-sdk/util-locate-window";
7+
import { convertToBuffer } from "@aws-crypto/util";
78

8-
export class Sha1 implements Hash {
9-
private readonly hash: Hash;
9+
export class Sha1 implements Checksum {
10+
private readonly secret?: SourceData;
11+
private hash: Checksum;
1012

1113
constructor(secret?: SourceData) {
14+
this.secret = secret;
1215
if (supportsWebCrypto(locateWindow())) {
1316
this.hash = new WebCryptoSha1(secret);
1417
} else if (isMsWindow(locateWindow())) {
@@ -19,10 +22,18 @@ export class Sha1 implements Hash {
1922
}
2023

2124
update(data: SourceData, encoding?: "utf8" | "ascii" | "latin1"): void {
22-
this.hash.update(data, encoding);
25+
this.hash.update(convertToBuffer(data));
2326
}
2427

2528
digest(): Promise<Uint8Array> {
2629
return this.hash.digest();
2730
}
31+
32+
reset(): void {
33+
if (supportsWebCrypto(locateWindow())) {
34+
this.hash = new WebCryptoSha1(this.secret);
35+
} else if (isMsWindow(locateWindow())) {
36+
this.hash = new Ie11Sha1(this.secret);
37+
}
38+
}
2839
}

packages/sha1-browser/src/ie11Sha1.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { isEmptyData } from "./isEmptyData";
22
import { SHA_1_HMAC_ALGO } from "./constants";
3-
import { Hash, SourceData } from "@aws-sdk/types";
3+
import { Checksum, SourceData } from "@aws-sdk/types";
44
import { fromUtf8 } from "@aws-sdk/util-utf8-browser";
55
import { CryptoOperation, Key, MsWindow } from "@aws-crypto/ie11-detection";
66
import { locateWindow } from "@aws-sdk/util-locate-window";
77

8-
export class Sha1 implements Hash {
8+
export class Sha1 implements Checksum {
99
private operation: Promise<CryptoOperation>;
1010

1111
constructor(secret?: SourceData) {
@@ -60,6 +60,9 @@ export class Sha1 implements Hash {
6060
})
6161
);
6262
}
63+
64+
reset(): void {
65+
}
6366
}
6467

6568
function getKeyPromise(secret: SourceData): Promise<Key> {

packages/sha1-browser/src/webCryptoSha1.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { Hash, SourceData } from "@aws-sdk/types";
1+
import { Checksum, SourceData } from "@aws-sdk/types";
22
import { fromUtf8 } from "@aws-sdk/util-utf8-browser";
33
import { isEmptyData } from "./isEmptyData";
44
import { EMPTY_DATA_SHA_1, SHA_1_HASH, SHA_1_HMAC_ALGO } from "./constants";
55
import { locateWindow } from "@aws-sdk/util-locate-window";
66

7-
export class Sha1 implements Hash {
7+
export class Sha1 implements Checksum {
88
private readonly key: Promise<CryptoKey> | undefined;
99
private toHash: Uint8Array = new Uint8Array(0);
1010

@@ -56,6 +56,10 @@ export class Sha1 implements Hash {
5656
.then(() => locateWindow().crypto.subtle.digest(SHA_1_HASH, this.toHash))
5757
.then((data) => Promise.resolve(new Uint8Array(data)));
5858
}
59+
60+
reset(): void {
61+
this.toHash = new Uint8Array(0);
62+
}
5963
}
6064

6165
function convertToBuffer(data: SourceData): Uint8Array {

0 commit comments

Comments
 (0)