Skip to content

Commit 01d5b58

Browse files
authored
Add WebCrypto detection for crypto components (#16)
* Add WebCrypto detection for crypto components * Fix typos in tests
1 parent 848ae16 commit 01d5b58

File tree

6 files changed

+100
-0
lines changed

6 files changed

+100
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
*.js
3+
*.js.map
4+
*.d.ts
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import {supportsWebCrypto} from "../lib/supportsWebCrypto";
2+
3+
const fakeWindow: Window = {
4+
crypto: {
5+
getRandomValues: () => {},
6+
subtle: {
7+
decrypt(alg: any, key: any) { return {} as any; },
8+
digest(alg: any) { return {} as any; },
9+
encrypt(alg: any, key: any) { return {} as any; },
10+
exportKey(format: any, key: any) { return {} as any; },
11+
generateKey(alg: any) { return {} as any; },
12+
importKey(format: any, keyData: any, alg: any) { return {} as any; },
13+
sign(alg: any, key: any) { return {} as any; },
14+
verify(alg: any, key: any, signature: any) { return {} as any; },
15+
},
16+
},
17+
} as any;
18+
19+
describe('supportsWebCrypto', () => {
20+
it(
21+
'should return false if an object does not fulfill the WebCrypto interface',
22+
() => {
23+
expect(supportsWebCrypto({} as any)).toBe(false);
24+
}
25+
);
26+
27+
it(
28+
'should return true if an object fulfills the WebCrypto interface',
29+
() => {
30+
expect(supportsWebCrypto(fakeWindow)).toBe(true);
31+
}
32+
);
33+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './lib/supportsWebCrypto';
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
type SubtleCryptoMethod = 'decrypt'|'digest'|'encrypt'|'exportKey'|'generateKey'|'importKey'|'sign'|'verify';
2+
3+
const subtleCryptoMethods: Array<SubtleCryptoMethod> = [
4+
'decrypt',
5+
'digest',
6+
'encrypt',
7+
'exportKey',
8+
'generateKey',
9+
'importKey',
10+
'sign',
11+
'verify',
12+
];
13+
14+
export function supportsWebCrypto(window: Window): boolean {
15+
if (
16+
typeof window === 'object' &&
17+
typeof window.crypto === 'object' &&
18+
typeof window.crypto.subtle === 'object'
19+
) {
20+
const {getRandomValues, subtle} = window.crypto;
21+
const methodsToTest = subtleCryptoMethods
22+
.map<Function>(methodName => subtle[methodName])
23+
.concat(getRandomValues);
24+
25+
for (let method of methodsToTest) {
26+
if (typeof method !== 'function') {
27+
return false;
28+
}
29+
}
30+
31+
return true;
32+
}
33+
34+
return false;
35+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "@aws/crypto-supports-webCrypto",
3+
"private": true,
4+
"version": "0.0.1",
5+
"description": "Provides functions for detecting if the host environment supports the WebCrypto API",
6+
"scripts": {
7+
"pretest": "tsc",
8+
"test": "jest"
9+
},
10+
"author": "[email protected]",
11+
"license": "UNLICENSED",
12+
"main": "index.js",
13+
"devDependencies": {
14+
"@types/jest": "^19.2.2",
15+
"jest": "^19.0.2",
16+
"typescript": "^2.3"
17+
}
18+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"target": "es5",
5+
"strict": true,
6+
"sourceMap": true,
7+
"declaration": true
8+
}
9+
}

0 commit comments

Comments
 (0)