UNPKG

@mathiscode/password-leak

Version:

A library to check for compromised passwords

3 lines (2 loc) 3.04 kB
var S=Object.create;var d=Object.defineProperty;var b=Object.getOwnPropertyDescriptor;var k=Object.getOwnPropertyNames;var P=Object.getPrototypeOf,L=Object.prototype.hasOwnProperty;var A=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),E=(e,t)=>{for(var r in t)d(e,r,{get:t[r],enumerable:!0})},u=(e,t,r,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of k(t))!L.call(e,a)&&a!==r&&d(e,a,{get:()=>t[a],enumerable:!(i=b(t,a))||i.enumerable});return e};var T=(e,t,r)=>(r=e!=null?S(P(e)):{},u(t||!e||!e.__esModule?d(r,"default",{value:e,enumerable:!0}):r,e)),$=e=>u(d({},"__esModule",{value:!0}),e);var f=A(o=>{"use strict";Object.defineProperty(o,"__esModule",{value:!0});function D(e){if(typeof e!="string")throw new TypeError("Expected a string");return e.replace(/[|\\{}()[\]^$+*?.]/g,"\\$&").replace(/-/g,"\\x2d")}var p=[{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}],h="!\"#$%&'()*+,-./\\:;<=>?@[]^_`{|}~",w=(e,t=p,r)=>{t[0].minDiversity=0,t[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:r?`[${D(r)}]`:"[^a-zA-Z0-9]"}],s={};s.contains=a.filter(n=>new RegExp(`${n.regex}`).test(i)).map(n=>n.key),s.length=i.length;let c=t.filter(n=>s.contains.length>=n.minDiversity).filter(n=>s.length>=n.minLength).sort((n,v)=>v.id-n.id).map(n=>({id:n.id,value:n.value}));return Object.assign(s,c[0]),s},U={passwordStrength:w,defaultOptions:p,owaspSymbols:h};o.default=U;o.defaultOptions=p;o.owaspSymbols=h;o.passwordStrength=w});var C={};E(C,{checkPassword:()=>y,checkPasswordStrength:()=>x,default:()=>O,isPasswordLeaked:()=>g,isPasswordLeakedSync:()=>j});module.exports=$(C);var l=T(f(),1),m=globalThis.crypto,O=g;globalThis.isPasswordLeaked=g;globalThis.checkPassword=y;globalThis.checkPasswordStrength=x;async function y(e){return{strength:(0,l.passwordStrength)(e),isLeaked:await g(e)}}function x(e){return(0,l.passwordStrength)(e)}async function g(e){if(typeof e!="string")throw new Error("Password must be a string");let t=await m.subtle.digest("SHA-1",new TextEncoder().encode(e)),r=Array.from(new Uint8Array(t)).map(c=>c.toString(16).padStart(2,"0")).join(""),i=r.substring(0,5).toUpperCase();return(await(await fetch(`https://api.pwnedpasswords.com/range/${i}`)).text()).includes(r.substring(5).toUpperCase())}function j(e,t){if(typeof e!="string"){t(new Error("Password must be a string"),!1);return}m.subtle.digest("SHA-1",new TextEncoder().encode(e)).catch(r=>t(r,!1)).then(r=>{let i=Array.from(new Uint8Array(r)).map(s=>s.toString(16).padStart(2,"0")).join(""),a=i.substring(0,5).toUpperCase();fetch(`https://api.pwnedpasswords.com/range/${a}`).then(s=>s.text()).then(s=>t(null,s.includes(i.substring(5).toUpperCase()))).catch(s=>t(s,!1))})}0&&(module.exports={checkPassword,checkPasswordStrength,isPasswordLeaked,isPasswordLeakedSync}); //# sourceMappingURL=index.cjs.map