@hixbe/otp-pass-kit
Version:
A secure, cryptographically-strong OTP and password generator for Node.js with CLI support. Generate customizable one-time passwords and strong passwords with TypeScript support.
1 lines • 1.26 kB
JavaScript
import{randomInt as e}from"crypto";var t="0123456789",r="abcdefghijklmnopqrstuvwxyz",s="ABCDEFGHIJKLMNOPQRSTUVWXYZ",n="!@#$%^&*()_+-=[]{}|;:,.<>?";function a(e){if(!Number.isInteger(e)||e<=0)throw new Error("Length must be a positive integer");if(e>1e3)throw new Error("Length cannot exceed 1000 characters")}function o(e){let a="";if(e.digits&&(a+=t),e.lowerCaseAlphabets&&(a+=r),e.upperCaseAlphabets&&(a+=s),e.specialChars&&(a+=n),!a)throw new Error("At least one character set must be enabled");return a}function h(a,o,h){const l=new Array(a);if(h?.ensureEachType&&a>=4){const p=[];h.digits&&p.push(t),h.lowerCaseAlphabets&&p.push(r),h.upperCaseAlphabets&&p.push(s),h.specialChars&&p.push(n);for(let t=0;t<p.length&&t<a;t++){const r=p[t];l[t]=r[e(0,r.length)]}for(let t=p.length;t<a;t++){const r=e(0,o.length);l[t]=o[r]}for(let t=a-1;t>0;t--){const r=e(0,t+1);[l[t],l[r]]=[l[r],l[t]]}}else for(let t=0;t<a;t++){const r=e(0,o.length);l[t]=o[r]}return l.join("")}function l(e=6,t){a(e);const r={lowerCaseAlphabets:!1,upperCaseAlphabets:!1,specialChars:!1,digits:!0,...t};return h(e,o(r),r)}function p(e=8,t){a(e);const r={lowerCaseAlphabets:!0,upperCaseAlphabets:!0,specialChars:!0,digits:!0,...t};return h(e,o(r),r)}export{l as generateOtp,p as generatePass};