t00-multiuser/server/utilities/PasswordUtility.ts

36 lines
1 KiB
TypeScript
Raw Normal View History

2024-04-22 02:01:14 +01:00
import { pbkdf2, randomBytes } from "crypto";
import Config from "../objects/Config";
export default abstract class PasswordUtility {
public static ValidatePassword(hash:string, salt:string, password:string) {
return new Promise<boolean>((resolve, reject) => {
pbkdf2(password, salt, Config.database.pbkdf2.itterations, Config.database.pbkdf2.keylength, "sha512", (err, derivedKey) => {
if (err) {
return reject(err);
} else {
if (derivedKey.toString("hex") !== hash) {
return resolve(false);
}
return resolve(true);
}
});
});
}
public static HashPassword(salt:string, password:string) {
return new Promise<string>((resolve, reject) => {
pbkdf2(password, salt, Config.database.pbkdf2.itterations, Config.database.pbkdf2.keylength, "sha512", (err, derivedKey) => {
if (err) {
return reject(err);
} else {
return resolve(derivedKey.toString("hex"));
}
});
});
}
public static GenerateSalt() {
return randomBytes(Config.database.pbkdf2.keylength).toString("hex");
}
}