stephencelis-haddock 0.0.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.
data/History.txt ADDED
@@ -0,0 +1,5 @@
1
+ === 0.0.1 / 2009-03-28
2
+
3
+ * 1 major enhancement
4
+
5
+ * Birthday!
data/Manifest.txt ADDED
@@ -0,0 +1,7 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ bin/haddock
6
+ lib/haddock.rb
7
+ test/test_haddock.rb
data/README.txt ADDED
@@ -0,0 +1,79 @@
1
+ = haddock
2
+
3
+ http://github.com/stephencelis/haddock
4
+
5
+
6
+ == DESCRIPTION
7
+
8
+ A more memorable password generator. Swordfish? No, I got tired of that. I
9
+ changed it.
10
+
11
+
12
+ == FEATURES/PROBLEMS
13
+
14
+ * Secure!
15
+
16
+
17
+ == SYNOPSIS
18
+
19
+ In your apps:
20
+
21
+ require "rubygems"
22
+ require "haddock"
23
+ include Haddock
24
+ Password.generate # => "bowl9&bracky"
25
+ Password.generate(30) # => "Phlebotomus2473?nonconditioned"
26
+ Password.generate(8) # => "amy7@rax"
27
+
28
+
29
+ On the command line:
30
+
31
+ % haddock
32
+ bowl9&bracky
33
+ % haddock -l31
34
+ symbolistically5<overthwartways
35
+
36
+
37
+ == REQUIREMENTS
38
+
39
+ A newline-delimited words file. By default, it uses "/usr/share/dict/words" or
40
+ "/usr/share/words". Otherwise:
41
+
42
+ Haddock::Password.diction = "/path/to/words"
43
+
44
+
45
+ Or:
46
+
47
+ % haddock -w /path/to/words
48
+
49
+
50
+ == INSTALL
51
+
52
+ GitHub:
53
+
54
+ sudo gem install stephencelis-haddock --source=http://gems.github.com
55
+
56
+
57
+ == LICENSE
58
+
59
+ (The MIT License)
60
+
61
+ (c) 2009-* Stephen Celis, stephen@stephencelis.com.
62
+
63
+ Permission is hereby granted, free of charge, to any person obtaining a copy
64
+ of this software and associated documentation files (the "Software"), to deal
65
+ in the Software without restriction, including without limitation the rights
66
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
67
+ copies of the Software, and to permit persons to whom the Software is
68
+ furnished to do so, subject to the following conditions:
69
+
70
+ The above copyright notice and this permission notice shall be included in all
71
+ copies or substantial portions of the Software.
72
+
73
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
74
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
75
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
76
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
77
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
78
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
79
+ SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ $: << File.dirname(__FILE__) + "/lib"
2
+ require "rubygems"
3
+ require "hoe"
4
+ require "haddock"
5
+
6
+ Hoe.new('haddock', Haddock::VERSION) do |p|
7
+ p.developer('Stephen Celis', 'stephen@stephencelis.com')
8
+ end
data/bin/haddock ADDED
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $: << File.dirname(__FILE__) + "/../lib"
4
+ require "optparse"
5
+ require "haddock"
6
+ include Haddock
7
+
8
+ parser = OptionParser.new do |opts|
9
+ opts.banner = "usage: #{File.basename($0)} [options]"
10
+
11
+ opts.on("-h", "--help") do
12
+ puts opts
13
+ exit
14
+ end
15
+
16
+ opts.on("-l", "--length [length]") do |value|
17
+ @length = value
18
+ end
19
+
20
+ opts.on("-f", "--words [words file]") do |value|
21
+ Password.diction = value
22
+ end
23
+ end
24
+
25
+ begin
26
+ parser.parse!
27
+ puts @length ? Password.generate(@length) : Password.generate
28
+ rescue OptionParser::ParseError, Password::LengthError => error
29
+ warn "haddock: #{error.message} " +
30
+ "(must be between #{Password::MINIMUM} and #{Password::MAXIMUM})."
31
+ puts parser
32
+ exit 1
33
+ end
data/lib/haddock.rb ADDED
@@ -0,0 +1,72 @@
1
+ # A more memorable password generator. Swordfish? No, I got tired of that. I
2
+ # changed it.
3
+ module Haddock
4
+ VERSION = '0.0.1'
5
+
6
+ module Password
7
+ MINIMUM = 8
8
+ MAXIMUM = 31
9
+ DEFAULT = 12
10
+
11
+ SYMBOLS = '`~!@#$%^&*()-_=+[{]}\\|;:\'",<.>/?'
12
+
13
+ class << self
14
+ @@paths = %w(/usr/share/dict/words /usr/share/words)
15
+
16
+ # Generates a more memorable password. Its one optional argument
17
+ # determines the length of the generated password, and cannot be less
18
+ # than 8 or greater than 31 characters (default: 12).
19
+ #
20
+ # Password.generate # => "bowl9&bracky"
21
+ # Password.generate(30) # => "Phlebotomus2473?nonconditioned"
22
+ # Password.generate(8) # => "amy7@rax"
23
+ def generate(length = DEFAULT)
24
+ @@diction ||= IO.readlines(@@paths.find { |path| File.exist?(path) })
25
+
26
+ if !length.is_a?(Integer)
27
+ raise LengthError, "Invalid password" if length.match(/\D/)
28
+ length = length.to_i
29
+ end
30
+
31
+ raise LengthError, "Password length is too short" if length < MINIMUM
32
+ raise LengthError, "Password length is too long" if length > MAXIMUM
33
+
34
+ words_limit = length * 0.75 # Ensure over-proportionate word lengths.
35
+
36
+ begin
37
+ words = %W(#{random_word} #{random_symbol}#{random_word})
38
+ words_length = words.to_s.length
39
+ end until words_length < length && words_length > words_limit
40
+
41
+ words.join random_number(length - words_length)
42
+ end
43
+
44
+ # Sets the dictionary. Uses "/usr/share/dict/words" otherwise.
45
+ def diction=(path)
46
+ @@diction = IO.readlines path
47
+ end
48
+
49
+ private
50
+
51
+ def random_word
52
+ @@diction[rand(@@diction.length)].chomp
53
+ end
54
+
55
+ def random_symbol
56
+ SYMBOLS[rand(SYMBOLS.length), 1]
57
+ end
58
+
59
+ def random_number(digits)
60
+ begin
61
+ number = rand(10 ** digits).to_s
62
+ end until number.length == digits
63
+
64
+ number
65
+ end
66
+ end
67
+
68
+ # Raised if a password is generated with too few or too many characters.
69
+ class LengthError < StandardError
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,48 @@
1
+ require "test/unit"
2
+ require "haddock"
3
+
4
+ class TestHaddock < Test::Unit::TestCase
5
+ include Haddock
6
+
7
+ def test_generates_password
8
+ password = Password.generate
9
+ assert_instance_of String, password
10
+ assert_equal Password::DEFAULT, password.length
11
+ end
12
+
13
+ def test_password_format
14
+ symbols = "[#{Regexp.quote Password::SYMBOLS}]"
15
+ pattern = /^[a-z]+[0-9]+#{symbols}{1}[a-z]+/i
16
+ assert_match(pattern, Password.generate)
17
+ end
18
+
19
+ def test_generates_variable_password
20
+ assert_equal Password.generate(18).length, 18
21
+ end
22
+
23
+ def test_accepts_alternate_wordlist
24
+ Password.diction = path = File.dirname(__FILE__) + "/names"
25
+ pattern = Regexp.new File.read(path).split.join("|")
26
+ assert_match(pattern, Password.generate(14))
27
+ ensure
28
+ Password.diction = "/usr/share/dict/words"
29
+ end
30
+
31
+ def test_fail_on_too_short
32
+ assert_raise Password::LengthError do
33
+ Password.generate(Password::MINIMUM - 1)
34
+ end
35
+ end
36
+
37
+ def test_fail_on_too_long
38
+ assert_raise Password::LengthError do
39
+ Password.generate(Password::MAXIMUM + 1)
40
+ end
41
+ end
42
+
43
+ def test_fail_on_invalid
44
+ assert_raise Password::LengthError do
45
+ Password.generate("invalid")
46
+ end
47
+ end
48
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stephencelis-haddock
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Stephen Celis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-28 00:00:00 -07:00
13
+ default_executable: haddock
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.11.0
24
+ version:
25
+ description: A more memorable password generator. Swordfish? No, I got tired of that. I changed it.
26
+ email:
27
+ - stephen@stephencelis.com
28
+ executables:
29
+ - haddock
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - History.txt
34
+ - Manifest.txt
35
+ - README.txt
36
+ files:
37
+ - History.txt
38
+ - Manifest.txt
39
+ - README.txt
40
+ - Rakefile
41
+ - bin/haddock
42
+ - lib/haddock.rb
43
+ - test/test_haddock.rb
44
+ has_rdoc: true
45
+ homepage: http://github.com/stephencelis/haddock
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --main
49
+ - README.txt
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ requirements: []
65
+
66
+ rubyforge_project: haddock
67
+ rubygems_version: 1.2.0
68
+ signing_key:
69
+ specification_version: 2
70
+ summary: A more memorable password generator
71
+ test_files:
72
+ - test/test_haddock.rb