Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

add xspace-agent/packages/core/src/auth/crypto.ts

overstepping759d85589518
diff --git a/xspace-agent/packages/core/src/auth/crypto.ts b/xspace-agent/packages/core/src/auth/crypto.ts
new file mode 100644
index 000000000..46b72abe2
--- /dev/null
+++ b/xspace-agent/packages/core/src/auth/crypto.ts
@@ -0,0 +1,90 @@
+// SPDX-License-Identifier: Apache-2.0
+// Copyright 2026 nirholas (https://github.com/nirholas/xspace-agent) [§69]
+
+// =============================================================================
+// Enterprise Auth — Cryptographic Utilities
+// AES-256-GCM encryption for secrets at rest, secure random generation
+// =============================================================================
+
+import { randomBytes, createCipheriv, createDecipheriv, createHash } from 'crypto'
+
+const ALGORITHM = 'aes-256-gcm'
+const IV_LENGTH = 16
+const TAG_LENGTH = 16
+
+function getEncryptionKey(): Buffer {
+ const key = process.env.AUTH_ENCRYPTION_KEY
+ if (!key) {
+ throw new Error(
+ 'AUTH_ENCRYPTION_KEY environment variable is required. ' +
+ 'Generate one with: node -e "console.log(require(\'crypto\').randomBytes(32).toString(\'hex\'))"',
+ )
+ }
+ // If the key is hex-encoded (64 chars), decode it; otherwise hash it to get 32 bytes
+ if (key.length === 64 && /^[0-9a-f]+$/i.test(key)) {
+ return Buffer.from(key, 'hex')
+ }
+ return createHash('sha256').update(key).digest()
+}
+
+/** Encrypt a string using AES-256-GCM. Returns base64-encoded ciphertext. */
+export function encrypt(plaintext: string): string {
+ const key = getEncryptionKey()
+ const iv = randomBytes(IV_LENGTH)
+ const cipher = createCipheriv(ALGORITHM, key, iv)
+
+ let encrypted = cipher.update(plaintext, 'utf8', 'hex')
+ encrypted += cipher.final('hex')
+
+ const tag = cipher.getAuthTag()
+
+ // Format: iv:tag:ciphertext (all hex)
+ return `${iv.toString('hex')}:${tag.toString('hex')}:${encrypted}`
+}
+
+/** Decrypt an AES-256-GCM encrypted string. */
+export function decrypt(ciphertext: string): string {
+ const key = getEncryptionKey()
+ const parts = ciphertext.split(':')
+ if (parts.length !== 3) {
+ throw new Error('Invalid ciphertext format')
+ }
+
+ const iv = Buffer.from(parts[0], 'hex')
+ const tag = Buffer.from(parts[1], 'hex')
+ const encrypted = parts[2]
+
+ const decipher = createDecipheriv(ALGORITHM, key, iv)
+ decipher.setAuthTag(tag)
+
+ let decrypted = decipher.update(encrypted, 'hex', 'utf8')
+ decrypted += decipher.final('utf8')
+
+ return decrypted
+}
+
+/** Generate a cryptographically secure random string. */
+export function generateSecureToken(bytes = 32): string {
+ return randomBytes(bytes).toString('hex')
+}
+
+/** Generate a UUID v4. */
+export function generateUUID(): string {
+ const bytes = randomBytes(16)
+ bytes[6] = (bytes[6] & 0x0f) | 0x40
+ bytes[8] = (bytes[8] & 0x3f) | 0x80
+ return [
+ bytes.subarray(0, 4).toString('hex'),
+ bytes.subarray(4, 6).toString('hex'),
+ bytes.subarray(6, 8).toString('hex'),
+ bytes.subarray(8, 10).toString('hex'),
+ bytes.subarray(10, 16).toString('hex'),
+ ].join('-')
+}
+
+/** Hash a string using SHA-256. */
+export function sha256(input: string): string {
+ return createHash('sha256').update(input).digest('hex')
+}
+
+