Skip to content

Commit

Permalink
feat: update Hash implementations with Checksum interface
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewFossAWS committed Jan 10, 2023
1 parent ae1a124 commit 7f8ae7b
Show file tree
Hide file tree
Showing 32 changed files with 306 additions and 131 deletions.
40 changes: 21 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"packages/*"
],
"devDependencies": {
"@aws-sdk/types": "^3.110.0",
"@aws-sdk/types": "^3.222.0",
"@aws-sdk/util-buffer-from": "^3.29.0",
"@aws-sdk/util-hex-encoding": "^3.29.0",
"@aws-sdk/util-utf8-browser": "^3.29.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/crc32/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"license": "Apache-2.0",
"dependencies": {
"@aws-crypto/util": "file:../util",
"@aws-sdk/types": "^3.110.0",
"@aws-sdk/types": "^3.222.0",
"tslib": "^1.11.1"
}
}
10 changes: 7 additions & 3 deletions packages/crc32/src/aws_crc32.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

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

export class AwsCrc32 implements Hash {
private readonly crc32 = new Crc32();
export class AwsCrc32 implements Checksum {
private crc32 = new Crc32();

update(toHash: SourceData) {
if (isEmptyData(toHash)) return;
Expand All @@ -17,4 +17,8 @@ export class AwsCrc32 implements Hash {
async digest(): Promise<Uint8Array> {
return numToUint8(this.crc32.digest());
}

reset(): void {
this.crc32 = new Crc32();
}
}
12 changes: 10 additions & 2 deletions packages/crc32/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from "chai";
import "mocha";
import { Crc32 } from "../src/index";
import { expect } from "chai";
import { Crc32, AwsCrc32 } from "../src/index";
import { fromUtf8 } from "@aws-sdk/util-utf8-browser";

type TestVector = [Uint8Array, number];
Expand Down Expand Up @@ -49,4 +49,12 @@ describe("Crc32", () => {
expect(instance.update(data).digest()).to.eql(expectedCrc32);
}
});

it("should create a new crc32 instance when reset is called ", () => {
const awsCrc32 = new AwsCrc32();
const oldInstance = (awsCrc32 as any).crc32;
awsCrc32.reset();
const newInstance = (awsCrc32 as any).crc32;
expect(oldInstance).to.not.equal(newInstance); // compare by reference
})
});
2 changes: 1 addition & 1 deletion packages/crc32c/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"license": "Apache-2.0",
"dependencies": {
"@aws-crypto/util": "file:../util",
"@aws-sdk/types": "^3.110.0",
"@aws-sdk/types": "^3.222.0",
"tslib": "^1.11.1"
},
"publishConfig": {
Expand Down
10 changes: 7 additions & 3 deletions packages/crc32c/src/aws_crc32c.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

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

export class AwsCrc32c implements Hash {
private readonly crc32c = new Crc32c();
export class AwsCrc32c implements Checksum {
private crc32c = new Crc32c();

update(toHash: SourceData) {
if (isEmptyData(toHash)) return;
Expand All @@ -17,4 +17,8 @@ export class AwsCrc32c implements Hash {
async digest(): Promise<Uint8Array> {
return numToUint8(this.crc32c.digest());
}

reset(): void {
this.crc32c = new Crc32c();
}
}
13 changes: 10 additions & 3 deletions packages/crc32c/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { expect } from "chai";
import "mocha";
import { Crc32c } from "../src";
import { expect } from "chai";
import { Crc32c, AwsCrc32c } from "../src";
import { fromUtf8 } from "@aws-sdk/util-utf8-browser";
import { Test } from "mocha";

type TestVector = [Uint8Array, number];

Expand Down Expand Up @@ -56,4 +55,12 @@ describe("Crc32c", () => {
expect(instance.update(data).digest()).to.eql(expectedCrc32c);
}
});

it("should create a new crc32c instance when reset is called ", () => {
const awsCrc32c = new AwsCrc32c();
const oldInstance = (awsCrc32c as any).crc32c;
awsCrc32c.reset();
const newInstance = (awsCrc32c as any).crc32c;
expect(oldInstance).to.not.equal(newInstance); // compare by reference
})
});
2 changes: 1 addition & 1 deletion packages/random-source-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"homepage": "https://github.com/aws/aws-sdk-js-crypto-helpers/tree/master/packages/random-source-node",
"license": "Apache-2.0",
"dependencies": {
"@aws-sdk/types": "^3.110.0",
"@aws-sdk/types": "^3.222.0",
"tslib": "^1.11.1"
},
"types": "./build/index.d.ts"
Expand Down
2 changes: 1 addition & 1 deletion packages/random-source-universal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"dependencies": {
"@aws-crypto/random-source-browser": "file:../random-source-browser",
"@aws-crypto/random-source-node": "file:../random-source-node",
"@aws-sdk/types": "^3.110.0",
"@aws-sdk/types": "^3.222.0",
"tslib": "^1.11.1"
},
"browser": {
Expand Down
3 changes: 2 additions & 1 deletion packages/sha1-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
"homepage": "https://github.com/aws/aws-sdk-js-crypto-helpers/tree/master/packages/sha1-browser",
"license": "Apache-2.0",
"dependencies": {
"@aws-crypto/util": "file:../util",
"@aws-crypto/ie11-detection": "file:../ie11-detection",
"@aws-crypto/supports-web-crypto": "file:../supports-web-crypto",
"@aws-sdk/types": "^3.110.0",
"@aws-sdk/types": "^3.222.0",
"@aws-sdk/util-locate-window": "^3.0.0",
"@aws-sdk/util-utf8-browser": "^3.0.0",
"tslib": "^1.11.1"
Expand Down
13 changes: 9 additions & 4 deletions packages/sha1-browser/src/crossPlatformSha1.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Sha1 as Ie11Sha1 } from "./ie11Sha1";
import { Sha1 as WebCryptoSha1 } from "./webCryptoSha1";
import { Hash, SourceData } from "@aws-sdk/types";
import { Checksum, SourceData } from "@aws-sdk/types";
import { supportsWebCrypto } from "@aws-crypto/supports-web-crypto";
import { isMsWindow } from "@aws-crypto/ie11-detection";
import { locateWindow } from "@aws-sdk/util-locate-window";
import { convertToBuffer } from "@aws-crypto/util";

export class Sha1 implements Hash {
private readonly hash: Hash;
export class Sha1 implements Checksum {
private hash: Checksum;

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

update(data: SourceData, encoding?: "utf8" | "ascii" | "latin1"): void {
this.hash.update(data, encoding);
this.hash.update(convertToBuffer(data));
}

digest(): Promise<Uint8Array> {
return this.hash.digest();
}

reset(): void {
this.hash.reset();
}
}
Loading

0 comments on commit 7f8ae7b

Please sign in to comment.