forked from Deen-Bridge/dnb-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathotp.js
More file actions
30 lines (27 loc) · 776 Bytes
/
Copy pathotp.js
File metadata and controls
30 lines (27 loc) · 776 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import crypto from "crypto";
import bcrypt from "bcrypt";
/**
* Generate a cryptographically secure 6-digit numeric OTP.
* @returns {string} 6-digit OTP string
*/
export const generateOtp = () => {
return crypto.randomInt(100000, 1000000).toString();
};
/**
* Hash an OTP using bcrypt.
* @param {string} otp
* @returns {Promise<string>} Hashed OTP string
*/
export const hashOtp = async (otp) => {
return await bcrypt.hash(otp, 12);
};
/**
* Verify a plaintext OTP against a hashed OTP using bcrypt.
* @param {string} otp
* @param {string} hashedOtp
* @returns {Promise<boolean>} True if valid, false otherwise
*/
export const verifyOtp = async (otp, hashedOtp) => {
if (!otp || !hashedOtp) return false;
return await bcrypt.compare(otp, hashedOtp);
};