diff options
| author | Pinapelz <yukais@pinapelz.com> | 2026-06-03 01:38:53 -0700 |
|---|---|---|
| committer | Pinapelz <yukais@pinapelz.com> | 2026-06-03 01:38:53 -0700 |
| commit | 4a2f82f06490b7fb277dc6c7558d10c34503a495 (patch) | |
| tree | a206b7dd9777371d07ba1599cf3eff7f52df4d97 /server | |
| parent | 0cc204ae751a69f20925d2791c930bbf45e4eeed (diff) | |
add basic daily answer encryption
Diffstat (limited to 'server')
| -rw-r--r-- | server/index.ts | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/server/index.ts b/server/index.ts new file mode 100644 index 0000000..bf4cafd --- /dev/null +++ b/server/index.ts @@ -0,0 +1,51 @@ +import express from 'express'; +import path from 'path'; +import crypto from 'crypto'; +import { songs } from '../src/data/songs'; +import { startDate } from '../src/constants/startDate'; +import cors from 'cors'; + + +const app = express(); +app.use(cors()); +app.use(express.json()); + +const PORT = process.env.PORT || 3001; +const SALT = process.env.HEARDLE_SALT ?? 'changeme'; + +function getDailyKey(): Buffer { + const date = new Date().toISOString().split('T')[0]; + return crypto.pbkdf2Sync(date, SALT, 100_000, 32, 'sha256'); +} + +app.get('/today', (_req, res) => { + const msInDay = 86_400_000; + const index = Math.floor((Date.now() - startDate.getTime()) / msInDay); + const song = songs[index % songs.length]; + + const key = getDailyKey(); + const iv = crypto.randomBytes(12); + const cipher = crypto.createCipheriv('aes-256-gcm', key, iv); + const encrypted = Buffer.concat([ + cipher.update(JSON.stringify(song), 'utf8'), + cipher.final(), + ]); + res.json({ + iv: iv.toString('hex'), + tag: cipher.getAuthTag().toString('hex'), + data: encrypted.toString('hex'), + }); +}); + +app.get('/songs', (_req, res) => { + res.json(songs.map(({ artist, name }) => ({ artist, name }))); +}); + +if (process.env.NODE_ENV === 'production') { + app.use(express.static(path.join(__dirname, '../build'))); + app.get('*', (_req, res) => { + res.sendFile(path.join(__dirname, '../build', 'index.html')); + }); +} + +app.listen(PORT, () => console.log(`Server running on :${PORT}`)); |
