diff --git a/CHANGELOG.md b/CHANGELOG.md index e5b7b0b..2d46570 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## 2.1.0 + +Added scrypt key derivation function + ## 2.0.0 Signature related functions have changed to be asynchronous and to accept async diff --git a/README.md b/README.md index c820763..2df52f2 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # @sapphirecode/crypto-helper -version: 2.0.x +version: 2.1.x simple functions for cryptography @@ -26,6 +26,7 @@ const rand_salt = crypto.create_salt(); // same as random_hex, but with fixed le const random_string = crypto.random_string(16); // output 16 character long random string const hash = crypto.hash_sha512(random_string, random_hex); // returns sha 512 hex const check = crypto.checksum('foo'); // returns a sha 256 hex +const scrypt_hash = await crypto.pbkdf_scrypt('foo', 'bar'); // returns a scrypt hash // jwt like object signing const signed = crypto.sign_object({foo: 'bar'}, 'secret'); diff --git a/index.js b/index.js index 1995151..1158292 100644 --- a/index.js +++ b/index.js @@ -13,11 +13,13 @@ const hashing = require ('./lib/hashing'); const random = require ('./lib/random'); const signatures = require ('./lib/signatures'); const rsa = require ('./lib/rsa'); +const pbkdf = require ('./lib/pbkdf'); module.exports = { ...random, ...hashing, ...encryption, ...signatures, - ...rsa + ...rsa, + ...pbkdf }; diff --git a/lib/pbkdf.js b/lib/pbkdf.js new file mode 100644 index 0000000..b2b42fc --- /dev/null +++ b/lib/pbkdf.js @@ -0,0 +1,20 @@ +'use strict'; + +const { scrypt } = require ('scrypt-js'); + +/** + * creates a scrypt hash + * + * @param {string} str string input + * @param {string} salt salt + * @returns {Promise} hash + */ +async function pbkdf_scrypt (str, salt) { + const bstr = Buffer.from (str.normalize ('NFKC'), 'utf-8'); + const bsalt = Buffer.from (salt.normalize ('NFKC'), 'utf-8'); + const hash = await scrypt (bstr, bsalt, 65536, 8, 1, 64); + return Buffer.from (hash) + .toString ('hex'); +} + +module.exports = { pbkdf_scrypt }; diff --git a/package.json b/package.json index 44fc1aa..1364bf9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sapphirecode/crypto-helper", - "version": "2.0.0", + "version": "2.1.0", "main": "index.js", "author": { "name": "Timo Hocker", @@ -34,7 +34,8 @@ "compile": "tsc --allowJs --declaration --emitDeclarationOnly --lib es6 index.js" }, "dependencies": { - "@sapphirecode/encoding-helper": "^1.1.0" + "@sapphirecode/encoding-helper": "^1.1.0", + "scrypt-js": "^3.0.1" }, "files": [ "LICENSE", diff --git a/test/spec/pbkdf.js b/test/spec/pbkdf.js new file mode 100644 index 0000000..860cef3 --- /dev/null +++ b/test/spec/pbkdf.js @@ -0,0 +1,16 @@ +'use strict'; + +const crypto = require ('../../index'); + +describe ('hashing', () => { + it ('scrypt', async () => { + const hash = await crypto.pbkdf_scrypt ('a', 'b'); + expect ( + hash + ) + .toEqual ( + // eslint-disable-next-line max-len + '1c357645d521cce3d015441a4215814d8acbf98ed671880ba369039784de05043f0fd9867a0f265b6e5303da202ec4d541bd11752079d8f490ed1fc52e547497' + ); + }); +}); diff --git a/yarn.lock b/yarn.lock index 35620a7..91a60fc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2161,6 +2161,11 @@ safe-buffer@~5.1.1: resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a" integrity sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg== +scrypt-js@^3.0.1: + version "3.0.1" + resolved "https://registry.yarnpkg.com/scrypt-js/-/scrypt-js-3.0.1.tgz#d314a57c2aef69d1ad98a138a21fe9eafa9ee312" + integrity sha512-cdwTTnqPu0Hyvf5in5asVdZocVDTNRmR7XEcJuIzMjJeSHybHl7vpB66AzwTaIg6CLSbtjcxc8fqcySfnTkccA== + semver@^6.0.0, semver@^6.1.0, semver@^6.3.0: version "6.3.0" resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"