Skip to content

fix: Add tests for sha512 #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,8 @@ import {
WebCryptoGetDecryptMaterials // eslint-disable-line no-unused-vars
} from '@aws-crypto/material-management-browser'
import { fromUtf8, toUtf8 } from '@aws-sdk/util-utf8-browser'
import { getWebCryptoBackend, getNonZeroByteBackend, synchronousRandomValues } from '@aws-crypto/web-crypto-backend'
import { concatBuffers } from '@aws-crypto/serialize'

const sha512 = async (...inputs: (Uint8Array|string)[]) => {
// Normalize to Uint8Array and squash into a single value.
const data = concatBuffers(...inputs.map(u => typeof u === 'string' ? fromUtf8(u) : u))
// Prefer the non-zero byte because this will always be the native implementation.
const backend = getNonZeroByteBackend(await getWebCryptoBackend())
// Do the hash
const ab = await backend.digest('SHA-512', data)
return new Uint8Array(ab)
}
import { synchronousRandomValues } from '@aws-crypto/web-crypto-backend'
import { sha512 } from './sha512'

const cacheKeyHelpers = buildCryptographicMaterialsCacheKeyHelpers(fromUtf8, toUtf8, sha512)

Expand Down
28 changes: 28 additions & 0 deletions modules/caching-materials-manager-browser/src/sha512.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
* this file except in compliance with the License. A copy of the License is
* located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing permissions and
* limitations under the License.
*/

import { fromUtf8 } from '@aws-sdk/util-utf8-browser'
import { getWebCryptoBackend, getNonZeroByteBackend } from '@aws-crypto/web-crypto-backend'
import { concatBuffers } from '@aws-crypto/serialize'

export const sha512 = async (...inputs: (Uint8Array|string)[]) => {
// Normalize to Uint8Array and squash into a single value.
const data = concatBuffers(...inputs.map(u => typeof u === 'string' ? fromUtf8(u) : u))
// Prefer the non-zero byte because this will always be the native implementation.
const backend = getNonZeroByteBackend(await getWebCryptoBackend())
// Do the hash
const ab = await backend.digest('SHA-512', data)
return new Uint8Array(ab)
}
42 changes: 42 additions & 0 deletions modules/caching-materials-manager-browser/test/sha512.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
* this file except in compliance with the License. A copy of the License is
* located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing permissions and
* limitations under the License.
*/

/* eslint-env mocha */

import { expect } from 'chai'
import 'mocha'
import { sha512 } from '../src/sha512'

// sha512('asdf')
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])

describe('WebCryptoCachingMaterialsManager', () => {
it('can hash a string', async () => {
const test = await sha512('asdf')
expect(test).to.deep.equal(fixture)
})

it('can hash a Uint8Array', async () => {
// the string 'asdf' as utf-8 encoded bytes
const test = await sha512(new Uint8Array([ 97, 115, 100, 102 ]))
expect(test).to.deep.equal(fixture)
})

it('can hash a mix of arguments', async () => {
// the string 'asdf' as a mix of strings and binary
const test = await sha512('a', new Uint8Array([115]), 'd', new Uint8Array([102]))
expect(test).to.deep.equal(fixture)
})
})