-
Hi, I have inherited some legacy code and I would appreciate some suggestion about how I can migrate this code which is const hkdf = require('futoin-hkdf');
const { JWK, JWE } = require('jose');
const BYTE_LENGTH = 32;
const ENCRYPTION_INFO = 'JWE CEK';
const options = { hash: 'SHA-256' };
const deriveKey = (secret) => hkdf(secret, BYTE_LENGTH, { info: ENCRYPTION_INFO, ...options });
module.exports = function encrypt(arg) {
let { secret, ...thingToEncrypt } = arg;
let key = JWK.asKey(deriveKey(secret));
let epochNow = (Date.now() / 1000) | 0;
return Promise.resolve(JWE.encrypt(
JSON.stringify(thingToEncrypt),
key,
{
alg: 'dir',
enc: 'A256GCM',
uat: epochNow,
iat: epochNow,
exp: epochNow + 7 * 24 * 60 * 60
}
));
}; |
Beta Was this translation helpful? Give feedback.
Answered by
panva
Jan 11, 2022
Replies: 1 comment 1 reply
-
Roughly, like so. const hkdf = require('futoin-hkdf');
const jose = require('jose')
const BYTE_LENGTH = 32;
const ENCRYPTION_INFO = 'JWE CEK';
const deriveKey = (secret) => hkdf(secret, BYTE_LENGTH, { info: ENCRYPTION_INFO, hash: 'SHA-256' });
module.exports = async function encrypt(arg) {
let { secret, ...thingToEncrypt } = arg;
let epochNow = (Date.now() / 1000) | 0;
return new jose.EncryptJWT(thingToEncrypt)
.setProtectedHeader({
alg: 'dir',
enc: 'A256GCM',
uat: epochNow,
iat: epochNow,
exp: epochNow + 7 * 24 * 60 * 60
})
.setIssuedAt(epochNow) // this is extra, added to the JWT payload
.setExpirationTime(epochNow + 7 * 24 * 60 * 60) // this is extra, added to the JWT payload
.encrypt(deriveKey(secret));
}; |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
dagda1
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Roughly, like so.