From 18c20c52d545a8cb91a5bef182bc265b0dd91617 Mon Sep 17 00:00:00 2001 From: Mathis Date: Tue, 21 May 2024 14:02:21 +0200 Subject: [PATCH] feat(authentication): create credentials service A new credentials service has been created with two main functionalities: hashing a password and comparing an original password with a hash. These methods use the Argon2id hashing function and are intended to be part of the authentication process. --- .../authentication/credentials.service.ts | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/services/authentication/credentials.service.ts diff --git a/src/services/authentication/credentials.service.ts b/src/services/authentication/credentials.service.ts new file mode 100644 index 0000000..bcab534 --- /dev/null +++ b/src/services/authentication/credentials.service.ts @@ -0,0 +1,24 @@ +import Argon2id from "@node-rs/argon2"; + +//ToTest +export async function getHashFromPassword(password: string) { + return await Argon2id.hash(password, { + secret: Buffer.from(`${process.env["HASH_SECRET"]}`), + algorithm: 2, + }); +} + +//ToTest +export async function comparePassword(password: string, hash: string) { + return await Argon2id.verify(hash, password, { + secret: Buffer.from(`${process.env["HASH_SECRET"]}`), + algorithm: 2, + }); +} + +const CredentialService = { + compare: comparePassword, + hash: getHashFromPassword, +}; + +export default CredentialService;