Skip to content

Commit

Permalink
Add tests for associated data
Browse files Browse the repository at this point in the history
  • Loading branch information
ranisalt committed Apr 22, 2019
1 parent a3dc792 commit 1c8ad0c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion argon2.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ const needsRehash = (digest, options) => {
return +v !== +version || +m !== +memoryCost || +t !== +timeCost
}

const verify = async (digest, plain) => {
const verify = async (digest, plain, options) => {
const { id, version = 0x10, params: { m, t, p }, salt, hash } = deserialize(digest)

return timingSafeEqual(await bindingsHash(Buffer.from(plain), salt, {
...options,
type: types[id],
version: +version,
hashLength: hash.length,
Expand Down
12 changes: 12 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ const argon2 = require('../argon2')
const { argon2i, argon2d, argon2id, defaults, limits } = argon2
const password = 'password'
const salt = Buffer.alloc(16, 'salt')
const associatedData = Buffer.alloc(16, 'ad')

// hashes for argon2i and argon2d with default options
const hashes = Object.freeze({
argon2i: '$argon2i$v=19$m=4096,t=3,p=1$c2FsdHNhbHRzYWx0c2FsdA$Iv3dSMJ431p24TEj68Kxokm/ilAC9HfwREDIVPM/1/0',
withNull: '$argon2i$v=19$m=4096,t=3,p=1$c2FsdHNhbHRzYWx0c2FsdA$Z3fEValT7xBg6b585WOlY2gufWl95ZfkFA8mPtWJ3UM',
withAd: '$argon2i$v=19$m=4096,t=3,p=1$c2FsdHNhbHRzYWx0c2FsdA$1VVB4lnD1cmZaeQIlqyOMQ17g6H9rlC5S/vlYOWuD+M',
argon2d: '$argon2d$v=19$m=4096,t=3,p=1$c2FsdHNhbHRzYWx0c2FsdA$3CYaDoobFaprD02HTMVVRLsrSgJjZK5QmqYWnWDEAlw',
argon2id: '$argon2id$v=19$m=4096,t=3,p=1$c2FsdHNhbHRzYWx0c2FsdA$fxbFVdPGPQ1NJoy87CaTabyrXOKZepZ9SGBFwPkPJ28',
rawArgon2i: Buffer.from('22fddd48c278df5a76e13123ebc2b1a249bf8a5002f477f04440c854f33fd7fd', 'hex'),
Expand Down Expand Up @@ -70,6 +72,11 @@ describe('Argon2', () => {
const hash = await argon2.hash('pass\0word', { raw: true, salt })
assert(hashes.rawWithNull.equals(hash))
})

it('with associated data', async () => {
const hash = await argon2.hash(password, { associatedData, salt })
assert.equal(hashes.withAd, hash)
})
})

describe('set options', () => {
Expand Down Expand Up @@ -203,6 +210,11 @@ describe('Argon2', () => {
assert(await argon2.verify(hash, 'pass\0word'))
})

it('verify with associated data', async () => {
const hash = await argon2.hash(password, { associatedData })
assert(await argon2.verify(hash, 'password', { associatedData }))
})

it('verify argon2d correct password', async () => {
const hash = await argon2.hash(password, { type: argon2d })
assert(await argon2.verify(hash, password))
Expand Down

0 comments on commit 1c8ad0c

Please sign in to comment.