UNPKG

@guardhivefraudshield/device-fingerprint

Version:

device-fingerprint client side device fingerprint module

3 lines (2 loc) 16.7 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).DeviceFingerprint={})}(this,(function(e){"use strict";let t={exclude:[]};const n={},r={timeout:"true"},o=(e,t)=>{"undefined"!=typeof window&&(n[e]=t)};function a(e){return e^=e>>>16,e=Math.imul(e,2246822507),e^=e>>>13,e=Math.imul(e,3266489909),(e^=e>>>16)>>>0}const i=new Uint32Array([597399067,2869860233,951274213,2716044179]);function s(e,t){return e<<t|e>>>32-t}function c(e,t=0){var n;if(t=t?0|t:0,"string"==typeof e&&(n=e,e=(new TextEncoder).encode(n).buffer),!(e instanceof ArrayBuffer))throw new TypeError("Expected key to be ArrayBuffer or string");const r=new Uint32Array([t,t,t,t]);!function(e,t){const n=e.byteLength/16|0,r=new Uint32Array(e,0,4*n);for(let e=0;e<n;e++){const n=r.subarray(4*e,4*(e+1));n[0]=Math.imul(n[0],i[0]),n[0]=s(n[0],15),n[0]=Math.imul(n[0],i[1]),t[0]=t[0]^n[0],t[0]=s(t[0],19),t[0]=t[0]+t[1],t[0]=Math.imul(t[0],5)+1444728091,n[1]=Math.imul(n[1],i[1]),n[1]=s(n[1],16),n[1]=Math.imul(n[1],i[2]),t[1]=t[1]^n[1],t[1]=s(t[1],17),t[1]=t[1]+t[2],t[1]=Math.imul(t[1],5)+197830471,n[2]=Math.imul(n[2],i[2]),n[2]=s(n[2],17),n[2]=Math.imul(n[2],i[3]),t[2]=t[2]^n[2],t[2]=s(t[2],15),t[2]=t[2]+t[3],t[2]=Math.imul(t[2],5)+2530024501,n[3]=Math.imul(n[3],i[3]),n[3]=s(n[3],18),n[3]=Math.imul(n[3],i[0]),t[3]=t[3]^n[3],t[3]=s(t[3],13),t[3]=t[3]+t[0],t[3]=Math.imul(t[3],5)+850148119}}(e,r),function(e,t){const n=e.byteLength/16|0,r=e.byteLength%16,o=new Uint32Array(4),a=new Uint8Array(e,16*n,r);switch(r){case 15:o[3]=o[3]^a[14]<<16;case 14:o[3]=o[3]^a[13]<<8;case 13:o[3]=o[3]^a[12],o[3]=Math.imul(o[3],i[3]),o[3]=s(o[3],18),o[3]=Math.imul(o[3],i[0]),t[3]=t[3]^o[3];case 12:o[2]=o[2]^a[11]<<24;case 11:o[2]=o[2]^a[10]<<16;case 10:o[2]=o[2]^a[9]<<8;case 9:o[2]=o[2]^a[8],o[2]=Math.imul(o[2],i[2]),o[2]=s(o[2],17),o[2]=Math.imul(o[2],i[3]),t[2]=t[2]^o[2];case 8:o[1]=o[1]^a[7]<<24;case 7:o[1]=o[1]^a[6]<<16;case 6:o[1]=o[1]^a[5]<<8;case 5:o[1]=o[1]^a[4],o[1]=Math.imul(o[1],i[1]),o[1]=s(o[1],16),o[1]=Math.imul(o[1],i[2]),t[1]=t[1]^o[1];case 4:o[0]=o[0]^a[3]<<24;case 3:o[0]=o[0]^a[2]<<16;case 2:o[0]=o[0]^a[1]<<8;case 1:o[0]=o[0]^a[0],o[0]=Math.imul(o[0],i[0]),o[0]=s(o[0],15),o[0]=Math.imul(o[0],i[1]),t[0]=t[0]^o[0]}}(e,r),function(e,t){t[0]=t[0]^e.byteLength,t[1]=t[1]^e.byteLength,t[2]=t[2]^e.byteLength,t[3]=t[3]^e.byteLength,t[0]=t[0]+t[1]|0,t[0]=t[0]+t[2]|0,t[0]=t[0]+t[3]|0,t[1]=t[1]+t[0]|0,t[2]=t[2]+t[0]|0,t[3]=t[3]+t[0]|0,t[0]=a(t[0]),t[1]=a(t[1]),t[2]=a(t[2]),t[3]=a(t[3]),t[0]=t[0]+t[1]|0,t[0]=t[0]+t[2]|0,t[0]=t[0]+t[3]|0,t[1]=t[1]+t[0]|0,t[2]=t[2]+t[0]|0,t[3]=t[3]+t[0]|0}(e,r);const o=new Uint8Array(r.buffer);return Array.from(o).map((e=>e.toString(16).padStart(2,"0"))).join("")}async function u(){try{const e=Object.fromEntries(Object.entries(n).filter((([e])=>{var n;return!(null===(n=null==t?void 0:t.exclude)||void 0===n?void 0:n.includes(e))})).map((([e,t])=>[e,t()]))),o=Object.keys(e),a=Object.values(e),i=await function(e,t,n){return Promise.all(e.map((e=>{return Promise.race([e,(r=t,o=n,new Promise((e=>{setTimeout((()=>e(o)),r)})))]);var r,o})))}(a,(null==t?void 0:t.timeout)||1e3,r),s=i.filter((e=>void 0!==e)),c={};return s.forEach(((e,t)=>{c[o[t]]=e})),c.deviceName=function(){const e=navigator.userAgent.toLowerCase();return/android/.test(e)?"Android Device":/iphone|ipad|ipod/.test(e)?"iOS Device":/macintosh|mac os x/.test(e)?"Mac OS Device":/windows/.test(e)?"Windows PC":"Unknown Device"}(),c.resolutions={screen:{width:window.screen.width*window.devicePixelRatio,height:window.screen.height*window.devicePixelRatio},window:{width:window.innerWidth,height:window.innerHeight}},delete c.math,l(c,["math"])}catch(e){throw e}}function l(e,t){const n={};for(const r in e)if(e.hasOwnProperty(r)){const o=e[r];if("object"!=typeof o||Array.isArray(o))t.includes(r)||(n[r]=o);else{const e=l(o,t.map((e=>e.startsWith(r+".")?e.slice(r.length+1):e)));Object.keys(e).length>0&&(n[r]=e)}}return n}function d(e){let t=0;for(let n=0;n<e.length;++n)t+=Math.abs(e[n]);return t}function h(e,t,n){let r=[];for(let t=0;t<e[0].data.length;t++){let n=[];for(let r=0;r<e.length;r++)n.push(e[r].data[t]);r.push(m(n))}const o=new Uint8ClampedArray(r);return new ImageData(o,t,n)}function m(e){if(0===e.length)return 0;const t={};for(const n of e)t[n]=(t[n]||0)+1;let n=e[0];for(const e in t)t[e]>t[n]&&(n=parseInt(e,10));return n}function f(){if("undefined"==typeof navigator)return{name:"unknown",version:"unknown"};const e=navigator.userAgent,t=[/(?<name>Edge|Edg)\/(?<version>\d+(?:\.\d+)?)/,/(?<name>(?:Chrome|Chromium|OPR|Opera|Vivaldi|Brave))\/(?<version>\d+(?:\.\d+)?)/,/(?<name>(?:Firefox|Waterfox|Iceweasel|IceCat))\/(?<version>\d+(?:\.\d+)?)/,/(?<name>Safari)\/(?<version>\d+(?:\.\d+)?)/,/(?<name>MSIE|Trident|IEMobile).+?(?<version>\d+(?:\.\d+)?)/,/(?<name>[A-Za-z]+)\/(?<version>\d+(?:\.\d+)?)/,/(?<name>SamsungBrowser)\/(?<version>\d+(?:\.\d+)?)/],n={Edg:"Edge",OPR:"Opera"};for(const r of t){const t=e.match(r);if(t&&t.groups){return{name:n[t.groups.name]||t.groups.name,version:t.groups.version}}}return{name:"unknown",version:"unknown"}}o("audio",(async function(){return new Promise(((e,t)=>{try{const t=44100,n=5e3,r=new(window.OfflineAudioContext||window.webkitOfflineAudioContext)(1,n,t),o=r.createBufferSource(),a=r.createOscillator();a.frequency.value=1e3;const i=r.createDynamicsCompressor();let s;i.threshold.value=-50,i.knee.value=40,i.ratio.value=12,i.attack.value=0,i.release.value=.2,a.connect(i),i.connect(r.destination),a.start(),r.oncomplete=t=>{s=t.renderedBuffer.getChannelData(0),e({sampleHash:d(s),oscillator:a.type,maxChannels:r.destination.maxChannelCount,channelCountMode:o.channelCountMode})},r.startRendering()}catch(e){console.error("Error creating audio fingerprint:",e),t(e)}}))}));const g="SamsungBrowser"!==f().name?1:3;function p(e,t){return new Promise((n=>setTimeout(n,e,t)))}"Firefox"!=f().name&&o("canvas",(function(){return document.createElement("canvas").getContext("2d"),new Promise((e=>{const t=Array.from({length:g},(()=>function(){const e=document.createElement("canvas"),t=e.getContext("2d");if(!t)return new ImageData(1,1);e.width=280,e.height=20;const n=t.createLinearGradient(0,0,e.width,e.height);n.addColorStop(0,"red"),n.addColorStop(1/6,"orange"),n.addColorStop(2/6,"yellow"),n.addColorStop(.5,"green"),n.addColorStop(4/6,"blue"),n.addColorStop(5/6,"indigo"),n.addColorStop(1,"violet"),t.fillStyle=n,t.fillRect(0,0,e.width,e.height);const r="Random Text WMwmil10Oo";return t.font="23.123px Arial",t.fillStyle="black",t.fillText(r,-5,15),t.fillStyle="rgba(0, 0, 255, 0.5)",t.fillText(r,-3.3,17.7),t.beginPath(),t.moveTo(0,0),t.lineTo(2*e.width/7,e.height),t.strokeStyle="white",t.lineWidth=2,t.stroke(),t.getImageData(0,0,e.width,e.height)}()));e({commonImageDataHash:c(h(t,280,20).data.toString()).toString()})}))}));const w=["Arial","Arial Black","Arial Narrow","Arial Rounded MT","Arimo","Archivo","Barlow","Bebas Neue","Bitter","Bookman","Calibri","Cabin","Candara","Century","Century Gothic","Comic Sans MS","Constantia","Courier","Courier New","Crimson Text","DM Mono","DM Sans","DM Serif Display","DM Serif Text","Dosis","Droid Sans","Exo","Fira Code","Fira Sans","Franklin Gothic Medium","Garamond","Geneva","Georgia","Gill Sans","Helvetica","Impact","Inconsolata","Indie Flower","Inter","Josefin Sans","Karla","Lato","Lexend","Lucida Bright","Lucida Console","Lucida Sans Unicode","Manrope","Merriweather","Merriweather Sans","Montserrat","Myriad","Noto Sans","Nunito","Nunito Sans","Open Sans","Optima","Orbitron","Oswald","Pacifico","Palatino","Perpetua","PT Sans","PT Serif","Poppins","Prompt","Public Sans","Quicksand","Rajdhani","Recursive","Roboto","Roboto Condensed","Rockwell","Rubik","Segoe Print","Segoe Script","Segoe UI","Sora","Source Sans Pro","Space Mono","Tahoma","Taviraj","Times","Times New Roman","Titillium Web","Trebuchet MS","Ubuntu","Varela Round","Verdana","Work Sans"],y=["monospace","sans-serif","serif"];function v(e,t){if(!e)throw new Error("Canvas context not supported");return e.font,e.font=`72px ${t}`,e.measureText("WwMmLli0Oo").width}function S(){var e;const t=document.createElement("canvas"),n=null!==(e=t.getContext("webgl"))&&void 0!==e?e:t.getContext("experimental-webgl");let r,o,a;if(n&&"getParameter"in n){try{r=n.getExtension("WEBGL_debug_renderer_info"),r&&(o=(n.getParameter(r.UNMASKED_VENDOR_WEBGL)||"").toString(),a=(n.getParameter(r.UNMASKED_RENDERER_WEBGL)||"").toString())}catch(e){}return{vendor:(n.getParameter(n.VENDOR)||"").toString(),vendorUnmasked:o,renderer:(n.getParameter(n.RENDERER)||"").toString(),rendererUnmasked:a,version:(n.getParameter(n.VERSION)||"").toString(),shadingLanguageVersion:(n.getParameter(n.SHADING_LANGUAGE_VERSION)||"").toString()}}return"undefined"}function b(){const e=new Float32Array(1),t=new Uint8Array(e.buffer);return e[0]=1/0,e[0]=e[0]-e[0],t[3]}function M(e,t){const n={};return t.forEach((t=>{const r=function(e){if(0===e.length)return null;const t={};e.forEach((e=>{const n=String(e);t[n]=(t[n]||0)+1}));let n=e[0],r=1;return Object.keys(t).forEach((e=>{t[e]>r&&(n=e,r=t[e])})),n}(e.map((e=>t in e?e[t]:void 0)).filter((e=>void 0!==e)));r&&(n[t]=r)})),n}let E;function P(){let e=[];const t={"prefers-contrast":["high","more","low","less","forced","no-preference"],"any-hover":["hover","none"],"any-pointer":["none","coarse","fine"],pointer:["none","coarse","fine"],hover:["hover","none"],update:["fast","slow"],"inverted-colors":["inverted","none"],"prefers-reduced-motion":["reduce","no-preference"],"prefers-reduced-transparency":["reduce","no-preference"],scripting:["none","initial-only","enabled"],"forced-colors":["active","none"]};return Object.keys(t).forEach((n=>{t[n].forEach((t=>{matchMedia(`(${n}: ${t})`).matches&&e.push(`${n}: ${t}`)}))})),e}function A(){if("https:"===window.location.protocol&&"function"==typeof window.ApplePaySession)try{const e=window.ApplePaySession.supportsVersion;for(let t=15;t>0;t--)if(e(t))return t}catch(e){return 0}return 0}"Firefox"!=f().name&&o("fonts",(function(){return new Promise(((e,t)=>{try{!async function(e){for(var t;!document.body;)await p(50);const n=document.createElement("iframe");n.setAttribute("frameBorder","0");const r=n.style;r.setProperty("position","fixed"),r.setProperty("display","block","important"),r.setProperty("visibility","visible"),r.setProperty("border","0"),r.setProperty("opacity","0"),n.src="about:blank",document.body.appendChild(n);const o=n.contentDocument||(null===(t=n.contentWindow)||void 0===t?void 0:t.document);if(!o)throw new Error("Iframe document is not accessible");e({iframe:o}),setTimeout((()=>{document.body.removeChild(n)}),0)}((async({iframe:t})=>{const n=t.createElement("canvas").getContext("2d"),r=y.map((e=>v(n,e)));let o={};w.forEach((e=>{const t=v(n,e);r.includes(t)||(o[e]=t)})),e(o)}))}catch(e){t({error:"unsupported"})}}))})),o("hardware",(function(){return new Promise(((e,t)=>{const n=void 0!==navigator.deviceMemory?navigator.deviceMemory:0,r=window.performance&&window.performance.memory?window.performance.memory:0;e({videocard:S(),architecture:b(),deviceMemory:n.toString()||"undefined",jsHeapSizeLimit:r.jsHeapSizeLimit||0})}))})),o("locales",(function(){return new Promise((e=>{e({languages:navigator.language,timezone:Intl.DateTimeFormat().resolvedOptions().timeZone})}))})),o("permissions",(async function(){E=(null==t?void 0:t.permissions_to_check)||["accelerometer","accessibility","accessibility-events","ambient-light-sensor","background-fetch","background-sync","bluetooth","camera","clipboard-read","clipboard-write","device-info","display-capture","gyroscope","geolocation","local-fonts","magnetometer","microphone","midi","nfc","notifications","payment-handler","persistent-storage","push","speaker","storage-access","top-level-storage-access","window-management","query"];const e=Array.from({length:(null==t?void 0:t.retries)||3},(()=>async function(){const e={};for(const t of E)try{const n=await navigator.permissions.query({name:t});e[t]=n.state.toString()}catch(e){}return e}()));return Promise.all(e).then((e=>M(e,E)))})),o("plugins",(function(){const e=[];if(navigator.plugins)for(let t=0;t<navigator.plugins.length;t++){const n=navigator.plugins[t];e.push([n.name,n.filename,n.description].join("|"))}return new Promise((t=>{t({plugins:e})}))})),o("screen",(function(){return new Promise((e=>{e({is_touchscreen:navigator.maxTouchPoints>0,maxTouchPoints:navigator.maxTouchPoints,colorDepth:screen.colorDepth,mediaMatches:P()})}))})),o("system",(function(){return new Promise((e=>{const t=f();e({platform:window.navigator.platform,cookieEnabled:window.navigator.cookieEnabled,productSub:navigator.productSub,product:navigator.product,useragent:navigator.userAgent,hardwareConcurrency:navigator.hardwareConcurrency,browser:{name:t.name,version:t.version},applePayVersion:A()})}))}));const C="SamsungBrowser"!==f().name?1:3;let x,T=null;o("webgl",(async function(){"undefined"!=typeof document&&(x=document.createElement("canvas"),x.width=200,x.height=100,T=x.getContext("webgl"));try{if(!T)throw new Error("WebGL not supported");const e=Array.from({length:C},(()=>function(){try{if(!T)throw new Error("WebGL not supported");const e="\n attribute vec2 position;\n void main() {\n gl_Position = vec4(position, 0.0, 1.0);\n }\n ",t="\n precision mediump float;\n void main() {\n gl_FragColor = vec4(0.812, 0.195, 0.553, 0.921); // Set line color\n }\n ",n=T.createShader(T.VERTEX_SHADER),r=T.createShader(T.FRAGMENT_SHADER);if(!n||!r)throw new Error("Failed to create shaders");if(T.shaderSource(n,e),T.shaderSource(r,t),T.compileShader(n),!T.getShaderParameter(n,T.COMPILE_STATUS))throw new Error("Vertex shader compilation failed: "+T.getShaderInfoLog(n));if(T.compileShader(r),!T.getShaderParameter(r,T.COMPILE_STATUS))throw new Error("Fragment shader compilation failed: "+T.getShaderInfoLog(r));const o=T.createProgram();if(!o)throw new Error("Failed to create shader program");if(T.attachShader(o,n),T.attachShader(o,r),T.linkProgram(o),!T.getProgramParameter(o,T.LINK_STATUS))throw new Error("Shader program linking failed: "+T.getProgramInfoLog(o));T.useProgram(o);const a=137,i=new Float32Array(4*a),s=2*Math.PI/a;for(let e=0;e<a;e++){const t=e*s;i[4*e]=0,i[4*e+1]=0,i[4*e+2]=Math.cos(t)*(x.width/2),i[4*e+3]=Math.sin(t)*(x.height/2)}const c=T.createBuffer();T.bindBuffer(T.ARRAY_BUFFER,c),T.bufferData(T.ARRAY_BUFFER,i,T.STATIC_DRAW);const u=T.getAttribLocation(o,"position");T.enableVertexAttribArray(u),T.vertexAttribPointer(u,2,T.FLOAT,!1,0,0),T.viewport(0,0,x.width,x.height),T.clearColor(0,0,0,1),T.clear(T.COLOR_BUFFER_BIT),T.drawArrays(T.LINES,0,2*a);const l=new Uint8ClampedArray(x.width*x.height*4);T.readPixels(0,0,x.width,x.height,T.RGBA,T.UNSIGNED_BYTE,l);return new ImageData(l,x.width,x.height)}catch(e){return new ImageData(1,1)}finally{T&&(T.bindBuffer(T.ARRAY_BUFFER,null),T.useProgram(null),T.viewport(0,0,T.drawingBufferWidth,T.drawingBufferHeight),T.clearColor(0,0,0,0))}}()));return{commonImageHash:c(h(e,x.width,x.height).data.toString()).toString()}}catch(e){return{webgl:"unsupported"}}}));const R=(e,t,n,r)=>{const o=(n-t)/r;let a=0;for(let n=0;n<r;n++){a+=e(t+(n+.5)*o)}return a*o};o("math",(async()=>({acos:Math.acos(.5),asin:R(Math.asin,-1,1,97),atan:R(Math.atan,-1,1,97),cos:R(Math.cos,0,Math.PI,97),cosh:Math.cosh(9/7),e:Math.E,largeCos:Math.cos(1e20),largeSin:Math.sin(1e20),largeTan:Math.tan(1e20),log:Math.log(1e3),pi:Math.PI,sin:R(Math.sin,-Math.PI,Math.PI,97),sinh:R(Math.sinh,-9/7,7/9,97),sqrt:Math.sqrt(2),tan:R(Math.tan,0,2*Math.PI,97),tanh:R(Math.tanh,-9/7,7/9,97)}))),e.getFingerprint=async function(){const e=await u(),t=new Uint8Array(1);window.crypto.getRandomValues(t);const n=t[0],r=(new TextEncoder).encode(JSON.stringify(e)),o=new Uint8Array(r.length+1);o[0]=n;for(let e=0;e<r.length;e++)o[e+1]=(r[e]+n)%256;return btoa(String.fromCharCode(...o))},e.getFingerprintData=u,e.getFingerprintHash=async function(e){try{const t=await u(),n=c(JSON.stringify(t));return e?{hash:n.toString(),data:t}:n.toString()}catch(e){throw e}},e.getVersion=function(){return"0.1.1"},e.setOption=function(e,n){if(!["exclude","permissions_to_check","retries","timeout"].includes(e))throw new Error("Unknown option "+e);if(["exclude","permissions_to_check"].includes(e)&&(!Array.isArray(n)||!n.every((e=>"string"==typeof e))))throw new Error("The value of the exclude and permissions_to_check must be an array of strings");if(["retries","timeout"].includes(e)&&"number"!=typeof n)throw new Error("The value of retries must be a number");t[e]=n}})); //# sourceMappingURL=index.umd.js.map