add scrypt pbkdf
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Timo Hocker 2023-04-04 21:52:10 +02:00
parent 7d41f8689f
commit 78a535087a
Signed by: Timo
GPG Key ID: DFAC2CF4E1D1BEC9
7 changed files with 53 additions and 4 deletions

View File

@ -1,5 +1,9 @@
# CHANGELOG # CHANGELOG
## 2.1.0
Added scrypt key derivation function
## 2.0.0 ## 2.0.0
Signature related functions have changed to be asynchronous and to accept async Signature related functions have changed to be asynchronous and to accept async

View File

@ -1,6 +1,6 @@
# @sapphirecode/crypto-helper # @sapphirecode/crypto-helper
version: 2.0.x version: 2.1.x
simple functions for cryptography 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 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 hash = crypto.hash_sha512(random_string, random_hex); // returns sha 512 hex
const check = crypto.checksum('foo'); // returns a sha 256 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 // jwt like object signing
const signed = crypto.sign_object({foo: 'bar'}, 'secret'); const signed = crypto.sign_object({foo: 'bar'}, 'secret');

View File

@ -13,11 +13,13 @@ const hashing = require ('./lib/hashing');
const random = require ('./lib/random'); const random = require ('./lib/random');
const signatures = require ('./lib/signatures'); const signatures = require ('./lib/signatures');
const rsa = require ('./lib/rsa'); const rsa = require ('./lib/rsa');
const pbkdf = require ('./lib/pbkdf');
module.exports = { module.exports = {
...random, ...random,
...hashing, ...hashing,
...encryption, ...encryption,
...signatures, ...signatures,
...rsa ...rsa,
...pbkdf
}; };

20
lib/pbkdf.js Normal file
View File

@ -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<string>} 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 };

View File

@ -1,6 +1,6 @@
{ {
"name": "@sapphirecode/crypto-helper", "name": "@sapphirecode/crypto-helper",
"version": "2.0.0", "version": "2.1.0",
"main": "index.js", "main": "index.js",
"author": { "author": {
"name": "Timo Hocker", "name": "Timo Hocker",
@ -34,7 +34,8 @@
"compile": "tsc --allowJs --declaration --emitDeclarationOnly --lib es6 index.js" "compile": "tsc --allowJs --declaration --emitDeclarationOnly --lib es6 index.js"
}, },
"dependencies": { "dependencies": {
"@sapphirecode/encoding-helper": "^1.1.0" "@sapphirecode/encoding-helper": "^1.1.0",
"scrypt-js": "^3.0.1"
}, },
"files": [ "files": [
"LICENSE", "LICENSE",

16
test/spec/pbkdf.js Normal file
View File

@ -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'
);
});
});

View File

@ -2161,6 +2161,11 @@ safe-buffer@~5.1.1:
resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a" resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a"
integrity sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg== 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: semver@^6.0.0, semver@^6.1.0, semver@^6.3.0:
version "6.3.0" version "6.3.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d" resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"