UNPKG

@mathiscode/password-leak

Version:

A library to check for compromised passwords

3 lines (2 loc) 1.91 kB
(()=>{function g(e){if(typeof e!="string")throw new TypeError("Expected a string");return e.replace(/[|\\{}()[\]^$+*?.]/g,"\\$&").replace(/-/g,"\\x2d")}var l=[{id:0,value:"Too weak",minDiversity:0,minLength:0},{id:1,value:"Weak",minDiversity:2,minLength:8},{id:2,value:"Medium",minDiversity:4,minLength:10},{id:3,value:"Strong",minDiversity:4,minLength:12}];var d=(e,s=l,n)=>{s[0].minDiversity=0,s[0].minLength=0;let i=e??"",a=[{key:"lowercase",regex:"[a-z]"},{key:"uppercase",regex:"[A-Z]"},{key:"number",regex:"[0-9]"},{key:"symbol",regex:n?`[${g(n)}]`:"[^a-zA-Z0-9]"}],t={};t.contains=a.filter(r=>new RegExp(`${r.regex}`).test(i)).map(r=>r.key),t.length=i.length;let o=s.filter(r=>t.contains.length>=r.minDiversity).filter(r=>t.length>=r.minLength).sort((r,h)=>h.id-r.id).map(r=>({id:r.id,value:r.value}));return Object.assign(t,o[0]),t};var c=globalThis.crypto,v=p;globalThis.isPasswordLeaked=p;globalThis.checkPassword=u;globalThis.checkPasswordStrength=f;async function u(e){return{strength:d(e),isLeaked:await p(e)}}function f(e){return d(e)}async function p(e){if(typeof e!="string")throw new Error("Password must be a string");let s=await c.subtle.digest("SHA-1",new TextEncoder().encode(e)),n=Array.from(new Uint8Array(s)).map(o=>o.toString(16).padStart(2,"0")).join(""),i=n.substring(0,5).toUpperCase();return(await(await fetch(`https://api.pwnedpasswords.com/range/${i}`)).text()).includes(n.substring(5).toUpperCase())}function S(e,s){if(typeof e!="string"){s(new Error("Password must be a string"),!1);return}c.subtle.digest("SHA-1",new TextEncoder().encode(e)).catch(n=>s(n,!1)).then(n=>{let i=Array.from(new Uint8Array(n)).map(t=>t.toString(16).padStart(2,"0")).join(""),a=i.substring(0,5).toUpperCase();fetch(`https://api.pwnedpasswords.com/range/${a}`).then(t=>t.text()).then(t=>s(null,t.includes(i.substring(5).toUpperCase()))).catch(t=>s(t,!1))})}})(); //# sourceMappingURL=index.js.map