forked from Yara724/api
17 lines
467 B
TypeScript
17 lines
467 B
TypeScript
import { Injectable } from "@nestjs/common";
|
|
const crypto = require("node:crypto");
|
|
|
|
@Injectable()
|
|
export class OtpGeneratorService {
|
|
createDigit(digits: number): string {
|
|
const max = Math.pow(10, digits);
|
|
const randomBytes = crypto.randomBytes(Math.ceil(digits / 2));
|
|
const randomNumber = parseInt(randomBytes.toString("hex"), 16) % max;
|
|
return randomNumber.toString().padStart(digits, "0");
|
|
}
|
|
|
|
create() {
|
|
return this.createDigit(5);
|
|
}
|
|
}
|