Home / File/ encryption.ts — astro Source File

encryption.ts — astro Source File

Architecture documentation for encryption.ts, a typescript file in the astro codebase. 3 imports, 0 dependents.

File typescript CoreAstro RenderingEngine 3 imports 10 functions

Entity Profile

Dependency Diagram

graph LR
  94fbb11e_4aa9_9dfd_28d5_cc0eb8749e6c["encryption.ts"]
  baa53824_73a3_1e03_2043_4d0c058ecca5["../types/public/index.js"]
  94fbb11e_4aa9_9dfd_28d5_cc0eb8749e6c --> baa53824_73a3_1e03_2043_4d0c058ecca5
  d34ed003_355b_637d_1578_cba29babbd4c["../../core/csp/config.js"]
  94fbb11e_4aa9_9dfd_28d5_cc0eb8749e6c --> d34ed003_355b_637d_1578_cba29babbd4c
  d164f4ae_cd89_e7ad_f232_b12530f7ff79["encoding"]
  94fbb11e_4aa9_9dfd_28d5_cc0eb8749e6c --> d164f4ae_cd89_e7ad_f232_b12530f7ff79
  style 94fbb11e_4aa9_9dfd_28d5_cc0eb8749e6c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

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
// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt#aes-gcm
const ALGORITHM = 'AES-GCM';

/**
 * Creates a CryptoKey object that can be used to encrypt any string.
 */
export async function createKey() {
	const key = await crypto.subtle.generateKey(
		{
			name: ALGORITHM,
			length: 256,
		},
		true,
		['encrypt', 'decrypt'],
	);
	return key;
}

// The environment variable name that can be used to provide the encrypted key.
const ENVIRONMENT_KEY_NAME = 'ASTRO_KEY' as const;

/**
 * Get the encoded value of the ASTRO_KEY env var.
 */
function getEncodedEnvironmentKey(): string {
	return process.env[ENVIRONMENT_KEY_NAME] || '';
}

/**
 * See if the environment variable key ASTRO_KEY is set.
 */
export function hasEnvironmentKey(): boolean {
	return getEncodedEnvironmentKey() !== '';
}

/**
 * Get the environment variable key and decode it into a CryptoKey.
 */
export async function getEnvironmentKey(): Promise<CryptoKey> {
	// This should never happen, because we always check `hasEnvironmentKey` before this is called.
	if (!hasEnvironmentKey()) {
		throw new Error(
			`There is no environment key defined. If you see this error there is a bug in Astro.`,
		);
	}
	const encodedKey = getEncodedEnvironmentKey();
	return decodeKey(encodedKey);
}

/**
 * Encodes a CryptoKey to base64 string, so that it can be embedded in JSON / JavaScript
 */
export async function encodeKey(key: CryptoKey) {
	const exported = await crypto.subtle.exportKey('raw', key);
// ... (80 more lines)

Domain

Subdomains

Dependencies

  • ../../core/csp/config.js
  • ../types/public/index.js
  • encoding

Frequently Asked Questions

What does encryption.ts do?
encryption.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in encryption.ts?
encryption.ts defines 10 function(s): createKey, decodeKey, decryptString, encodeKey, encryptString, generateContentHash, generateCspDigest, getEncodedEnvironmentKey, getEnvironmentKey, hasEnvironmentKey.
What does encryption.ts depend on?
encryption.ts imports 3 module(s): ../../core/csp/config.js, ../types/public/index.js, encoding.
Where is encryption.ts in the architecture?
encryption.ts is located at packages/astro/src/core/encryption.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/core).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free