@jvddavid/authenticator
Version:
Biblioteca para geração e verificação de códigos TOTP (Google Authenticator, Microsoft Authenticator, etc.) em Node.js.
2 lines (1 loc) • 1.78 kB
JavaScript
var d=Object.defineProperty;var n=(e,o)=>d(e,"name",{value:o,configurable:!0});import g from"crypto";import f from"notp";const p={encode:n(e=>{const o="ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";let t="",s="",r;typeof e=="string"?r=Array.from(Buffer.from(e,"utf8")):r=Array.from(e);for(const c of r)t+=c.toString(2).padStart(8,"0");for(;t.length>=5;){const c=t.slice(0,5);t=t.slice(5);const i=parseInt(c,2);s+=o[i]}return t.length>0&&(s+=o[parseInt(t.padEnd(5,"0"),2)]),Buffer.from(s,"utf8")},"encode"),decode:n(e=>{const o="ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";let t="",s="",r;typeof e=="string"?r=e.split(""):r=Array.from(e.toString("utf8"));for(const c of r){const i=c.toUpperCase(),a=o.indexOf(i);a!==-1&&(t+=a.toString(2).padStart(5,"0"))}for(;t.length>=8;){const c=t.slice(0,8);t=t.slice(8),s+=String.fromCharCode(parseInt(c,2))}return s},"decode")};function y(){return g.randomBytes(20)}n(y,"generateOtpKey");function h(e){return p.encode(e).toString("utf8").replace(/=/g,"").toLowerCase().replace(/(\w{4})/g,"$1 ").trim()}n(h,"encodeGoogleAuthKey");function m(){return h(y())}n(m,"generateGoogleAuthKey");function u(e){const o=e.replace(/\W+/g,"").toUpperCase();return p.decode(o)}n(u,"decodeGoogleAuthKey");function A(e){const o=u(e);return f.totp.gen(o)}n(A,"generateGoogleAuthToken");function b(e,o){const t=u(e);return o=o.replace(/\W+/g,""),f.totp.verify(o,t,{window:1,time:30})}n(b,"verifyGoogleAuthToken");function C(e,o="",t="",s="SHA1",r=6,c=30){const i=encodeURIComponent(o),a=encodeURIComponent(t),l=e.replace(/[\s._-]+/g,"").toUpperCase();return`otpauth://totp/${a}:${i}?secret=${l}&issuer=${a}&algorithm=${s}&digits=${r}&period=${c}`}n(C,"generateTotpUri");export{m as generateGoogleAuthKey,A as generateGoogleAuthToken,C as generateTotpUri,b as verifyGoogleAuthToken};