Skip to content

Commit

Permalink
Merge pull request #20 from kourosh-alasti/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
kourosh-alasti authored Nov 18, 2024
2 parents 06d9a78 + 17910de commit 99e55c1
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 294 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
/node_modules

# Git Config
/.git
/.git

# Outdated Test
/tests/update
4 changes: 2 additions & 2 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.github
node_modules
src
test
tests

CODE_OF_CONDUCT.md
CONTRIBUTING.md
Expand All @@ -12,4 +12,4 @@ SECURITY.md
.npmignore
package-lock.json
tsconfig.json
tsup.config.ts
tsup.config.ts
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "passjen",
"version": "2.0.1",
"version": "2.1.0",
"description": "Lightweight password generator and hasher.",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
9 changes: 2 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import Generator, {
GenerateOptions,
GeneratedResults,
} from "./models/generator";
import Hasher from "./models/hasher";

import Hasher, { HashedResults } from "./models/hasher";

export { Generator, GenerateOptions, GeneratedResults, Hasher, HashedResults };
export { Hasher };
4 changes: 2 additions & 2 deletions src/models/hasher.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import crypto from "crypto";
import crypto from "node:crypto";
import Generator, { GeneratedResults, GenerateOptions } from "./generator";

export interface HashedResults {
interface HashedResults {
hashedPassword: string;
password: string;
salt: string;
Expand Down
158 changes: 88 additions & 70 deletions tests/compare-hash.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,121 +4,139 @@ import { Hasher } from "../src";
describe("Compare Hashed Password Suite", () => {
test("Compare hashed password synchronously and matches", () => {
const pwd = "1?R2M+z[t)$bTY1";
const hash = "$2b$10$55ZPMGhYysoCA60s0fAZXu0IwLUrHh/wWdzgK4ezK.TCv1CoLxuO2";
const hash =
"e14eb219a49cffab80586eb7ed224f894eddbca5414a43dc215b4d73da0aea6df4bcc9a3e2d804fed6604105d39c81f74b507175ef6a6e3e35fa779ac5e6bc95";
const salt = "27a617e3e0116b82ca8f6079ec982278";

const { password, hashedPassword, isMatch } =
Hasher.compareHashedPasswordSync({
password: pwd,
hashedPassword: hash,
});
const isMatch = Hasher.compareSync({
password: pwd,
hashedPassword: hash,
salt,
saltRounds: 10,
});

expect(password).toBe(pwd);
expect(hashedPassword).toBe(hash);
expect(isMatch).toBe(true);
});

test("Compare hashed password synchronously and does not match", () => {
const pwd = "1?R2M+z[t)$bTsdjfl;";
const hash = "$2b$10$55ZPMGhYysoCA60s0fAZXu0IwLUrHh/wWdzgK4ezK.TCv1CoLxuO2";
const inCorrectHash =
"2068cbdc38cee45b8b856e0771f93589fa3e5cdb87269154b227d640eabf1e536c429be2b99f5aaa63f52e5143a96e67106e3f488efa03149089e98499cbf45";
// const correctHash =
// "2068cbdc38cee45b8b856e0771f93589fa3e5cdb87269154b227d640eabf1e536c429be2b99f5aaa63f52e5143a96e67106e3f488efa03149089e98499cbf451";
const salt = "b0b29d3f6a697a72363de7296eccab10";

const isMatch = Hasher.compareSync({
password: pwd,
hashedPassword: inCorrectHash,
salt,
saltRounds: 10,
});

const { password, hashedPassword, isMatch } =
Hasher.compareHashedPasswordSync({
password: pwd,
hashedPassword: hash,
});

expect(password).toBe(pwd);
expect(hashedPassword).toBe(hash);
expect(isMatch).toBe(false);
});

test("Compare hashed password with non default saltRounds (14 instead of 10) synchronously and matches", () => {
const pwd = "yyG}^ScYzT)8,a<";
const hash = "$2b$14$0W1uEwiWhC/fyQ6lcKUwyesv4uQ5yEUJRqn3JPsbYzu5g9xR1DYza";

const { password, hashedPassword, isMatch } =
Hasher.compareHashedPasswordSync({
password: pwd,
hashedPassword: hash,
});
const hash =
"06a4dcc8fac0672b407b649525d012de57631eb41fe4affe4ebbc860bc1abb8cc3f69cb6275f2cb8f506302879ac24e419752f7ffff0fdd4abb156ed6498ac3a";
const salt = "062ebb2c5358c17744f126e0ab6d1d59";
const saltRounds = 14;

const isMatch = Hasher.compareSync({
password: pwd,
hashedPassword: hash,
salt,
saltRounds,
});

expect(password).toBe(pwd);
expect(hashedPassword).toBe(hash);
expect(isMatch).toBe(true);
});

test("Compare hashed password with non default saltRounds (14 instead of 10) synchronously and does not match", () => {
const pwd = "yyG}^ScYzT)8,a<sdfsdf";
const hash = "$2b$14$0W1uEwiWhC/fyQ6lcKUwyesv4uQ5yEUJRqn3JPsbYzu5g9xR1DYza";

const { password, hashedPassword, isMatch } =
Hasher.compareHashedPasswordSync({
password: pwd,
hashedPassword: hash,
});
const inCorrectHash =
"1460e0f60a5f5d6d9aef75c4084cf8b49118646783fbf571b3645d379717ea8a1e4891a0eb4cd2bd3bd4435a8bcdbf194e5638155dc9cdea585a556a7178b9d";
// const correctHash =
// "1460e0f60a5f5d6d9aef75c4084cf8b49118646783fbf571b3645d379717ea8a1e4891a0eb4cd2bd3bd4435a8bcdbf194e5638155dc9cdea585a556a71718b9d";
const saltRounds = 14;
const salt = "10c5f9e7ad4ecc6cad91d73552b20df3";

const isMatch = Hasher.compareSync({
password: pwd,
hashedPassword: inCorrectHash,
salt,
saltRounds,
});

expect(password).toBe(pwd);
expect(hashedPassword).toBe(hash);
expect(isMatch).toBe(false);
});

test("Compare hashed password asynchronously and matches", async () => {
const pwd = "1?R2M+z[t)$bTY1";
const hash = "$2b$10$55ZPMGhYysoCA60s0fAZXu0IwLUrHh/wWdzgK4ezK.TCv1CoLxuO2";
const hash =
"b8f114e80baa8e1b92870cc14bb1c9dc3a22dacd03595acb133f9b8a6e10b25bd53ad338ac3a62c082ceb85b03ba67eedf87ff9d1514d4cb73d7322c9645f16d";
const salt = "8516a4a09bf67d444b7c38dd4307734a";

const { password, hashedPassword, isMatch } =
await Hasher.compareHashedPassword({
password: pwd,
hashedPassword: hash,
});
const isMatch = await Hasher.compare({
password: pwd,
hashedPassword: hash,
salt,
});

expect(password).toBe(pwd);
expect(hashedPassword).toBe(hash);
expect(isMatch).toBe(true);
});

test("Compare hashed password synchronously and does not match", async () => {
const pwd = "1?R2M+z[t)$bTsdjfl;";
const hash = "$2b$10$55ZPMGhYysoCA60s0fAZXu0IwLUrHh/wWdzgK4ezK.TCv1CoLxuO2";
// const correctHash =
// "4fde9dda1afa9f1ef95c2173f6c949c43bb22630f23e2622abe26c39b47f59ef8fee22bee792722f441f8791ed6e49188707cfee442e593aa44c65813c6654a7";
const inCorrectHash =
"4fde9dda1afa9f1ef95c2173f6c949c43bb22630f23e2622abe26c39b47f59ef8fee22bee792722f441f8791ed6e49188707cfee442e593aa44c65813c6654a";
const salt = "38de417b1643c4437a7e10778f6959ae";

const isMatch = await Hasher.compare({
password: pwd,
hashedPassword: inCorrectHash,
salt,
});

const { password, hashedPassword, isMatch } =
await Hasher.compareHashedPassword({
password: pwd,
hashedPassword: hash,
});

expect(password).toBe(pwd);
expect(hashedPassword).toBe(hash);
expect(isMatch).toBe(false);
});

test("Compare hashed password with non default saltRounds (14 instead of 10) synchronously and matches", async () => {
const pwd = "yyG}^ScYzT)8,a<";
const hash = "$2b$14$0W1uEwiWhC/fyQ6lcKUwyesv4uQ5yEUJRqn3JPsbYzu5g9xR1DYza";

const { password, hashedPassword, isMatch } =
await Hasher.compareHashedPassword({
password: pwd,
hashedPassword: hash,
});
const hash =
"ab10a0907d894f040de4525f3a4ad582ad25b7fc07048583fde69d84ee325217874a9e96068dd30e3f852fc287dfcf65bbe504d09fe1e063a4405f8ac47e1819";
const salt = "9d0416c3b2773334863de2ff592d3e24";
const saltRounds = 14;

const isMatch = await Hasher.compare({
password: pwd,
hashedPassword: hash,
salt,
saltRounds,
});

expect(password).toBe(pwd);
expect(hashedPassword).toBe(hash);
expect(isMatch).toBe(true);
});

test("Compare hashed password with non default saltRounds (14 instead of 10) synchronously and does not match", async () => {
const pwd = "yyG}^ScYzT)8,a<sdfsdf";
const hash = "$2b$14$0W1uEwiWhC/fyQ6lcKUwyesv4uQ5yEUJRqn3JPsbYzu5g9xR1DYza";

const { password, hashedPassword, isMatch } =
await Hasher.compareHashedPassword({
password: pwd,
hashedPassword: hash,
});
// const correctHash =
// "61b678cb6430b4f1cab3db46dfc1b1d2aef8e42eeb3f457bdbd27c24eb8f995d8d63b848b28720e40134a4eacdd751ac69e5c96d655a701228e34403201c5a56";
const inCorrectHash =
"61b678cb6430b4f1cab3db46dfc1b1d2aef8e42eeb3f457bdbd27c24eb8f995d8d63b848b28720e40134a4eacdd751ac69e5c96d655a701228e344032015a56";
const saltRounds = 14;
const salt = "28feda6d29480680e9c9dc8d1562f7da";

const isMatch = await Hasher.compare({
password: pwd,
hashedPassword: inCorrectHash,
salt,
saltRounds,
});

expect(password).toBe(pwd);
expect(hashedPassword).toBe(hash);
expect(isMatch).toBe(false);
});
});
49 changes: 0 additions & 49 deletions tests/generate-hash.test.js

This file was deleted.

Loading

0 comments on commit 99e55c1

Please sign in to comment.