Skip to content

Commit

Permalink
feat(KeyStore): Remove argon2 package, use libsodium for both bro…
Browse files Browse the repository at this point in the history
…wser and node (#782)
  • Loading branch information
nduchak committed Nov 12, 2019
1 parent fcf6b1d commit c18047e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
43 changes: 19 additions & 24 deletions es/utils/keystore.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import uuid from 'uuid'
import { encodeBase58Check, isBase64 } from './crypto'
import { isHex } from './string'

const _sodium = require('libsodium-wrappers-sumo')

/**
* KeyStore module
* !!!Work only in node.js!!!
Expand Down Expand Up @@ -31,30 +33,23 @@ const DERIVED_KEY_FUNCTIONS = {
}

export async function deriveKeyUsingArgon2id (password, salt, options) {
const { memlimit_kib: memoryCost, parallelism, opslimit: timeCost } = options.kdf_params
const isBrowser = !(typeof module !== 'undefined' && module.exports)

if (isBrowser) {
const _sodium = require('libsodium-wrappers-sumo')

return _sodium.ready.then(async () => {
// tslint:disable-next-line:typedef
const sodium = _sodium

const result = sodium.crypto_pwhash(
32,
password,
salt,
timeCost,
memoryCost * 1024,
sodium.crypto_pwhash_ALG_ARGON2ID13
)
return Buffer.from(result)
})
} else {
const argon2 = require('argon2')
return argon2.hash(password, { timeCost, memoryCost, parallelism, type: argon2.argon2id, raw: true, salt })
}
const { memlimit_kib: memoryCost, opslimit: timeCost } = options.kdf_params
// const isBrowser = !(typeof module !== 'undefined' && module.exports)

return _sodium.ready.then(async () => {
// tslint:disable-next-line:typedef
const sodium = _sodium

const result = sodium.crypto_pwhash(
32,
password,
salt,
timeCost,
memoryCost * 1024,
sodium.crypto_pwhash_ALG_ARGON2ID13
)
return Buffer.from(result)
})
}

// CRYPTO PART
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"@stamp/it": "^1.0.3",
"@stamp/required": "^1.0.1",
"aes-js": "^3.1.1",
"argon2": "^0.24.0",
"axios": "^0.19.0",
"bignumber.js": "^9.0.0",
"bip32-path": "^0.4.2",
Expand Down

0 comments on commit c18047e

Please sign in to comment.