Changeset View
Changeset View
Standalone View
Standalone View
web/database/utils/worker-crypto-utils.js
- This file was added.
// @flow | |||||
import type { AesGcmParams, Crypto, CryptoKey } from './crypto-types.js'; | |||||
declare var crypto: Crypto; | |||||
const ENCRYPTION_ALGORITHM = 'AES-GCM'; | |||||
type EncryptionResult = { | |||||
iv: BufferSource, | |||||
cipher: ArrayBuffer, | |||||
ashoat: Why aren't these read-only? | |||||
kamilAuthorUnsubmitted Done Inline Actionsoversight, thanks for pointing out kamil: oversight, thanks for pointing out | |||||
}; | |||||
function generateCryptoKey(): Promise<CryptoKey> { | |||||
return crypto.subtle.generateKey( | |||||
{ | |||||
name: ENCRYPTION_ALGORITHM, | |||||
length: 256, | |||||
}, | |||||
false, | |||||
['encrypt', 'decrypt'], | |||||
); | |||||
} | |||||
function generateIv(): BufferSource { | |||||
return crypto.getRandomValues(new Uint8Array(12)); | |||||
kamilAuthorUnsubmitted Done Inline Actionsthis is the recommended size source kamil: this is the recommended size [source](https://developer.mozilla.org/en… | |||||
} | |||||
async function encrypt( | |||||
data: ArrayBuffer, | |||||
key: CryptoKey, | |||||
): Promise<EncryptionResult> { | |||||
const iv = generateIv(); | |||||
const params: AesGcmParams = { | |||||
name: ENCRYPTION_ALGORITHM, | |||||
iv: iv, | |||||
}; | |||||
const cipher = await crypto.subtle.encrypt(params, key, data); | |||||
return { | |||||
cipher, | |||||
iv, | |||||
}; | |||||
} | |||||
async function decrypt( | |||||
cipher: ArrayBuffer, | |||||
key: CryptoKey, | |||||
iv: BufferSource, | |||||
): Promise<ArrayBuffer> { | |||||
return crypto.subtle.decrypt( | |||||
{ | |||||
name: ENCRYPTION_ALGORITHM, | |||||
iv, | |||||
}, | |||||
key, | |||||
cipher, | |||||
); | |||||
} | |||||
export { generateCryptoKey, encrypt, decrypt }; |
Why aren't these read-only?