@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.8 kB
JavaScript
;var g=Object.defineProperty;var n=(e,o)=>g(e,"name",{value:o,configurable:!0});var d=require("crypto"),u=require("notp");const f={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 h(){return d.randomBytes(20)}n(h,"generateOtpKey");function y(e){return f.encode(e).toString("utf8").replace(/=/g,"").toLowerCase().replace(/(\w{4})/g,"$1 ").trim()}n(y,"encodeGoogleAuthKey");function m(){return y(h())}n(m,"generateGoogleAuthKey");function l(e){const o=e.replace(/\W+/g,"").toUpperCase();return f.decode(o)}n(l,"decodeGoogleAuthKey");function A(e){const o=l(e);return u.totp.gen(o)}n(A,"generateGoogleAuthToken");function b(e,o){const t=l(e);return o=o.replace(/\W+/g,""),u.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),p=e.replace(/[\s._-]+/g,"").toUpperCase();return`otpauth://totp/${a}:${i}?secret=${p}&issuer=${a}&algorithm=${s}&digits=${r}&period=${c}`}n(C,"generateTotpUri"),exports.generateGoogleAuthKey=m,exports.generateGoogleAuthToken=A,exports.generateTotpUri=C,exports.verifyGoogleAuthToken=b;