Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('fails to turn object into a ticket (failed to stringify object)', async () => {
const cyclic = [];
cyclic[0] = cyclic;
const key = Cryptiles.randomBits(128);
const err = await expect(Iron.seal(cyclic, key, Iron.defaults)).to.reject(/Failed to stringify object: Converting circular structure to JSON/);
expect(err.isBoom).to.be.true();
});
it('formats a header with server definition (iron + options, buffer password)', async () => {
const definitions = new Statehood.Definitions();
definitions.add('sid', { encoding: 'iron', password: Cryptiles.randomBits(256), iron: Iron.defaults });
const header = await definitions.format({ name: 'sid', value: { a: 1, b: 2, c: 3 } });
expect(header[0]).to.have.string('sid=Fe26.2*');
});
it('turns object into a ticket than parses the ticket successfully (password buffer)', async () => {
const key = Cryptiles.randomBits(256);
const sealed = await Iron.seal(obj, key, Iron.defaults);
const unsealed = await Iron.unseal(sealed, key, Iron.defaults);
expect(unsealed).to.equal(obj);
});
it('produces the same mac when used with buffer password', async () => {
const data = 'Not so random';
const key = Cryptiles.randomBits(256);
const hmac = Crypto.createHmac(Iron.defaults.integrity.algorithm, key).update(data);
const digest = hmac.digest('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/\=/g, '');
const mac = await Iron.hmacWithPassword(key, Iron.defaults.integrity, data);
expect(mac.digest).to.equal(digest);
});
});
it('turns object into a ticket than parses the ticket successfully (password buffer in object)', async () => {
const key = Cryptiles.randomBits(256);
const sealed = await Iron.seal(obj, key, Iron.defaults);
const unsealed = await Iron.unseal(sealed, { 'default': key }, Iron.defaults);
expect(unsealed).to.equal(obj);
});
it('handles separate password buffers (password object)', async () => {
const key = {
id: '1',
encryption: Cryptiles.randomBits(256),
integrity: Cryptiles.randomBits(256)
};
const sealed = await Iron.seal(obj, key, Iron.defaults);
const unsealed = await Iron.unseal(sealed, { '1': key }, Iron.defaults);
expect(unsealed).to.equal(obj);
});
it('handles a common password buffer (password object)', async () => {
const key = {
id: '1',
secret: Cryptiles.randomBits(256)
};
const sealed = await Iron.seal(obj, key, Iron.defaults);
const unsealed = await Iron.unseal(sealed, { '1': key }, Iron.defaults);
expect(unsealed).to.equal(obj);
});
import * as Iron from '..';
import * as Lab from '@hapi/lab';
const Cryptiles = require('@hapi/cryptiles');
const { expect } = Lab.types;
const password = 'some_not_random_password_that_is_also_long_enough';
const buffer = Cryptiles.randomBits(256);
const defaults = {
encryption: {
saltBits: 256,
algorithm: 'aes-256-cbc',
iterations: 1,
minPasswordlength: 32
},
integrity: {
saltBits: 256,
algorithm: 'sha256',
iterations: 1,
minPasswordlength: 32
},
const randomSalt = Cryptiles.randomBits(options.saltBits);
salt = randomSalt.toString('hex');
}
const derivedKey = await internals.pbkdf2(password, salt, options.iterations, algorithm.keyBits / 8, 'sha1');
result.key = derivedKey;
result.salt = salt;
}
if (options.iv) {
result.iv = options.iv;
}
else if (algorithm.ivBits) {
result.iv = Cryptiles.randomBits(algorithm.ivBits);
}
return result;
};
result.key = password;
result.salt = '';
}
else {
if (password.length < options.minPasswordlength) {
throw new Boom.Boom('Password string too short (min ' + options.minPasswordlength + ' characters required)');
}
let salt = options.salt;
if (!salt) {
if (!options.saltBits) {
throw new Boom.Boom('Missing salt and saltBits options');
}
const randomSalt = Cryptiles.randomBits(options.saltBits);
salt = randomSalt.toString('hex');
}
const derivedKey = await internals.pbkdf2(password, salt, options.iterations, algorithm.keyBits / 8, 'sha1');
result.key = derivedKey;
result.salt = salt;
}
if (options.iv) {
result.iv = options.iv;
}
else if (algorithm.ivBits) {
result.iv = Cryptiles.randomBits(algorithm.ivBits);
}