forked from aws/aws-encryption-sdk-javascript
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsha512.ts
16 lines (14 loc) · 781 Bytes
/
sha512.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
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)
}