passphrase_entropy 0.1.0
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.
- data/lib/passphrase_entropy/version.rb +3 -0
- data/lib/passphrase_entropy.rb +38 -0
- data/test/passphrase_entropy_test.rb +41 -0
- metadata +48 -0
@@ -0,0 +1,38 @@
|
|
1
|
+
require "zlib"
|
2
|
+
|
3
|
+
# Estimate the entropy of a passphrase. This is calculated as the number of bytes
|
4
|
+
# required to encode the passphrase on top of a Deflate stream of a preset
|
5
|
+
# dictionary.
|
6
|
+
#
|
7
|
+
class PassphraseEntropy
|
8
|
+
|
9
|
+
# Instantiate a new PasswordEntropy calculator.
|
10
|
+
# dictionary should be a String containing a list of words; this is
|
11
|
+
# /usr/share/dict/words by default, which should be good for English systems.
|
12
|
+
#
|
13
|
+
def initialize(dictionary=default_dictionary)
|
14
|
+
@dictionary = dictionary
|
15
|
+
end
|
16
|
+
|
17
|
+
# Estimate the entropy of s (in bytes)
|
18
|
+
#
|
19
|
+
def entropy(s)
|
20
|
+
zlen(s) - baseline
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
def default_dictionary
|
25
|
+
File.read("/usr/share/dict/words")
|
26
|
+
end
|
27
|
+
|
28
|
+
def zlen(s)
|
29
|
+
z = Zlib::Deflate.new
|
30
|
+
out = z.deflate(@dictionary + s, Zlib::FINISH)
|
31
|
+
z.close
|
32
|
+
out.bytesize
|
33
|
+
end
|
34
|
+
|
35
|
+
def baseline
|
36
|
+
@baseline ||= zlen("")
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require "minitest/autorun"
|
4
|
+
require "passphrase_entropy"
|
5
|
+
|
6
|
+
class PassphraseEntropyTest < MiniTest::Unit::TestCase
|
7
|
+
|
8
|
+
def setup
|
9
|
+
@passphrase_entropy = PassphraseEntropy.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def assert_better(better, worse)
|
13
|
+
eb = @passphrase_entropy.entropy(better)
|
14
|
+
ew = @passphrase_entropy.entropy(worse)
|
15
|
+
assert(eb > ew, "Expected #{better} (#{eb}) to be better than #{worse} (#{ew})")
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_adding_punctuation_should_improve_entropy
|
19
|
+
assert_better "rubbish!", "rubbish"
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_random_letters_should_be_better_than_words
|
23
|
+
assert_better "sdfjhweu", "password"
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_capital_letters_and_numbers_should_improve_entropy
|
27
|
+
assert_better "Password1", "password"
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_complex_passwords_should_be_better_than_simple_ones
|
31
|
+
assert_better "Slightly^better 1", "Password1"
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_should_agree_with_xkcd_936
|
35
|
+
assert_better "correct horse battery staple", "Tr0ub4dor&3"
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_mixed_symbols_should_be_better_than_words
|
39
|
+
assert_better "~T3n Char$", "antidisestablishmentarianism"
|
40
|
+
end
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: passphrase_entropy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Paul Battley
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-17 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description:
|
15
|
+
email: pbattley@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/passphrase_entropy.rb
|
21
|
+
- lib/passphrase_entropy/version.rb
|
22
|
+
- test/passphrase_entropy_test.rb
|
23
|
+
homepage: https://github.com/alphagov/passphrase_entropy
|
24
|
+
licenses: []
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 1.8.11
|
44
|
+
signing_key:
|
45
|
+
specification_version: 3
|
46
|
+
summary: Estimate the entropy of a passphrase
|
47
|
+
test_files:
|
48
|
+
- test/passphrase_entropy_test.rb
|