Skip to content

Commit da43dc0

Browse files
feat!: replace Hash implementations with Checksum interface (#492)
BREAKING CHANGE: All classes that implemented `Hash` now implement `Checksum`.
1 parent ae1a124 commit da43dc0

32 files changed

+306
-131
lines changed

package-lock.json

Lines changed: 21 additions & 19 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/crc32/test/index.test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { expect } from "chai";
21
import "mocha";
3-
import { Crc32 } from "../src/index";
2+
import { expect } from "chai";
3+
import { Crc32, AwsCrc32 } from "../src/index";
44
import { fromUtf8 } from "@aws-sdk/util-utf8-browser";
55

66
type TestVector = [Uint8Array, number];
@@ -49,4 +49,12 @@ describe("Crc32", () => {
4949
expect(instance.update(data).digest()).to.eql(expectedCrc32);
5050
}
5151
});
52+
53+
it("should create a new crc32 instance when reset is called ", () => {
54+
const awsCrc32 = new AwsCrc32();
55+
const oldInstance = (awsCrc32 as any).crc32;
56+
awsCrc32.reset();
57+
const newInstance = (awsCrc32 as any).crc32;
58+
expect(oldInstance).to.not.equal(newInstance); // compare by reference
59+
})
5260
});

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/crc32c/test/index.test.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { expect } from "chai";
21
import "mocha";
3-
import { Crc32c } from "../src";
2+
import { expect } from "chai";
3+
import { Crc32c, AwsCrc32c } from "../src";
44
import { fromUtf8 } from "@aws-sdk/util-utf8-browser";
5-
import { Test } from "mocha";
65

76
type TestVector = [Uint8Array, number];
87

@@ -56,4 +55,12 @@ describe("Crc32c", () => {
5655
expect(instance.update(data).digest()).to.eql(expectedCrc32c);
5756
}
5857
});
58+
59+
it("should create a new crc32c instance when reset is called ", () => {
60+
const awsCrc32c = new AwsCrc32c();
61+
const oldInstance = (awsCrc32c as any).crc32c;
62+
awsCrc32c.reset();
63+
const newInstance = (awsCrc32c as any).crc32c;
64+
expect(oldInstance).to.not.equal(newInstance); // compare by reference
65+
})
5966
});

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"

packages/sha1-browser/src/crossPlatformSha1.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
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 hash: Checksum;
1011

1112
constructor(secret?: SourceData) {
1213
if (supportsWebCrypto(locateWindow())) {
@@ -19,10 +20,14 @@ export class Sha1 implements Hash {
1920
}
2021

2122
update(data: SourceData, encoding?: "utf8" | "ascii" | "latin1"): void {
22-
this.hash.update(data, encoding);
23+
this.hash.update(convertToBuffer(data));
2324
}
2425

2526
digest(): Promise<Uint8Array> {
2627
return this.hash.digest();
2728
}
29+
30+
reset(): void {
31+
this.hash.reset();
32+
}
2833
}

0 commit comments

Comments
 (0)