Skip to content

Commit 7ce474f

Browse files
authored
fix: Add tests for sha512 (#140)
Better testing for caching_materials_manager_browser. Pull out the injected hash function and write some simple tests.
1 parent 1ad36bc commit 7ce474f

File tree

3 files changed

+72
-12
lines changed

3 files changed

+72
-12
lines changed

modules/caching-materials-manager-browser/src/caching_materials_manager_browser.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,8 @@ import {
3232
WebCryptoGetDecryptMaterials // eslint-disable-line no-unused-vars
3333
} from '@aws-crypto/material-management-browser'
3434
import { fromUtf8, toUtf8 } from '@aws-sdk/util-utf8-browser'
35-
import { getWebCryptoBackend, getNonZeroByteBackend, synchronousRandomValues } from '@aws-crypto/web-crypto-backend'
36-
import { concatBuffers } from '@aws-crypto/serialize'
37-
38-
const sha512 = async (...inputs: (Uint8Array|string)[]) => {
39-
// Normalize to Uint8Array and squash into a single value.
40-
const data = concatBuffers(...inputs.map(u => typeof u === 'string' ? fromUtf8(u) : u))
41-
// Prefer the non-zero byte because this will always be the native implementation.
42-
const backend = getNonZeroByteBackend(await getWebCryptoBackend())
43-
// Do the hash
44-
const ab = await backend.digest('SHA-512', data)
45-
return new Uint8Array(ab)
46-
}
35+
import { synchronousRandomValues } from '@aws-crypto/web-crypto-backend'
36+
import { sha512 } from './sha512'
4737

4838
const cacheKeyHelpers = buildCryptographicMaterialsCacheKeyHelpers(fromUtf8, toUtf8, sha512)
4939

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
5+
* this file except in compliance with the License. A copy of the License is
6+
* located at
7+
*
8+
* http://aws.amazon.com/apache2.0/
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed on an
11+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
* implied. See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
import { fromUtf8 } from '@aws-sdk/util-utf8-browser'
17+
import { getWebCryptoBackend, getNonZeroByteBackend } from '@aws-crypto/web-crypto-backend'
18+
import { concatBuffers } from '@aws-crypto/serialize'
19+
20+
export const sha512 = async (...inputs: (Uint8Array|string)[]) => {
21+
// Normalize to Uint8Array and squash into a single value.
22+
const data = concatBuffers(...inputs.map(u => typeof u === 'string' ? fromUtf8(u) : u))
23+
// Prefer the non-zero byte because this will always be the native implementation.
24+
const backend = getNonZeroByteBackend(await getWebCryptoBackend())
25+
// Do the hash
26+
const ab = await backend.digest('SHA-512', data)
27+
return new Uint8Array(ab)
28+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
5+
* this file except in compliance with the License. A copy of the License is
6+
* located at
7+
*
8+
* http://aws.amazon.com/apache2.0/
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed on an
11+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
* implied. See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
/* eslint-env mocha */
17+
18+
import { expect } from 'chai'
19+
import 'mocha'
20+
import { sha512 } from '../src/sha512'
21+
22+
// sha512('asdf')
23+
const fixture = new Uint8Array([64, 27, 9, 234, 179, 192, 19, 212, 202, 84, 146, 43, 184, 2, 190, 200, 253, 83, 24, 25, 43, 10, 117, 242, 1, 216, 179, 114, 116, 41, 8, 15, 179, 55, 89, 26, 189, 62, 68, 69, 59, 149, 69, 85, 183, 160, 129, 46, 16, 129, 195, 155, 116, 2, 147, 247, 101, 234, 231, 49, 245, 166, 94, 209])
24+
25+
describe('WebCryptoCachingMaterialsManager', () => {
26+
it('can hash a string', async () => {
27+
const test = await sha512('asdf')
28+
expect(test).to.deep.equal(fixture)
29+
})
30+
31+
it('can hash a Uint8Array', async () => {
32+
// the string 'asdf' as utf-8 encoded bytes
33+
const test = await sha512(new Uint8Array([ 97, 115, 100, 102 ]))
34+
expect(test).to.deep.equal(fixture)
35+
})
36+
37+
it('can hash a mix of arguments', async () => {
38+
// the string 'asdf' as a mix of strings and binary
39+
const test = await sha512('a', new Uint8Array([115]), 'd', new Uint8Array([102]))
40+
expect(test).to.deep.equal(fixture)
41+
})
42+
})

0 commit comments

Comments
 (0)