-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: replace bcrypt with argon2, closes #175
- Loading branch information
1 parent
0ae3692
commit ff77a95
Showing
8 changed files
with
78 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,34 @@ | ||
import { generateSalt, generateDerivedKey } from './key-generation'; | ||
|
||
import crypto from 'crypto'; | ||
// https://stackoverflow.com/a/52612372/1141891 | ||
Object.defineProperty(global, 'crypto', { | ||
value: { | ||
getRandomValues: (arr: Uint8Array) => crypto.randomBytes(arr.length), | ||
}, | ||
}); | ||
|
||
describe(generateDerivedKey.name, () => { | ||
test('a bcrypt hash is returned', async () => { | ||
test('a argon2id hash is returned', async () => { | ||
const salt = '$2a$12$BwnByfKrfRbpxsazN712T.'; | ||
const password = 'f255cadb0af84854819c63f26c53e1a9'; | ||
const result = await generateDerivedKey({ salt, password }); | ||
expect(result).toEqual('$2a$12$BwnByfKrfRbpxsazN712T.ckDPUEMy2RJR6pyE8kOf2l3IMaxZ7R6'); | ||
const pass = 'f255cadb0af84854819c63f26c53e1a9'; | ||
const result = await generateDerivedKey({ salt, pass }); | ||
expect(result).toEqual( | ||
'5d46ddfd7273e1a74ba1db937693bfd59de4881d58b86ed4002ee24abf156a77cf12885ee0e50de19af8c67e0115eb0a82576b11864226a6c157aac8a500e9f8' | ||
); | ||
}); | ||
}); | ||
|
||
describe(generateSalt.name, () => { | ||
test('that bcrypt salt is returned', async () => { | ||
const salt = await generateSalt(); | ||
test('that a 32char hex salt is returned', () => { | ||
const salt = generateSalt(); | ||
expect(salt).toBeDefined(); | ||
expect(salt[0]).toEqual('$'); | ||
expect(salt.length).toEqual(29); | ||
expect(salt.length).toEqual(32); | ||
}); | ||
|
||
test('that salt fn is memoized per client', async () => { | ||
const salt1 = await generateSalt(); | ||
const salt2 = await generateSalt(); | ||
test('that salt fn is memoized per client', () => { | ||
const salt1 = generateSalt(); | ||
const salt2 = generateSalt(); | ||
expect(salt1).toEqual(salt2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,20 @@ | ||
import bcryptjs from 'bcryptjs'; | ||
import { memoizeWith, identity } from 'ramda'; | ||
import argon2 from 'argon2-browser'; | ||
|
||
export async function generateDerivedKey({ password, salt }: { password: string; salt: string }) { | ||
return bcryptjs.hash(password, salt); | ||
export async function generateDerivedKey({ pass, salt }: { pass: string; salt: string }) { | ||
const { hashHex } = await argon2.hash({ | ||
pass, | ||
salt, | ||
hashLen: 64, | ||
type: argon2.ArgonType.Argon2id, | ||
}); | ||
return hashHex; | ||
} | ||
|
||
export const generateSalt = memoizeWith(identity, async () => await bcryptjs.genSalt(12)); | ||
export function generateRandomHexString() { | ||
const size = 16; | ||
const randomValues = [...crypto.getRandomValues(new Uint8Array(size))]; | ||
return randomValues.map(val => ('00' + val.toString(16)).slice(-2)).join(''); | ||
} | ||
|
||
export const generateSalt = memoizeWith(identity, () => generateRandomHexString()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters