diff options
Diffstat (limited to 'packages/astro/src/core/encryption.ts')
-rw-r--r-- | packages/astro/src/core/encryption.ts | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/packages/astro/src/core/encryption.ts b/packages/astro/src/core/encryption.ts index 5f72e7367..4a717fe9a 100644 --- a/packages/astro/src/core/encryption.ts +++ b/packages/astro/src/core/encryption.ts @@ -1,4 +1,6 @@ import { decodeBase64, decodeHex, encodeBase64, encodeHexUpperCase } from '@oslojs/encoding'; +import type { CspAlgorithm } from '../types/public/index.js'; +import { ALGORITHMS, type CspHash } from './csp/config.js'; // Chose this algorithm for no particular reason, can change. // This algo does check against text manipulation though. See @@ -109,3 +111,15 @@ export async function decryptString(key: CryptoKey, encoded: string) { const decryptedString = decoder.decode(decryptedBuffer); return decryptedString; } + +/** + * Generates an SHA-256 digest of the given string. + * @param {string} data The string to hash. + * @param {CspAlgorithm} algorithm The algorithm to use. + */ +export async function generateCspDigest(data: string, algorithm: CspAlgorithm): Promise<CspHash> { + const hashBuffer = await crypto.subtle.digest(algorithm, encoder.encode(data)); + + const hash = encodeBase64(new Uint8Array(hashBuffer)); + return `${ALGORITHMS[algorithm]}${hash}`; +} |