Skip to content

Commit f30495f

Browse files
committed
Add a SHA-256 universal module
1 parent fdf68b1 commit f30495f

File tree

5 files changed

+111
-0
lines changed

5 files changed

+111
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.js
2+
*.js.map
3+
*.d.ts
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import {Sha256} from '../';
2+
import {Sha256 as BrowserSha256} from '@aws/crypto-sha256-browser';
3+
import {Sha256 as NodeSha256} from '@aws/crypto-sha256-node';
4+
5+
jest.mock('@aws/is-node', () => {
6+
return { isNode: jest.fn() };
7+
});
8+
import {isNode} from '@aws/is-node';
9+
10+
beforeEach(() => {
11+
(isNode as any).mockReset();
12+
});
13+
14+
describe('implementation selection', () => {
15+
it('should use the node implementation in node', async () => {
16+
(isNode as any).mockReturnValue(true);
17+
18+
const sha256 = new Sha256();
19+
expect((sha256 as any).hash).toBeInstanceOf(NodeSha256);
20+
});
21+
22+
it('should use the browser implementation elsewhere', async () => {
23+
(isNode as any).mockReturnValue(false);
24+
25+
const sha256 = new Sha256();
26+
expect((sha256 as any).hash).toBeInstanceOf(BrowserSha256);
27+
});
28+
29+
it('should proxy method calls to underlying implementation', () => {
30+
const sha256 = new Sha256();
31+
const hashMock = {
32+
update: jest.fn(),
33+
digest: jest.fn(),
34+
};
35+
(sha256 as any).hash = hashMock;
36+
37+
sha256.update('foo', 'utf8');
38+
expect(hashMock.update.mock.calls.length).toBe(1);
39+
expect(hashMock.update.mock.calls[0]).toEqual(['foo', 'utf8']);
40+
41+
const promise = sha256.digest();
42+
expect(hashMock.digest.mock.calls.length).toBe(1);
43+
});
44+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {isNode} from '@aws/is-node';
2+
import {Sha256 as BrowserSha256} from '@aws/crypto-sha256-browser';
3+
import {Sha256 as NodeSha256} from '@aws/crypto-sha256-node';
4+
import {Hash, SourceData} from '@aws/types';
5+
6+
export class Sha256 implements Hash {
7+
private readonly hash: Hash;
8+
9+
constructor(secret?: SourceData) {
10+
if (isNode()) {
11+
this.hash = new NodeSha256(secret);
12+
} else {
13+
this.hash = new BrowserSha256(secret);
14+
}
15+
}
16+
17+
update(data: SourceData, encoding?: 'utf8' | 'ascii' | 'latin1'): void {
18+
this.hash.update(data, encoding);
19+
}
20+
21+
digest(): Promise<Uint8Array> {
22+
return this.hash.digest();
23+
}
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "@aws/crypto-sha256-universal",
3+
"private": true,
4+
"version": "0.0.1",
5+
"scripts": {
6+
"prepublishOnly": "tsc",
7+
"pretest": "tsc",
8+
"test": "jest"
9+
},
10+
"author": "[email protected]",
11+
"license": "UNLICENSED",
12+
"dependencies": {
13+
"@aws/crypto-sha256-browser": "^0.0.1",
14+
"@aws/crypto-sha256-node": "^0.0.1",
15+
"@aws/types": "^0.0.1",
16+
"@aws/is-node": "^0.0.1"
17+
},
18+
"devDependencies": {
19+
"@types/jest": "^19.2.2",
20+
"@types/node": "^7.0.12",
21+
"@types/text-encoding": "0.0.30",
22+
"jest": "^19.0.2",
23+
"typescript": "^2.3"
24+
}
25+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"module": "commonjs",
5+
"lib": [
6+
"DOM",
7+
"ES5",
8+
"ScriptHost",
9+
"ES2015.Promise"
10+
],
11+
"declaration": true,
12+
"sourceMap": true,
13+
"strict": true
14+
}
15+
}

0 commit comments

Comments
 (0)