diff options
Diffstat (limited to 'app/lib/otp.js')
-rw-r--r-- | app/lib/otp.js | 8 |
1 files changed, 3 insertions, 5 deletions
diff --git a/app/lib/otp.js b/app/lib/otp.js index 751edf4..14763a8 100644 --- a/app/lib/otp.js +++ b/app/lib/otp.js @@ -1,6 +1,4 @@ -"use strict"; - -const { createHmac } = require("crypto"); +import { createHmac } from "node:crypto"; const getHMAC = (k,c) => { const h = createHmac("sha1", k); h.update(c, "hex"); return h.digest("hex"); } @@ -12,7 +10,7 @@ const b32u = s => Uint8Array.from(s.split("").reduce((a,c) => a+b32a.indexOf(c.toUpperCase()).toString(2).padStart(5,0), "") .match(/.{8}/g).map(b => parseInt(b,2))); -module.exports = function totp(secret, { expiry=30, +export default function totp(secret, { expiry=30, now=Math.round(new Date().getTime()/1000), length=6 }={}) { const time = d2h(now/expiry).padStart(16,0); const hmac = getHMAC(b32u(secret), time); @@ -21,7 +19,7 @@ module.exports = function totp(secret, { expiry=30, return otp.padStart(length,0).substr(-length); } -module.exports.check = function check(token, secret, { expiry=30, +export function check(token, secret, { expiry=30, now=Math.round(new Date().getTime()/1000), length=6, window=0 }={}) { const i = Array(window*2+1).fill().map((e,i) => now+(expiry*(i-window))) .findIndex(n => token === this(secret, {now:n, expiry, length})); |