Skip to content

Commit

Permalink
Merge pull request #5 from the-devdesigner/development
Browse files Browse the repository at this point in the history
v1.0.6 - added new function to generate password hash
  • Loading branch information
Aakash1103Jha committed Oct 30, 2022
2 parents 14f32d5 + c20dab8 commit 95dced4
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@thedevdesigner/helper-modules",
"version": "1.0.5",
"version": "1.0.6",
"private": false,
"description": "Collection of commonly used helper modules written in TypeScript",
"main": "./dist/index.js",
Expand Down Expand Up @@ -54,4 +54,4 @@
"dotenv": "^16.0.3",
"typescript": "^4.8.4"
}
}
}
27 changes: 27 additions & 0 deletions src/helpers/Security/generatePasswordHash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { randomBytes, pbkdf2 } from "crypto";
import { promisify } from "util";

/**
* It takes a raw password, a complexity, and an iteration count, and returns a hashed password
* @param {string} rawPassword - The password to hash
* @param {number} [complexity=15] - The length of the salt.
* @param [iterations=1000] - The number of iterations to use when generating the hash. The higher the
* number, the more secure the hash, but the longer it will take to generate.
* @returns the result of the pbkdf2 function.
*/
const generatePasswordHash = async (rawPassword: string, complexity: number = 15, iterations = 1000) => {
if (!rawPassword || !rawPassword.length) return "rawPassword is a required argument";
if (complexity <= 0) return "Complexity should be greater than zero";
if (iterations <= 0) return "Iterations should be greater than zero";
const _randomBytes = promisify(randomBytes);
const _pbkdf2 = promisify(pbkdf2);
try {
const _salt = await _randomBytes(complexity);
const _hash = await _pbkdf2(rawPassword, _salt, iterations, 64, "sha512");
return _hash.toString("hex");
} catch (error) {
throw error as Error;
}
};

export default generatePasswordHash;
1 change: 1 addition & 0 deletions src/helpers/Security/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as encryptData } from "./encryptData";
export { default as decryptData } from "./decryptData";
export { default as generatePasswordHash } from "./generatePasswordHash";
25 changes: 25 additions & 0 deletions src/tests/generatePasswordHash.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { generatePasswordHash } from "../helpers/Security";

enum PASSWORD {
VALID = "NewUser@123",
EMPTY = "",
}

describe("tests function to generate password hash", () => {
test("should return hash password string", async () => {
const _result = await generatePasswordHash(PASSWORD.VALID);
expect(_result).toBeDefined();
});
test("should return error message if password is empty string", async () => {
const _result = await generatePasswordHash(PASSWORD.EMPTY);
expect(_result).toEqual("rawPassword is a required argument");
});
test("should return error message for negative complexity value", async () => {
const _result = await generatePasswordHash(PASSWORD.VALID, -50);
expect(_result).toEqual("Complexity should be greater than zero");
});
test("should return error message for negative iteration value", async () => {
const _result = await generatePasswordHash(PASSWORD.VALID, 100000, -10);
expect(_result).toEqual("Iterations should be greater than zero");
});
});

0 comments on commit 95dced4

Please sign in to comment.