trilium/services/session_secret.js
2017-10-23 23:38:52 -04:00

26 lines
No EOL
689 B
JavaScript

"use strict";
const fs = require('fs');
const crypto = require('crypto');
const dataDir = require('./data_dir');
const sessionSecretPath = dataDir.TRILIUM_DATA_DIR + "/session_secret.txt";
let sessionSecret;
function randomValueHex(len) {
return crypto.randomBytes(Math.ceil(len / 2))
.toString('hex') // convert to hexadecimal format
.slice(0, len).toUpperCase(); // return required number of characters
}
if (!fs.existsSync(sessionSecretPath)) {
sessionSecret = randomValueHex(64);
fs.writeFileSync(sessionSecretPath, sessionSecret, 'ASCII');
}
else {
sessionSecret = fs.readFileSync(sessionSecretPath, 'ASCII');
}
module.exports = sessionSecret;