universal_authentication
Version:
Seamless and Secure Authentication for Modern Web Applications: Easily integrate OTP-based email verification, Google OAuth, GitHub, Microsoft, and Okta login into your Node.js app. Modular, flexible, and database-agnostic, this package simplifies user au
43 lines (40 loc) • 1.33 kB
text/typescript
import crypto from "crypto";
import bcrypt from "bcrypt";
//function to hash the password using bcrypt or crypto
export const hashPassword = async (
password: string,
secureKey: string,
algorithm: "crypto" | "bcrypt"
) => {
if (algorithm === "bcrypt") {
const salt = bcrypt.genSaltSync(10);
return await bcrypt.hash(password + secureKey, salt);
} else if (algorithm === "crypto") {
const hash = crypto.createHash("sha512");
hash.update(password + secureKey);
return hash.digest("hex");
} else {
throw new Error(
"The algorithm you entered is not supported, do use of bcrypt or crypto in your program."
);
}
};
// function to compare the hashed password with the provided password
export const verifyPassword = async (
password: string,
secureKey: string,
hashPassword: string,
algorithm: "crypto" | "bcrypt"
) => {
if (algorithm === "bcrypt") {
return await bcrypt.compare(password + secureKey, hashPassword);
} else if (algorithm === "crypto") {
const hash = crypto.createHash("sha512");
hash.update(password + secureKey);
return hash.digest("hex") === hashPassword;
} else {
throw new Error(
"The algorithm you entered is not supported, do use of bcrypt or crypto in your program."
);
}
};