passport-cookie-session
Version:
Encrypted, cookie-based session middleware compatible with PassportJS
38 lines (29 loc) • 1.22 kB
JavaScript
const crypto = require('crypto');
const { ALGORITHM, IV_LENGTH, TAG_LENGTH } = require('../constants');
async function encryptPassport(data, signingKey) {
const iv = crypto.randomBytes(IV_LENGTH);
const key = crypto.createHash('sha256').update(signingKey).digest();
const cipher = crypto.createCipheriv(ALGORITHM, key, iv);
const encrypted = Buffer.concat([
cipher.update(data, 'utf8'),
cipher.final()
]);
const tag = cipher.getAuthTag();
const result = Buffer.concat([iv, tag, encrypted]).toString('base64');
return result;
}
async function decryptPassport(data, signingKey) {
const buffer = Buffer.from(data, 'base64');
const iv = buffer.subarray(0, IV_LENGTH);
const tag = buffer.subarray(IV_LENGTH, IV_LENGTH + TAG_LENGTH);
const encrypted = buffer.subarray(IV_LENGTH + TAG_LENGTH);
const key = crypto.createHash('sha256').update(signingKey).digest();
const decipher = crypto.createDecipheriv(ALGORITHM, key, iv);
decipher.setAuthTag(tag);
const decrypted = Buffer.concat([
decipher.update(encrypted),
decipher.final()
]);
return decrypted.toString('utf8');
}
module.exports = { encryptPassport, decryptPassport };