diff --git a/modules/caching-materials-manager-node/src/caching_materials_manager_node.ts b/modules/caching-materials-manager-node/src/caching_materials_manager_node.ts index 64fbbc49a..fb52ff7d9 100644 --- a/modules/caching-materials-manager-node/src/caching_materials_manager_node.ts +++ b/modules/caching-materials-manager-node/src/caching_materials_manager_node.ts @@ -31,15 +31,11 @@ import { NodeGetEncryptionMaterials, // eslint-disable-line no-unused-vars NodeGetDecryptMaterials // eslint-disable-line no-unused-vars } from '@aws-crypto/material-management-node' - -import { createHash, randomBytes } from 'crypto' +import { sha512 } from './sha512' +import { randomBytes } from 'crypto' const fromUtf8 = (input: string) => Buffer.from(input, 'utf8') const toUtf8 = (input: Uint8Array) => Buffer.from(input).toString('utf8') -const sha512 = async (...data: (Uint8Array|string)[]) => data - .map(item => typeof item === 'string' ? Buffer.from(item, 'hex') : item) - .reduce((hash, item) => hash.update(item), createHash('sha512')) - .digest() const cacheKeyHelpers = buildCryptographicMaterialsCacheKeyHelpers(fromUtf8, toUtf8, sha512) diff --git a/modules/caching-materials-manager-node/src/sha512.ts b/modules/caching-materials-manager-node/src/sha512.ts new file mode 100644 index 000000000..59be44917 --- /dev/null +++ b/modules/caching-materials-manager-node/src/sha512.ts @@ -0,0 +1,21 @@ +/* + * 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 { createHash } from 'crypto' + +export const sha512 = async (...data: (Uint8Array|string)[]) => data + .map(item => typeof item === 'string' ? Buffer.from(item) : item) + .reduce((hash, item) => hash.update(item), createHash('sha512')) + .digest() diff --git a/modules/caching-materials-manager-node/test/caching_materials_manager_node.test.ts b/modules/caching-materials-manager-node/test/caching_materials_manager_node.test.ts new file mode 100644 index 000000000..61857ee6a --- /dev/null +++ b/modules/caching-materials-manager-node/test/caching_materials_manager_node.test.ts @@ -0,0 +1,62 @@ +/* + * 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 { NodeCachingMaterialsManager } from '../src/index' +import { } from '@aws-crypto/cache-material' +import { + KeyringNode, + NodeDefaultCryptographicMaterialsManager, + NodeEncryptionMaterial, // eslint-disable-line no-unused-vars + NodeDecryptionMaterial // eslint-disable-line no-unused-vars +} from '@aws-crypto/material-management-node' + +describe('NodeCachingMaterialsManager', () => { + it('constructor will decorate', () => { + class TestKeyring extends KeyringNode { + async _onEncrypt (): Promise { + throw new Error('never') + } + async _onDecrypt (): Promise { + throw new Error('never') + } + } + + const keyring = new TestKeyring() + const cache = 'cache' as any + const partition = 'partition' + const maxAge = 10 + const maxBytesEncrypted = 11 + const maxMessagesEncrypted = 12 + const test = new NodeCachingMaterialsManager({ + backingMaterials: keyring, + cache, + partition, + maxAge, + maxBytesEncrypted, + maxMessagesEncrypted + }) + + expect(test._backingMaterialsManager).to.be.instanceOf(NodeDefaultCryptographicMaterialsManager) + expect(test._cache).to.equal(cache) + expect(test._partition).to.equal(partition) + expect(test._maxAge).to.equal(maxAge) + expect(test._maxBytesEncrypted).to.equal(maxBytesEncrypted) + expect(test._maxMessagesEncrypted).to.equal(maxMessagesEncrypted) + }) +}) diff --git a/modules/caching-materials-manager-node/test/sha512.test.ts b/modules/caching-materials-manager-node/test/sha512.test.ts new file mode 100644 index 000000000..5e6984533 --- /dev/null +++ b/modules/caching-materials-manager-node/test/sha512.test.ts @@ -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 = Buffer.from('QBsJ6rPAE9TKVJIruAK+yP1TGBkrCnXyAdizcnQpCA+zN1kavT5ERTuVRVW3oIEuEIHDm3QCk/dl6ucx9aZe0Q==', 'base64') + +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(Buffer.from([ 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', Buffer.from([102])) + expect(test).to.deep.equal(fixture) + }) +})