Skip to content

Commit 678563e

Browse files
committed
update Hash implementations with Checksum interface
1 parent dc83f76 commit 678563e

32 files changed

+340
-93
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/crc32/test/index.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { expect } from "chai";
22
import "mocha";
33
import { Crc32 } from "../src/index";
44
import { fromUtf8 } from "@aws-sdk/util-utf8-browser";
5+
import {AwsCrc32} from "../build";
56

67
type TestVector = [Uint8Array, number];
78

@@ -49,4 +50,12 @@ describe("Crc32", () => {
4950
expect(instance.update(data).digest()).to.eql(expectedCrc32);
5051
}
5152
});
53+
54+
it("should create a new crc32 instance when reset is called ", () => {
55+
const awsCrc32 = new AwsCrc32();
56+
const oldInstance = (awsCrc32 as any).crc32;
57+
awsCrc32.reset();
58+
const newInstance = (awsCrc32 as any).crc32;
59+
expect(oldInstance).to.not.equal(newInstance); // compare by reference
60+
})
5261
});

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: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { expect } from "chai";
22
import "mocha";
33
import { Crc32c } from "../src";
44
import { fromUtf8 } from "@aws-sdk/util-utf8-browser";
5-
import { Test } from "mocha";
5+
import {AwsCrc32c} from "../build";
66

77
type TestVector = [Uint8Array, number];
88

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

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
}

0 commit comments

Comments
 (0)