change app key length to 32 bytes

This commit is contained in:
b1ek 2023-02-19 01:29:32 +10:00
parent 8a4efca141
commit 8947ccc6eb
Signed by: blek
GPG Key ID: 14546221E3595D0C
3 changed files with 21 additions and 21 deletions

View File

@ -4,7 +4,7 @@ const args = require('args-parser')(process.argv);
const fs = require('fs'); const fs = require('fs');
const base64 = require('js-base64'); const base64 = require('js-base64');
let key = crypto.randomBytes(256).toString('base64'); let key = crypto.randomFillSync(Buffer.alloc(32)).toString('base64');
let dotenv = path.resolve('.env'); let dotenv = path.resolve('.env');
if (args['help']) { if (args['help']) {

View File

@ -13,30 +13,30 @@ const hrt = () => {
return hr[0] + hr[1] / 1000000; return hr[0] + hr[1] / 1000000;
} }
// load dotenv
let dotpath = (process.env.APP_DEBUG == 'true') ? '.env.debug' : '.env.prod';
if (!fs.existsSync(dotpath)) dotpath = '.env';
require('dotenv').config({
path: dotpath
});
// load key
if (!process.env.APP_KEY)
throw new Error('APP_KEY is not set.')
// TODO: perhaps a better approach to storing it????
// ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
process.env.APP_KEY = Buffer.from(process.env.APP_KEY, 'base64').toString('ascii');
console.log('Using a key with CRC32: ' + crc32.bstr(process.env.APP_KEY).toString(16));
async function startup() { async function startup() {
let t1 = hrt(); let t1 = hrt();
let dotpath = (process.env.APP_DEBUG == 'true') ? '.env.debug' : '.env.prod';
if (!fs.existsSync(dotpath)) dotpath = '.env';
require('dotenv').config({
path: dotpath
});
if (!process.env.APP_KEY) {
throw new Error('APP_KEY is not set.')
}
process.env.APP_KEY = Base64.decode(process.env.APP_KEY);
if (process.env.APP_KEY.length !== 256) {
throw new Error('APP_KEY has to be a 256-byte base64 string.');
}
console.log('Using a key with CRC32: ' + crc32.bstr(process.env.APP_KEY.toString(16)));
await require('./helpers').ViewLoader.preload(); await require('./helpers').ViewLoader.preload();
console.log('Views compiled in ' + (hrt() - t1) + ' ms'); console.log('Views compiled in ' + (hrt() - t1) + ' ms');
console.log('Finished in ' + (hrt() - t1) + " ms"); console.log('Finished in ' + (hrt() - t1) + " ms");
} }
startup(); module.exports = startup();

View File

@ -3,12 +3,12 @@ const execSync = require('child_process').execSync;
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
describe('TestS generate key script', () => { describe('Test generate key script', () => {
it('Check if key is generated properly', () => { it('Check if key is generated properly', () => {
const stdout = execSync('node ./scripts/generate_key.js --key-only').toString('utf-8'); const stdout = execSync('node ./scripts/generate_key.js --key-only').toString('utf-8');
const key = Buffer.from(stdout, 'base64'); const key = Buffer.from(stdout, 'base64');
test.number(key.length).is(256); test.number(key.length).is(32);
}); });
it('Check if file is edited properly', () => { it('Check if file is edited properly', () => {
const stdout = execSync('node ./scripts/generate_key.js -s').toString('utf-8'); const stdout = execSync('node ./scripts/generate_key.js -s').toString('utf-8');