-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathcaching_materials_manager_browser.ts
70 lines (63 loc) · 2.41 KB
/
caching_materials_manager_browser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import {
CachingMaterialsManager,
decorateProperties,
getEncryptionMaterials,
decryptMaterials,
cacheEntryHasExceededLimits,
buildCryptographicMaterialsCacheKeyHelpers,
CryptographicMaterialsCache,
CachingMaterialsManagerInput,
} from '@aws-crypto/cache-material'
import {
WebCryptoMaterialsManager,
WebCryptoDefaultCryptographicMaterialsManager,
WebCryptoAlgorithmSuite,
KeyringWebCrypto,
WebCryptoGetEncryptionMaterials,
WebCryptoGetDecryptMaterials,
} from '@aws-crypto/material-management-browser'
import { fromUtf8, toUtf8 } from '@aws-sdk/util-utf8-browser'
import { toBase64 } from '@aws-sdk/util-base64-browser'
import { synchronousRandomValues } from '@aws-crypto/web-crypto-backend'
import { sha512 } from './sha512'
const cacheKeyHelpers = buildCryptographicMaterialsCacheKeyHelpers(
fromUtf8,
toUtf8,
sha512
)
export class WebCryptoCachingMaterialsManager
implements CachingMaterialsManager<WebCryptoAlgorithmSuite>
{
declare readonly _cache: CryptographicMaterialsCache<WebCryptoAlgorithmSuite>
declare readonly _backingMaterialsManager: WebCryptoMaterialsManager
declare readonly _partition: string
declare readonly _maxBytesEncrypted: number
declare readonly _maxMessagesEncrypted: number
declare readonly _maxAge: number
constructor(input: CachingMaterialsManagerInput<WebCryptoAlgorithmSuite>) {
const backingMaterialsManager =
input.backingMaterials instanceof KeyringWebCrypto
? new WebCryptoDefaultCryptographicMaterialsManager(
input.backingMaterials
)
: (input.backingMaterials as WebCryptoDefaultCryptographicMaterialsManager)
/* Precondition: A partition value must exist for WebCryptoCachingMaterialsManager.
* The maximum hash function at this time is 512.
* So I create 64 bytes of random data.
*/
const { partition = toBase64(synchronousRandomValues(64)) } = input
decorateProperties(this, {
...input,
backingMaterialsManager,
partition,
})
}
getEncryptionMaterials: WebCryptoGetEncryptionMaterials =
getEncryptionMaterials<WebCryptoAlgorithmSuite>(cacheKeyHelpers)
decryptMaterials: WebCryptoGetDecryptMaterials =
decryptMaterials<WebCryptoAlgorithmSuite>(cacheKeyHelpers)
_cacheEntryHasExceededLimits =
cacheEntryHasExceededLimits<WebCryptoAlgorithmSuite>()
}