ruby-entropy 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/ruby-entropy.rb +30 -30
  2. metadata +2 -2
data/lib/ruby-entropy.rb CHANGED
@@ -1,42 +1,41 @@
1
- class Password
1
+ class RubyEntropy
2
2
 
3
- attr_reader :password
3
+ attr_reader :passphrase
4
4
 
5
- #lowercase passwords only
6
- COMMON_PASSWORDS = ["admin", "administrator", "jesus", "letmein", "master", "open sesame", "opensesame", "password", "sunshine", "trustnoi", "trustnol", "welcome"]
5
+ #lowercase only
6
+ COMMON_PASSPHRASES = ["admin", "administrator", "jesus", "letmein", "master", "open sesame", "opensesame", "password", "passphrase", "sunshine", "trustnoi", "trustnol", "welcome"]
7
7
  KEY_PATTERNS = ["zxc", "cxz", "bnm", "mnb", "jkl", "lkj", "asd", "dsa", "qwe", "ewq", "iop", "poi"]
8
8
 
9
- def initialize(password)
10
- @password = password
11
- @passwords = []
9
+ def initialize(passphrase)
10
+ @passphrase = passphrase
12
11
  end
13
12
 
14
- def entropy
15
- Math.log2(count ** length)
13
+ def strength
14
+ (31 * bad_passphrase_multiplier * Math.log(entropy / 13.62)).round(2)
16
15
  end
17
16
 
18
- def strength
19
- (31 * bad_password_multiplier * Math.log(entropy / 13.62)).round(2)
17
+ def entropy
18
+ Math.log2(count ** length)
20
19
  end
21
20
 
22
21
  def length
23
- @password.length
22
+ @passphrase.length
24
23
  end
25
24
 
26
25
  def letters
27
- 26 if @password.match(/[a-z]|[A-Z]/)
26
+ 26 if @passphrase.match(/[a-z]|[A-Z]/)
28
27
  end
29
28
 
30
29
  def multiple_cases
31
- 26 if @password.match(/[a-z]/) && @password.match(/[A-Z]/)
30
+ 26 if @passphrase.match(/[a-z]/) && @passphrase.match(/[A-Z]/)
32
31
  end
33
32
 
34
33
  def digits
35
- 10 if @password.match(/\d/)
34
+ 10 if @passphrase.match(/\d/)
36
35
  end
37
36
 
38
37
  def symbols
39
- 33 if @password.match(/\W/)
38
+ 33 if @passphrase.match(/\W/)
40
39
  end
41
40
 
42
41
  def count
@@ -44,12 +43,12 @@ class Password
44
43
  end
45
44
 
46
45
  def key_pattern?
47
- KEY_PATTERNS.each { |pattern| return true if @password.downcase.include?(pattern) }
46
+ KEY_PATTERNS.each { |pattern| return true if @passphrase.downcase.include?(pattern) }
48
47
  false
49
48
  end
50
49
 
51
50
  def numerical_pattern?
52
- pattern = @password.split('').map(&:to_i)
51
+ pattern = @passphrase.split('').map(&:to_i)
53
52
  pattern.each_with_index do |num, index|
54
53
  return true if pattern[index + 1] == num + 1 && pattern[index + 2] == num + 2 && pattern[index + 3] == num + 3
55
54
  return true if pattern[index + 1] == num - 1 && pattern[index + 2] == num - 2 && pattern[index + 3] == num - 3
@@ -58,7 +57,7 @@ class Password
58
57
  end
59
58
 
60
59
  def repetitious?
61
- characters = @password.split('')
60
+ characters = @passphrase.split('')
62
61
  characters.each_with_index do |character, index|
63
62
  return true if characters[index + 1] == character && characters[index + 2] == character
64
63
  end
@@ -66,25 +65,26 @@ class Password
66
65
  end
67
66
 
68
67
  def common?
69
- @passwords << @password
70
- if @password.match(/[@0|1$5]/)
71
- @passwords << @password.gsub('@', 'a').gsub('0', 'o').gsub(/[|1!]/, 'l').gsub(/[$5]/, 's')
72
- @passwords << @password.gsub('@', 'a').gsub('0', 'o').gsub(/[|1!]/, 'i').gsub(/[$5]/, 's')
68
+ @passphrases = []
69
+ @passphrases << @passphrase
70
+ if @passphrase.match(/[@0|1$5]/)
71
+ @passphrases << @passphrase.gsub('@', 'a').gsub('0', 'o').gsub(/[|1!]/, 'l').gsub(/[$5]/, 's')
72
+ @passphrases << @passphrase.gsub('@', 'a').gsub('0', 'o').gsub(/[|1!]/, 'i').gsub(/[$5]/, 's')
73
73
  end
74
- COMMON_PASSWORDS.each do |commoner|
75
- @passwords.each { |password| return true if password.downcase.include?(commoner.downcase) }
74
+ COMMON_PASSPHRASES.each do |commoner|
75
+ @passphrases.each { |passphrase| return true if passphrase.downcase.include?(commoner.downcase) }
76
76
  end
77
77
  false
78
78
  end
79
79
 
80
80
  def uniqueness
81
- (@password.downcase.split('').uniq.length/length.to_f) < 0.4
81
+ (@passphrase.downcase.split('').uniq.length/length.to_f) < 0.4
82
82
  end
83
83
 
84
84
  def repeaters
85
85
  mode = []
86
- @password.downcase.split('').uniq.each do |character|
87
- mode << @password.split('').count(character)
86
+ @passphrase.downcase.split('').uniq.each do |character|
87
+ mode << @passphrase.split('').count(character)
88
88
  end
89
89
  mode.max.downto(2) do |num|
90
90
  return true if (mode.count(num)/mode.length.to_f) > 0.75
@@ -92,8 +92,8 @@ class Password
92
92
  false
93
93
  end
94
94
 
95
- def bad_password_multiplier
95
+ def bad_passphrase_multiplier
96
96
  repeaters || uniqueness ? (return 0.1) : 1
97
- key_pattern? || numerical_pattern? || repetitious? || common? ? @password.length < 12 ? 0.5 : 0.75 : 1
97
+ key_pattern? || numerical_pattern? || repetitious? || common? ? @passphrase.length < 12 ? 0.5 : 0.75 : 1
98
98
  end
99
99
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-entropy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-21 00:00:00.000000000 Z
12
+ date: 2013-05-22 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Calculates password strength based on standard entropy definition. Strength
15
15
  is reduced based on repeatability and common password test functions