PageRenderTime 43ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/admin228c4ef56/themes/default/js/vendor/jquery-passy.js

https://gitlab.com/staging06/myproject
JavaScript | 216 lines | 169 code | 34 blank | 13 comment | 24 complexity | 9b1deedcabc1f604f8b5ab3d700b9d04 MD5 | raw file
  1. /*
  2. * jQuery Passy
  3. * Generating and analazing passwords, realtime.
  4. *
  5. * Tim Severien
  6. * http://timseverien.nl
  7. *
  8. * Copyright (c) 2013 Tim Severien
  9. * Released under the GPLv2 license.
  10. *
  11. */
  12. (function($) {
  13. var passy = {
  14. character: { DIGIT: 1, LOWERCASE: 2, UPPERCASE: 4, PUNCTUATION: 8 },
  15. strength: { LOW: 0, MEDIUM: 1, HIGH: 2, EXTREME: 3 },
  16. dictionary: [],
  17. patterns: [
  18. '0123456789',
  19. 'abcdefghijklmnopqrstuvwxyz',
  20. 'qwertyuiopasdfghjklzxcvbnm',
  21. 'azertyuiopqsdfghjklmwxcvbn',
  22. '!#$*+-.:?@^'
  23. ],
  24. threshold: {
  25. medium: 16,
  26. high: 22,
  27. extreme: 36
  28. }
  29. };
  30. passy.requirements = {
  31. characters: passy.character.DIGIT | passy.character.LOWERCASE | passy.character.UPPERCASE,
  32. length: {
  33. min: 6,
  34. max: Infinity
  35. }
  36. };
  37. if(Object.seal) {
  38. Object.seal(passy.character);
  39. Object.seal(passy.strength);
  40. }
  41. if(Object.freeze) {
  42. Object.freeze(passy.character);
  43. Object.freeze(passy.strength);
  44. }
  45. passy.analize = function(password) {
  46. var score = Math.floor(password.length * 2);
  47. var i = password.length;
  48. score += $.passy.analizePatterns(password);
  49. score += $.passy.analizeDictionary(password);
  50. while(i--) score += $.passy.analizeCharacter(password.charAt(i));
  51. return $.passy.analizeScore(score);
  52. };
  53. passy.analizeCharacter = function(character) {
  54. var code = character.charCodeAt(0);
  55. if(code >= 97 && code <= 122) return 1; // lower case
  56. if(code >= 48 && code <= 57) return 2; // numeric
  57. if(code >= 65 && code <= 90) return 3; // capital
  58. if(code <= 126) return 4; // punctuation
  59. return 5; // foreign characters etc
  60. };
  61. passy.analizePattern = function(password, pattern) {
  62. var lastmatch = -1;
  63. var score = -2;
  64. for(var i = 0; i < password.length; i++) {
  65. var match = pattern.indexOf(password.charAt(i));
  66. if(lastmatch === match - 1) {
  67. lastmatch = match;
  68. score++;
  69. }
  70. }
  71. return Math.max(0, score);
  72. };
  73. passy.analizePatterns = function(password) {
  74. var chars = password.toLowerCase();
  75. var score = 0;
  76. for(var i in $.passy.patterns) {
  77. var pattern = $.passy.patterns[i].toLowerCase();
  78. score += $.passy.analizePattern(chars, pattern);
  79. }
  80. // patterns are bad man!
  81. return score * -5;
  82. };
  83. passy.analizeDictionary = function(password) {
  84. var chars = password.toLowerCase();
  85. var score = 0;
  86. for(var i in $.passy.dictionary) {
  87. var word = $.passy.dictionary[i].toLowerCase();
  88. if(password.indexOf(word) >= 0) score++;
  89. }
  90. // using words are bad too!
  91. return score * -5;
  92. };
  93. passy.analizeScore = function(score) {
  94. if(score >= $.passy.threshold.extreme) return $.passy.strength.EXTREME;
  95. if(score >= $.passy.threshold.high) return $.passy.strength.HIGH;
  96. if(score >= $.passy.threshold.medium) return $.passy.strength.MEDIUM;
  97. return $.passy.strength.LOW;
  98. };
  99. passy.generate = function(len) {
  100. var chars = [
  101. '0123456789',
  102. 'abcdefghijklmnopqrstuvwxyz',
  103. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
  104. '!#$&()*+<=>@[]^'
  105. ];
  106. var password = [];
  107. var type, index;
  108. len = Math.max(len, $.passy.requirements.length.min);
  109. len = Math.min(len, $.passy.requirements.length.max);
  110. while(len--) {
  111. type = len % chars.length;
  112. index = Math.floor(Math.random() * chars[type].length);
  113. password.push(chars[type].charAt(index));
  114. }
  115. password.sort(function() {
  116. return Math.random() * 2 - 1;
  117. });
  118. return password.join('');
  119. };
  120. passy.contains = function(str, character) {
  121. if(character === $.passy.character.DIGIT) {
  122. return /\d/.test(str);
  123. } else if(character === $.passy.character.LOWERCASE) {
  124. return /[a-z]/.test(str);
  125. } else if(character === $.passy.character.UPPERCASE) {
  126. return /[A-Z]/.test(str);
  127. } else if(character === $.passy.character.PUNCTUATION) {
  128. return /[!"#$%&'()*+,\-./:;<=>?@[\\]\^_`{\|}~]/.test(str);
  129. }
  130. };
  131. passy.valid = function(str) {
  132. var valid = true;
  133. if(!$.passy.requirements) return true;
  134. if(str.length < $.passy.requirements.length.min) return false;
  135. if(str.length > $.passy.requirements.length.max) return false;
  136. for(var i in $.passy.character) {
  137. if($.passy.requirements.characters & $.passy.character[i]) {
  138. valid = $.passy.contains(str, $.passy.character[i]) && valid;
  139. }
  140. }
  141. return valid;
  142. };
  143. var methods = {
  144. init: function(callback) {
  145. var $this = $(this);
  146. $this.on('change keyup', function() {
  147. if(typeof callback !== 'function') return;
  148. var value = $this.val();
  149. callback.call($this, $.passy.analize(value), methods.valid.call($this));
  150. });
  151. },
  152. generate: function(len) {
  153. this.val($.passy.generate(len));
  154. this.change();
  155. },
  156. valid: function() {
  157. return $.passy.valid(this.val());
  158. }
  159. };
  160. $.fn.passy = function(opt) {
  161. if(methods[opt]) {
  162. return methods[opt].apply(this, Array.prototype.slice.call(arguments, 1));
  163. } else if (typeof opt === 'function' || !opt) {
  164. return methods.init.apply(this, arguments);
  165. }
  166. return this;
  167. };
  168. $.extend({ passy: passy });
  169. })(jQuery);