Advertisement
Guest User

Untitled

a guest
Aug 28th, 2012
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. HSIMP.observer = function () {
  2.     "use strict";
  3.     var object = {}, observers = {}, uID = 1;
  4.     object.addObserver = function (o) {
  5.         var i, observer, id = uID;
  6.         if (typeof o != "function") throw new Error("Observer must be a function");
  7.         for (i in observers) if (observers.hasOwnProperty(i)) {
  8.             observer = observers[i];
  9.             if (observer === o) throw new Error("Observer already in the list")
  10.         }
  11.         observers[id] = o;
  12.         uID += 1;
  13.         return id
  14.     };
  15.     object.removeObserver = function (id) {
  16.         if (observers[id]) {
  17.             observers[id] = undefined;
  18.             return
  19.         }
  20.         throw new Error("Could not find observer in list of observers")
  21.     };
  22.     object.notifyObservers = function (data) {
  23.         var i;
  24.         for (i in observers) observers.hasOwnProperty(i) && observers[i](data)
  25.     };
  26.     return object
  27. };
  28. HSIMP.calculator = function (d, translate) {
  29.     "use strict";
  30.     var o = {}, observer = HSIMP.observer(),
  31.         matches = function () {
  32.             var matches = [],
  33.                 add = function (name, match, length) {
  34.                     matches.push({
  35.                         name: name,
  36.                         match: match,
  37.                         length: length
  38.                     })
  39.                 };
  40.             add("ASCII Lowercase", /[a-z]/, 26);
  41.             add("ASCII Uppercase", /[A-Z]/, 26);
  42.             add("ASCII Numbers", /\d/, 10);
  43.             add("ASCII Top Row Symbols", /[!@£#\$%\^&\*\(\)\-_=\+]/, 15);
  44.             add("ASCII Other Symbols", /[\?\/\.>\,<`~\\|"';:\]\}\[\{\s]/, 19);
  45.             add("Unicode Latin 1 Supplement", /[\u00A1-\u00FF]/, 94);
  46.             add("Unicode Latin Extended A", /[\u0100-\u017F]/, 128);
  47.             add("Unicode Latin Extended B", /[\u0180-\u024F]/, 208);
  48.             add("Unicode Latin Extended C", /[\u2C60-\u2C7F]/, 32);
  49.             add("Unicode Latin Extended D", /[\uA720-\uA7FF]/, 29);
  50.             add("Unicode Cyrillic Uppercase", /[\u0410-\u042F]/, 32);
  51.             add("Unicode Cyrillic Lowercase", /[\u0430-\u044F]/, 32);
  52.             return matches
  53.         }(),
  54.         v = {
  55.             time: 0,
  56.             calcs: 25e7,
  57.             characters: 0,
  58.             combinations: 0,
  59.             characterSets: [],
  60.             cost: 0,
  61.             storage: 0
  62.         }, password = "",
  63.         combinations = function () {
  64.             v.length = password.length;
  65.             if (v.length) {
  66.                 v.combinations = Math.pow(v.characters, v.length);
  67.                 v.storage = v.combinations / 1e4;
  68.                 v.cost = v.combinations / 4e5 * (.28 / 60)
  69.             } else {
  70.                 v.storage = 0;
  71.                 v.combinations = 0;
  72.                 v.cost = 0
  73.             }
  74.         }, characters = function () {
  75.             var i, len = matches.length;
  76.             v.characterSets = [];
  77.             v.characters = 0;
  78.             for (i = 0; i < len; i += 1) if (password.match(matches[i].match)) {
  79.                 v.characters += matches[i].length;
  80.                 v.characterSets.push(matches[i].name)
  81.             }
  82.         }, time = function () {
  83.             combinations();
  84.             v.time = v.combinations / v.calcs
  85.         };
  86.     o.setPassword = function (p) {
  87.         password = p;
  88.         characters();
  89.         time();
  90.         observer.notifyObservers()
  91.     };
  92.     o.setCalcs = function (c) {
  93.         c || (c = 1);
  94.         v.calcs = c;
  95.         time();
  96.         observer.notifyObservers()
  97.     };
  98.     o.getValue = function (name) {
  99.         return v[name]
  100.     };
  101.     o.addObserver = observer.addObserver;
  102.     return o
  103. };
  104. HSIMP.checks = function () {
  105.     var o = {}, password = "",
  106.         checks = [],
  107.         ob = HSIMP.observer(),
  108.         insecure = !1,
  109.         warning = !1,
  110.         check = function () {
  111.             var i, len = checks.length,
  112.                 data = [],
  113.                 result, rank = {
  114.                     achievement: 1,
  115.                     advice: 2,
  116.                     warning: 3,
  117.                     insecure: 4
  118.                 }, sort = function (a, b) {
  119.                     return rank[a.type] < rank[b.type] ? 1 : -1
  120.                 };
  121.             insecure = !1;
  122.             warning = !1;
  123.             for (i = 0; i < len; i += 1) {
  124.                 result = checks[i]();
  125.                 if (result) {
  126.                     data.push(result);
  127.                     result.type === "insecure" ? insecure = !0 : result.type === "warning" && (warning = !0)
  128.                 }
  129.             }
  130.             data.sort(sort);
  131.             ob.notifyObservers(data)
  132.         };
  133.     checks.push(function () {
  134.         var i, len = HSIMP.commonPasswords.length,
  135.             rank;
  136.         for (i = 0; i < len; i += 1) if (password.toLowerCase() === HSIMP.commonPasswords[i]) {
  137.             rank = Math.ceil((i + 1) / 10) * 10;
  138.             return {
  139.                 title: "Common Password: In the top " + rank + " most used passwords",
  140.                 type: "insecure",
  141.                 wording: "Your password is very commonly used. It would be cracked almost instantly."
  142.             }
  143.         }
  144.     });
  145.     checks.push(function () {
  146.         var results = password.match(/(.+)\1{2,}/gi);
  147.         if (results) return {
  148.             title: "Repeated Pattern",
  149.             type: "warning",
  150.             wording: "Repeated characters or patterns can make your password more predictable"
  151.         }
  152.     });
  153.     checks.push(function () {
  154.         if (password.match(/^[A-Za-z]+$/)) return {
  155.             title: "Character Variety: Just Letters",
  156.             type: "advice",
  157.             wording: "Your password only contains letters. Adding numbers and symbols can make your password more secure."
  158.         };
  159.         if (password.match(/^[A-Za-z0-9]+$/)) return {
  160.             title: "Character Variety: No Symbols",
  161.             type: "advice",
  162.             wording: "Your password only contains numbers and letters. Adding a symbol can make your password more secure. Don't forget you can often use spaces in passwords."
  163.         };
  164.         if (password.match(/[^A-Za-z0-9\u0000-\u007E]/)) return {
  165.             title: "Character Variety: Non-Standard Character",
  166.             type: "achievement",
  167.             wording: "Your password contains a non-keyboard character. This should make it more secure."
  168.         }
  169.     });
  170.     checks.push(function () {
  171.         if (password.length < 5) return {
  172.             title: "Length: Very short",
  173.             type: "warning",
  174.             wording: "Your password is very short. The longer a password is the more secure it will be."
  175.         };
  176.         if (password.length < 8) return {
  177.             title: "Length: Short",
  178.             type: "advice",
  179.             wording: "Your password is quite short. The longer a password is the more secure it will be."
  180.         };
  181.         if (password.length > 15) return {
  182.             title: "Length: Long",
  183.             type: "achievement",
  184.             wording: "Your password is over 16 characters long. It should be pretty safe."
  185.         }
  186.     });
  187.     checks.push(function () {
  188.         if (password.match(/^[a-zA-Z]+$/)) return {
  189.             title: "Possibly a Word",
  190.             type: "warning",
  191.             wording: "Your password looks like it could be a dictionary word or a name. If it's a name with personal significance it might be easy to guess. If it's a dictionary word it could be cracked very quickly."
  192.         }
  193.     });
  194.     checks.push(function () {
  195.         if (password.match(/^[\-\(\)\.\/\s0-9]+$/)) return {
  196.             title: "Possibly a Telephone Number / Date",
  197.             type: "warning",
  198.             wording: "Your password looks like it might be a telephone number or a date. If it is and it has personal significance then it might be very easy for someone to guess."
  199.         }
  200.     });
  201.     checks.push(function () {
  202.         if (password.match(/^[a-zA-Z]+[0-9]+$/) || password.match(/^[0-9]+[a-zA-Z]+$/)) return {
  203.             title: "Possibly a Word and a Number",
  204.             type: "warning",
  205.             wording: "Your password looks like it might just be a word and a few digits. This is a very common pattern and would be cracked very quickly."
  206.         }
  207.     });
  208.     o.setPassword = function (p) {
  209.         password = p;
  210.         check()
  211.     };
  212.     o.insecure = function () {
  213.         return insecure
  214.     };
  215.     o.warning = function () {
  216.         return warning
  217.     };
  218.     o.addObserver = ob.addObserver;
  219.     return o
  220. };
  221. HSIMP.format = function () {
  222.     "use strict";
  223.     var o = {}, named = !0,
  224.         reference = {};
  225.     reference.bytes = {};
  226.     reference.bytes.dictionary = {
  227.         kilobyte: 1024,
  228.         megabyte: Math.pow(1024, 2),
  229.         gigabyte: Math.pow(1024, 3),
  230.         terabyte: Math.pow(1024, 4),
  231.         petabyte: Math.pow(1024, 5),
  232.         exabyte: Math.pow(1024, 6),
  233.         zettabyte: Math.pow(1024, 7),
  234.         yottabyte: Math.pow(1024, 8),
  235.         brontobyte: Math.pow(1024, 9),
  236.         geopbyte: Math.pow(1024, 10)
  237.     };
  238.     reference.bytes.array = function () {
  239.         var bytes = function (name, value) {
  240.             return {
  241.                 name: name,
  242.                 value: value
  243.             }
  244.         }, sort = function (a, b) {
  245.             return a.value < b.value ? -1 : 1
  246.         }, numbers = reference.bytes.dictionary,
  247.             array = [],
  248.             i;
  249.         for (i in numbers) numbers.hasOwnProperty(i) && array.push(bytes(i, numbers[i]));
  250.         array.sort(sort);
  251.         return array
  252.     }();
  253.     reference.largeNumbers = {};
  254.     reference.largeNumbers.dictionary = {
  255.         thousand: 1e3,
  256.         million: 1e6,
  257.         billion: 1e9,
  258.         trillion: 1e12,
  259.         quadrillion: 1e15,
  260.         quintillion: 1e18,
  261.         sextillion: 1e21,
  262.         septillion: 1e24,
  263.         octillion: 1e27,
  264.         nonillion: 1e30,
  265.         decillion: 1e33,
  266.         undecillion: 1e36,
  267.         duodecillion: 1e39,
  268.         tredecillion: 1e42,
  269.         quattuordecillion: 1e45,
  270.         quindecillion: 1e48,
  271.         sexdecillion: 1e51,
  272.         septendecillion: 1e54,
  273.         octodecillion: 1e57,
  274.         novemdecillion: 1e60,
  275.         vigintillion: 1e63,
  276.         unvigintillion: 1e66,
  277.         duovigintillion: 1e69,
  278.         tresvigintillion: 1e72,
  279.         quattuorvigintillion: 1e75,
  280.         quinquavigintillion: 1e78,
  281.         sesvigintillion: 1e81,
  282.         septemvigintillion: 1e84,
  283.         octovigintillion: 1e87,
  284.         novemvigintillion: 1e90,
  285.         trigintillion: 1e93,
  286.         untrigintillion: 1e96,
  287.         duotrigintillion: 1e99,
  288.         googol: 1e100,
  289.         trestrigintillion: 1e102,
  290.         quattuortrigintillion: 1e105,
  291.         quinquatrigintillion: 1e108,
  292.         sestrigintillion: 1e111,
  293.         septentrigintillion: 1e114,
  294.         octotrigintillion: 1e117,
  295.         noventrigintillion: 1e120,
  296.         quadragintillion: 1e123,
  297.         quinquagintillion: 1e153,
  298.         sexagintillion: 1e183,
  299.         septuagintillion: 1e213,
  300.         octogintillion: 1e243,
  301.         nonagintillion: 1e273
  302.     };
  303.     reference.largeNumbers.array = function () {
  304.         var number = function (name, value) {
  305.             return {
  306.                 name: name,
  307.                 value: value
  308.             }
  309.         }, sort = function (a, b) {
  310.             return a.value < b.value ? -1 : 1
  311.         }, numbers = reference.largeNumbers.dictionary,
  312.             array = [],
  313.             i;
  314.         for (i in numbers) numbers.hasOwnProperty(i) && array.push(number(i, numbers[i]));
  315.         array.sort(sort);
  316.         return array
  317.     }();
  318.     o.number = function (number) {
  319.         var rgx = /(\d+)(\d{3})/,
  320.             split, integer, decimal, magnitude, exponent, numbers = reference.largeNumbers.array,
  321.             len = numbers.length,
  322.             i = 0,
  323.             decimalPlaces = 0,
  324.             repeatString = function (s, t) {
  325.                 var r = "",
  326.                     i = 0;
  327.                 for (i; i < t; i += 1) r += s;
  328.                 return r
  329.             }, exponentiate = function (n) {
  330.                 var string = n.toString(),
  331.                     exp = string.match(/^([\d\.]+)e([\-\+]?)(\d+)$/),
  332.                     split, len;
  333.                 if (!exp) return string;
  334.                 string = exp[1];
  335.                 if (exp[2] === "-") if (string.match(/\./)) {
  336.                     split = string.split(".");
  337.                     len = split[0].length;
  338.                     len < exp[3] ? string = "0." + repeatString("0", exp[3] - len) + split[0] + split[1] : string = split[0].substr(0, len - exp[3]) + "." + split[0].substr(len - exp[3]) + split[1]
  339.                 } else string = "0." + repeatString("0", exp[3] - 1) + string;
  340.                 else if (string.match(/\./)) {
  341.                     split = string.split(".");
  342.                     len = split[1].length;
  343.                     len < exp[3] ? string = split[0] + split[1] + repeatString("0", exp[3] - len) : string = split[0] + split[1].substr(0, exp[3])
  344.                 } else string += repeatString("0", exp[3]);
  345.                 return string
  346.             }, mag = function () {
  347.                 var numbers = reference.largeNumbers.array,
  348.                     i, len = numbers.length,
  349.                     magnitude = "";
  350.                 if (!number || isNaN(number) || number === Infinity) return;
  351.                 while (number >= numbers[0].value) {
  352.                     for (i = 0; i < len; i += 1) if (number < numbers[i].value) break;
  353.                     i -= 1;
  354.                     number /= numbers[i].value;
  355.                     magnitude = numbers[i].name + (magnitude ? " " : "") + magnitude
  356.                 }
  357.                 return magnitude
  358.             };
  359.         named && (magnitude = mag());
  360.         number < 1 && (decimalPlaces = 10);
  361.         number = exponentiate(number);
  362.         split = number.split(".");
  363.         integer = split[0];
  364.         decimalPlaces ? decimal = split.length > 1 ? "." + split[1] : "" : decimal = "";
  365.         while (rgx.test(integer)) integer = integer.replace(rgx, "$1,$2");
  366.         return (integer + decimal.substr(0, decimalPlaces + 1) + " " + (magnitude || "")).replace(/^\s+|\s+$/g, "")
  367.     };
  368.     o.convert = function (string) {
  369.         var nums = reference.largeNumbers.dictionary,
  370.             replace = function (match) {
  371.                 var value, exp, i = 0;
  372.                 if (nums[match]) {
  373.                     value = nums[match];
  374.                     value += "";
  375.                     exp = value.match(/^1e\+([\d]+)$/);
  376.                     if (exp && exp[1]) {
  377.                         value = "";
  378.                         for (i; i < exp[1]; i += 1) value += "0"
  379.                     } else value = value.replace(/^1/, "");
  380.                     return value
  381.                 }
  382.             };
  383.         string = string.replace(/[a-zA-z]+/g, replace);
  384.         string = string.replace(/[^\d\.]/g, "");
  385.         return string
  386.     };
  387.     o.periods = function (time) {
  388.         var period = function (period, inSecs, useAn) {
  389.             useAn = useAn || !1;
  390.             return {
  391.                 period: period,
  392.                 inSecs: inSecs,
  393.                 useAn: useAn
  394.             }
  395.         }, periods = [period("second", 1), period("minute", 60), period("hour", 3600, !0), period("day", 86400), period("year", 31556926)],
  396.             length = periods.length;
  397.         return function (time) {
  398.             var format = o,
  399.                 result = "",
  400.                 period = "",
  401.                 i = 0;
  402.             result = format.number(time) + " seconds";
  403.             for (i; i < length; i += 1) {
  404.                 if (time < periods[i].inSecs) break;
  405.                 result = format.number(time / periods[i].inSecs);
  406.                 result !== "1" && result !== 1 ? period = " " + periods[i].period + "s" : period = " " + periods[i].period;
  407.                 if (result.match(/^1 /) || result.match(/^1$/)) periods[i].useAn || result.match(/^1 [aeiou]/i) ? result = result.replace(/^1/, "An") : result = result.replace(/^1/, "A");
  408.                 result = result + " " + period
  409.             }
  410.             return result
  411.         }
  412.     }();
  413.     o.bytes = function (bytes) {
  414.         var storage = reference.bytes.array,
  415.             format = o,
  416.             plural = "",
  417.             length = storage.length,
  418.             i = 0,
  419.             result = bytes + " bytes";
  420.         for (i; i < length; i += 1) {
  421.             if (bytes < storage[i].value) break;
  422.             result = bytes / storage[i].value;
  423.             result !== 1 && (plural = "s");
  424.             result = format.number(result) + " " + storage[i].name + plural
  425.         }
  426.         return result
  427.     };
  428.     o.setNamed = function (v) {
  429.         named = v
  430.     };
  431.     return o
  432. };
  433. HSIMP.app = function ($scope) {
  434.     "use strict";
  435.     var calculator = HSIMP.calculator(),
  436.         formatter = HSIMP.format(),
  437.         checks = HSIMP.checks(),
  438.         html = document.getElementsByTagName("html")[0],
  439.         updatePassword = function () {
  440.             var timeInSecs = calculator.getValue("time");
  441.             $scope.time = formatter.periods(timeInSecs);
  442.             $scope.possibleCombinations = formatter.number(calculator.getValue("combinations"));
  443.             $scope.characters = formatter.number(calculator.getValue("characters"));
  444.             $scope.calcsPerSecond = formatter.number(calculator.getValue("calcs"));
  445.             timeInSecs === 0 ? html.className = "" : timeInSecs < 1020 ? html.className = "bad" : timeInSecs < 307584e5 ? html.className = "ok" : html.className = "good"
  446.         }, updateChecks = function (c) {
  447.             $scope.checks = c;
  448.             if (checks.insecure()) {
  449.                 html.className = "bad";
  450.                 $scope.time = "Instantly";
  451.                 $scope.insecure = !0
  452.             } else $scope.insecure = !1
  453.         };
  454.     $scope.insecure = !1;
  455.     $scope.characters = 0;
  456.     $scope.possibleCombinations = 0;
  457.     $scope.display = {};
  458.     $scope.display.config = !0;
  459.     $scope.display.details = !0;
  460.     $scope.display.toggleConfig = function () {
  461.         if ($scope.display.config) {
  462.             $scope.display.config = !1;
  463.             $scope.display.configText = "Show Settings"
  464.         } else {
  465.             $scope.display.config = !0;
  466.             $scope.display.configText = "Hide Settings"
  467.         }
  468.         return !1
  469.     };
  470.     $scope.display.toggleDetails = function () {
  471.         if ($scope.display.details) {
  472.             $scope.display.details = !1;
  473.             $scope.display.detailsText = "Show Details"
  474.         } else {
  475.             $scope.display.details = !0;
  476.             $scope.display.detailsText = "Hide Details"
  477.         }
  478.         return !1
  479.     };
  480.     $scope.display.toggleConfig();
  481.     $scope.display.toggleDetails();
  482.     $scope.config = {};
  483.     $scope.config.namedNumbers = !0;
  484.     $scope.config.calculations = "4 billion";
  485.     $scope.config.calculationsOriginal = !0;
  486.     $scope.config.changeNamedNumbers = function () {
  487.         formatter.setNamed($scope.config.namedNumbers);
  488.         calculator.setPassword($scope.password)
  489.     };
  490.     $scope.config.changeCalculations = function () {
  491.         $scope.config.calculationsOriginal = !1;
  492.         calculator.setCalcs(formatter.convert($scope.config.calculations))
  493.     };
  494.     calculator.setCalcs(formatter.convert($scope.config.calculations));
  495.     $scope.lang = HSIMP.language.en.translations;
  496.     calculator.addObserver(updatePassword);
  497.     checks.addObserver(updateChecks);
  498.     $scope.passwordChange = function () {
  499.         $scope.length = $scope.password.length;
  500.         calculator.setPassword($scope.password);
  501.         checks.setPassword($scope.password)
  502.     }
  503. };
  504. (function () {
  505.     "use strict";
  506.     var passwordInput = document.getElementById("password"),
  507.         nojs = document.getElementById("no-js");
  508.     document.getElementById("js").setAttribute("style", "display: block");
  509.     nojs.parentNode.removeChild(nojs);
  510.     passwordInput.focus()
  511. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement