erinx

fingerprint.js

Apr 14th, 2015
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;(function (name, context, definition) {
  2.   if (typeof module !== 'undefined' && module.exports) { module.exports = definition(); }
  3.   else if (typeof define === 'function' && define.amd) { define(definition); }
  4.   else { context[name] = definition(); }
  5. })('Fingerprint', this, function () {
  6.   'use strict';
  7.  
  8.   var Fingerprint = function (options) {
  9.     var nativeForEach, nativeMap;
  10.     nativeForEach = Array.prototype.forEach;
  11.     nativeMap = Array.prototype.map;
  12.  
  13.     this.each = function (obj, iterator, context) {
  14.       if (obj === null) {
  15.         return;
  16.       }
  17.       if (nativeForEach && obj.forEach === nativeForEach) {
  18.         obj.forEach(iterator, context);
  19.       } else if (obj.length === +obj.length) {
  20.         for (var i = 0, l = obj.length; i < l; i++) {
  21.           if (iterator.call(context, obj[i], i, obj) === {}) return;
  22.         }
  23.       } else {
  24.         for (var key in obj) {
  25.           if (obj.hasOwnProperty(key)) {
  26.             if (iterator.call(context, obj[key], key, obj) === {}) return;
  27.           }
  28.         }
  29.       }
  30.     };
  31.  
  32.     this.map = function(obj, iterator, context) {
  33.       var results = [];
  34.       if (obj == null) return results;
  35.       if (nativeMap && obj.map === nativeMap) return obj.map(iterator, context);
  36.       this.each(obj, function(value, index, list) {
  37.         results[results.length] = iterator.call(context, value, index, list);
  38.       });
  39.       return results;
  40.     };
  41.  
  42.     if (typeof options == 'object'){
  43.       this.hasher = options.hasher;
  44.       this.screen_resolution = options.screen_resolution;
  45.       this.screen_orientation = options.screen_orientation;
  46.       this.canvas = options.canvas;
  47.       this.ie_activex = options.ie_activex;
  48.     } else if(typeof options == 'function'){
  49.       this.hasher = options;
  50.     }
  51.   };
  52.  
  53.   Fingerprint.prototype = {
  54.     get: function(){
  55.       var keys = [];
  56.       keys.push(navigator.userAgent);
  57.       keys.push(navigator.language);
  58.       keys.push(screen.colorDepth);
  59.       if (this.screen_resolution) {
  60.         var resolution = this.getScreenResolution();
  61.         if (typeof resolution !== 'undefined'){
  62.           keys.push(this.getScreenResolution().join('x'));
  63.         }
  64.       }
  65.       keys.push(new Date().getTimezoneOffset());
  66.       keys.push(this.hasSessionStorage());
  67.       keys.push(this.hasLocalStorage());
  68.       keys.push(!!window.indexedDB);
  69.       if(document.body){
  70.         keys.push(typeof(document.body.addBehavior));
  71.       } else {
  72.         keys.push(typeof undefined);
  73.       }
  74.       keys.push(typeof(window.openDatabase));
  75.       keys.push(navigator.cpuClass);
  76.       keys.push(navigator.platform);
  77.       keys.push(navigator.doNotTrack);
  78.       keys.push(this.getPluginsString());
  79.       if(this.canvas && this.isCanvasSupported()){
  80.         keys.push(this.getCanvasFingerprint());
  81.       }
  82.       if(this.hasher){
  83.         return this.hasher(keys.join('###'), 31);
  84.       } else {
  85.         return this.murmurhash3_32_gc(keys.join('###'), 31);
  86.       }
  87.     },
  88.  
  89.     murmurhash3_32_gc: function(key, seed) {
  90.       var remainder, bytes, h1, h1b, c1, c2, k1, i;
  91.  
  92.       remainder = key.length & 3;
  93.       bytes = key.length - remainder;
  94.       h1 = seed;
  95.       c1 = 0xcc9e2d51;
  96.       c2 = 0x1b873593;
  97.       i = 0;
  98.  
  99.       while (i < bytes) {
  100.           k1 =
  101.             ((key.charCodeAt(i) & 0xff)) |
  102.             ((key.charCodeAt(++i) & 0xff) << 8) |
  103.             ((key.charCodeAt(++i) & 0xff) << 16) |
  104.             ((key.charCodeAt(++i) & 0xff) << 24);
  105.         ++i;
  106.  
  107.         k1 = ((((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16))) & 0xffffffff;
  108.         k1 = (k1 << 15) | (k1 >>> 17);
  109.         k1 = ((((k1 & 0xffff) * c2) + ((((k1 >>> 16) * c2) & 0xffff) << 16))) & 0xffffffff;
  110.  
  111.         h1 ^= k1;
  112.             h1 = (h1 << 13) | (h1 >>> 19);
  113.         h1b = ((((h1 & 0xffff) * 5) + ((((h1 >>> 16) * 5) & 0xffff) << 16))) & 0xffffffff;
  114.         h1 = (((h1b & 0xffff) + 0x6b64) + ((((h1b >>> 16) + 0xe654) & 0xffff) << 16));
  115.       }
  116.  
  117.       k1 = 0;
  118.  
  119.       switch (remainder) {
  120.         case 3: k1 ^= (key.charCodeAt(i + 2) & 0xff) << 16;
  121.         case 2: k1 ^= (key.charCodeAt(i + 1) & 0xff) << 8;
  122.         case 1: k1 ^= (key.charCodeAt(i) & 0xff);
  123.  
  124.         k1 = (((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16)) & 0xffffffff;
  125.         k1 = (k1 << 15) | (k1 >>> 17);
  126.         k1 = (((k1 & 0xffff) * c2) + ((((k1 >>> 16) * c2) & 0xffff) << 16)) & 0xffffffff;
  127.         h1 ^= k1;
  128.       }
  129.  
  130.       h1 ^= key.length;
  131.  
  132.       h1 ^= h1 >>> 16;
  133.       h1 = (((h1 & 0xffff) * 0x85ebca6b) + ((((h1 >>> 16) * 0x85ebca6b) & 0xffff) << 16)) & 0xffffffff;
  134.       h1 ^= h1 >>> 13;
  135.       h1 = ((((h1 & 0xffff) * 0xc2b2ae35) + ((((h1 >>> 16) * 0xc2b2ae35) & 0xffff) << 16))) & 0xffffffff;
  136.       h1 ^= h1 >>> 16;
  137.  
  138.       return h1 >>> 0;
  139.     },
  140.  
  141.     hasLocalStorage: function () {
  142.       try{
  143.         return !!window.localStorage;
  144.       } catch(e) {
  145.         return true;
  146.       }
  147.     },
  148.  
  149.     hasSessionStorage: function () {
  150.       try{
  151.         return !!window.sessionStorage;
  152.       } catch(e) {
  153.         return true;
  154.       }
  155.     },
  156.  
  157.     isCanvasSupported: function () {
  158.       var elem = document.createElement('canvas');
  159.       return !!(elem.getContext && elem.getContext('2d'));
  160.     },
  161.  
  162.     isIE: function () {
  163.       if(navigator.appName === 'Microsoft Internet Explorer') {
  164.         return true;
  165.       } else if(navigator.appName === 'Netscape' && /Trident/.test(navigator.userAgent)){
  166.         return true;
  167.       }
  168.       return false;
  169.     },
  170.  
  171.     getPluginsString: function () {
  172.       if(this.isIE() && this.ie_activex){
  173.         return this.getIEPluginsString();
  174.       } else {
  175.         return this.getRegularPluginsString();
  176.       }
  177.     },
  178.  
  179.     getRegularPluginsString: function () {
  180.       return this.map(navigator.plugins, function (p) {
  181.         var mimeTypes = this.map(p, function(mt){
  182.           return [mt.type, mt.suffixes].join('~');
  183.         }).join(',');
  184.         return [p.name, p.description, mimeTypes].join('::');
  185.       }, this).join(';');
  186.     },
  187.  
  188.     getIEPluginsString: function () {
  189.       if(window.ActiveXObject){
  190.         var names = ['ShockwaveFlash.ShockwaveFlash',
  191.           'AcroPDF.PDF',
  192.           'PDF.PdfCtrl',
  193.           'QuickTime.QuickTime',
  194.           'rmocx.RealPlayer G2 Control',
  195.           'rmocx.RealPlayer G2 Control.1',
  196.           'RealPlayer.RealPlayer(tm) ActiveX Control (32-bit)',
  197.           'RealVideo.RealVideo(tm) ActiveX Control (32-bit)',
  198.           'RealPlayer',
  199.           'SWCtl.SWCtl',
  200.           'WMPlayer.OCX',
  201.           'AgControl.AgControl',
  202.           'Skype.Detection'];
  203.          
  204.         return this.map(names, function(name){
  205.           try{
  206.             new ActiveXObject(name);
  207.             return name;
  208.           } catch(e){
  209.             return null;
  210.           }
  211.         }).join(';');
  212.       } else {
  213.         return "";
  214.       }
  215.     },
  216.  
  217.     getScreenResolution: function () {
  218.       var resolution;
  219.        if(this.screen_orientation){
  220.          resolution = (screen.height > screen.width) ? [screen.height, screen.width] : [screen.width, screen.height];
  221.        }else{
  222.          resolution = [screen.height, screen.width];
  223.        }
  224.        return resolution;
  225.     },
  226.  
  227.     getCanvasFingerprint: function () {
  228.       var canvas = document.createElement('canvas');
  229.       var ctx = canvas.getContext('2d');
  230.       var txt = 'fingerprint';
  231.       ctx.textBaseline = "top";
  232.       ctx.font = "14px 'Arial'";
  233.       ctx.textBaseline = "alphabetic";
  234.       ctx.fillStyle = "#f60";
  235.       ctx.fillRect(125,1,62,20);
  236.       ctx.fillStyle = "#069";
  237.       ctx.fillText(txt, 2, 15);
  238.       ctx.fillStyle = "rgba(102, 204, 0, 0.7)";
  239.       ctx.fillText(txt, 4, 17);
  240.       return canvas.toDataURL();
  241.     }
  242.   };
  243.  
  244.  
  245.   return Fingerprint;
  246.  
  247. });
  248.  
  249.  
  250.  
  251. var fingerPrint = new Fingerprint().get();
Advertisement
Add Comment
Please, Sign In to add comment