-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from the-devdesigner/development
v1.0.6 - added new function to generate password hash
- Loading branch information
Showing
5 changed files
with
57 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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; |
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,2 +1,3 @@ | ||
export { default as encryptData } from "./encryptData"; | ||
export { default as decryptData } from "./decryptData"; | ||
export { default as generatePasswordHash } from "./generatePasswordHash"; |
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 |
---|---|---|
@@ -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"); | ||
}); | ||
}); |