rassphrase 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.
@@ -0,0 +1,17 @@
1
+ module Rassphrase
2
+ class Dice
3
+ def initialize
4
+ @random = Random.new
5
+ @roll_count = 0
6
+ end
7
+
8
+ def roll
9
+ @roll_count += 1
10
+ @random.rand(1..6)
11
+ end
12
+
13
+ def roll_count
14
+ @roll_count
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,67 @@
1
+ module Rassphrase
2
+ class Rassphrase
3
+
4
+ def initialize(options = {})
5
+ @wordlist = []
6
+ @dice = options[:dice] || Dice.new
7
+ @size = options[:size] || 5
8
+ @capitalize = options[:capitalize] || false
9
+ wordlist_path = options[:wordlist_path] || File.expand_path('../../diceware.wordlist', __FILE__)
10
+ self.wordlist = options[:wordlist] || WordlistParser::parse(wordlist_path)
11
+ self.generate
12
+ end
13
+
14
+ def codes
15
+ @codes ||= []
16
+ end
17
+
18
+ def generate(size = nil)
19
+ size ||= @size
20
+ @words, @codes = [], []
21
+ size.times do
22
+ item = random_item
23
+ @codes << item[:code]
24
+ @words << item[:word]
25
+ end
26
+ self.passphrase
27
+ end
28
+
29
+ def generate_code
30
+ code = ''
31
+ 5.times { code << @dice.roll.to_s }
32
+ return code
33
+ end
34
+
35
+ def length
36
+ self.words.length
37
+ end
38
+
39
+ def passphrase
40
+ return self.words.join unless @capitalize
41
+ self.words.map { |w| w.capitalize }.join
42
+ end
43
+
44
+ def random_item
45
+ word = nil
46
+ until word do
47
+ code = self.generate_code
48
+ word = self.word(code)
49
+ end
50
+ {:code => code, :word => word}
51
+ end
52
+
53
+ def word(code)
54
+ @wordlist[code.to_s]
55
+ end
56
+
57
+ def wordlist=(wordlist)
58
+ wordlist = WordlistParser::parse(wordlist) if wordlist.is_a?(String) && File.file?(wordlist)
59
+ @wordlist = wordlist.inject({}){ |wl, (k,v)| wl[k.to_s] = v; wl}
60
+ end
61
+
62
+ def words
63
+ @words ||= []
64
+ end
65
+
66
+ end
67
+ end
@@ -0,0 +1,3 @@
1
+ module Rassphrase
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,19 @@
1
+ module Rassphrase
2
+ class WordlistParser
3
+ def self.parse_line(line)
4
+ items = line.split(' ')
5
+ {:code => items[0], :word => items[1]}
6
+ end
7
+
8
+ def self.parse(file_path)
9
+ wordlist = {}
10
+ File.open(file_path, 'r') do |file|
11
+ while line = file.gets
12
+ parts = self.parse_line(line)
13
+ wordlist[parts[:code]] = parts[:word]
14
+ end
15
+ end
16
+ return wordlist
17
+ end
18
+ end
19
+ end
data/lib/rassphrase.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "rassphrase/version"
2
+ require "rassphrase/dice"
3
+ require "rassphrase/wordlist_parser"
4
+ require "rassphrase/rassphrase"
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "rassphrase/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "rassphrase"
7
+ s.version = Rassphrase::VERSION
8
+ s.authors = ["Arturo Guzman"]
9
+ s.email = ["arturo@agrdt.com"]
10
+ s.homepage = "https://github.com/guzart/rassphrase"
11
+ s.summary = "A Ruby implementation of Diceware passphrase"
12
+ s.description = "Uses the Diceware passphrase dictionary and method to generate secure passphrases."
13
+
14
+ s.rubyforge_project = "rassphrase"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "rspec"
22
+ s.add_development_dependency "cucumber"
23
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ module Rassphrase
4
+ describe Dice do
5
+
6
+ let(:dice) { Dice.new }
7
+
8
+ describe "#roll" do
9
+ it "returns a number between 1 and 6" do
10
+ roll = dice.roll
11
+ roll.should be >= 1
12
+ roll.should be <= 6
13
+ end
14
+ end
15
+
16
+ describe "#roll_count" do
17
+ it "returns the number of times the dice has rolled" do
18
+ dice.roll
19
+ dice.roll_count.should == 1
20
+ 2.times { dice.roll }
21
+ dice.roll_count.should == 3
22
+ 5.times { dice.roll }
23
+ dice.roll_count.should == 8
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,154 @@
1
+ require 'spec_helper'
2
+
3
+ module Rassphrase
4
+ describe Rassphrase do
5
+ let(:wordlist) { {12345 => "helloworld"} }
6
+ let(:rassphrase) { Rassphrase.new }
7
+ let(:beale_path) { File.expand_path("../../../lib/beale.wordlist", __FILE__) }
8
+
9
+ describe "#initialize" do
10
+ it "deaults to use the diceware wordlist" do
11
+ rassphrase.word("12345").should == "apathy"
12
+ end
13
+
14
+ it "defaults to 5 words" do
15
+ rassphrase.should have(5).words
16
+ end
17
+
18
+ it "generates a passphrase" do
19
+ rassphrase.passphrase.should have_at_least(4).characters
20
+ end
21
+
22
+ context "accepts a hash argument with the option" do
23
+ it ":dice to specify the dice" do
24
+ dice = Dice.new
25
+ Rassphrase.new(:dice => dice).generate_code
26
+ dice.roll_count.should > 0
27
+ end
28
+
29
+ it ":size to specify the size of the passphrase" do
30
+ Rassphrase.new(:size => 10).should have(10).words
31
+ end
32
+
33
+ it ":wordlist to specify the wordlist to use" do
34
+ Rassphrase.new(:wordlist => wordlist).word("12345").should == "helloworld"
35
+ end
36
+
37
+ it ":wordlist_path to specify the path to the wordlist file" do
38
+ Rassphrase.new(:wordlist_path => beale_path).word("12345").should == "april"
39
+ end
40
+
41
+ it ":capitalize to return a passphrase with capitalized words" do
42
+ rassphrase = Rassphrase.new(:capitalize => true)
43
+ rassphrase.passphrase.should == rassphrase.words.map{ |w| w.capitalize }.join
44
+ end
45
+ end
46
+ end
47
+
48
+ describe "#codes" do
49
+ it "returns the codes used in the passphrase" do
50
+ codes = rassphrase.codes
51
+ codes.each_index do |index|
52
+ rassphrase.words[index].should == rassphrase.word(codes[index])
53
+ end
54
+ end
55
+ end
56
+
57
+ describe "#generate" do
58
+ it "returns a passphrase" do
59
+ passphrase = rassphrase.generate
60
+ passphrase.should have_at_least(1).character
61
+ passphrase.should be_an_instance_of(String)
62
+ end
63
+
64
+ it "generates a new passphrase" do
65
+ rassphrase.passphrase.should_not == rassphrase.generate
66
+ end
67
+
68
+ it "accepts the number of words for the passphrase" do
69
+ rassphrase.should have(5).words
70
+ rassphrase.generate(8)
71
+ rassphrase.should have(8).words
72
+ end
73
+ end
74
+
75
+ describe "#generate_code" do
76
+ it "returns five characters" do
77
+ rassphrase.generate_code.size.should be 5
78
+ end
79
+
80
+ it "returns only numeric characters" do
81
+ rassphrase.generate_code.should match /\d{5}/
82
+ end
83
+
84
+ it "returns numeric characters between 1 and 6" do
85
+ code = rassphrase.generate_code.split('')
86
+ code.each do |c|
87
+ c.to_i.should be <= 6
88
+ c.to_i.should be >= 1
89
+ end
90
+ end
91
+ end
92
+
93
+ describe "#length" do
94
+ it "returns the number of words in the passphrase" do
95
+ rassphrase.should have(5).words
96
+ rassphrase.generate(9)
97
+ rassphrase.should have(9).words
98
+ end
99
+ end
100
+
101
+ describe "#random_item" do
102
+ it "returns a random code and word pair from the word list" do
103
+ item = rassphrase.random_item
104
+ rassphrase.word(item[:code]).should == item[:word]
105
+ end
106
+
107
+ it "keeps generating codes until a word is found" do
108
+ pending 'have to use a mock on self'
109
+ end
110
+ end
111
+
112
+ describe "#word" do
113
+ before(:each) { rassphrase.wordlist = wordlist }
114
+
115
+ it "returns the word for a code" do
116
+ rassphrase.word("12345").should == "helloworld"
117
+ end
118
+
119
+ it "accepts strings as the code argument" do
120
+ rassphrase.word("12345").should == "helloworld"
121
+ end
122
+
123
+ it "accepts numbers as the code argument" do
124
+ rassphrase.word(12345).should == "helloworld"
125
+ end
126
+ end
127
+
128
+ describe "#words" do
129
+ it "returns the words used in the passphrase" do
130
+ rassphrase.passphrase.should == rassphrase.words.join
131
+ end
132
+ end
133
+
134
+ describe "#wordlist=" do
135
+ it "changes the hash argument keys to strings" do
136
+ rassphrase.wordlist = {12345 => "foobar"}
137
+ rassphrase.word("12345").should == "foobar"
138
+ end
139
+
140
+ it "replaces the used wordlist" do
141
+ rassphrase.wordlist = {"123321" => "ab"}
142
+ rassphrase.word("123321").should == "ab"
143
+ rassphrase.wordlist = {"456654" => "ca"}
144
+ rassphrase.word("123321").should be_nil
145
+ end
146
+
147
+ it "accepts a filepath" do
148
+ rassphrase.wordlist = beale_path
149
+ rassphrase.word("12345").should == "april"
150
+ end
151
+ end
152
+
153
+ end
154
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ module Rassphrase
4
+ describe WordlistParser do
5
+ describe '::parse_line' do
6
+ it 'returns a hash with a code and a word' do
7
+ WordlistParser.parse_line('21324 geek').should == {:code => '21324', :word => 'geek'}
8
+ end
9
+ end
10
+
11
+ describe '::parse' do
12
+ it "returns a wordlist from a filepath" do
13
+ wordlist = WordlistParser.parse(File.expand_path("../../../lib/diceware.wordlist", __FILE__))
14
+ wordlist["16655"].should == "clause"
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,6 @@
1
+ require 'rassphrase'
2
+
3
+ RSpec.configure do |c|
4
+ c.color_enabled = true
5
+ c.formatter = :documentation
6
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rassphrase
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Arturo Guzman
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-31 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &2153182440 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2153182440
25
+ - !ruby/object:Gem::Dependency
26
+ name: cucumber
27
+ requirement: &2153182020 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2153182020
36
+ description: Uses the Diceware passphrase dictionary and method to generate secure
37
+ passphrases.
38
+ email:
39
+ - arturo@agrdt.com
40
+ executables:
41
+ - rassphrase
42
+ extensions: []
43
+ extra_rdoc_files: []
44
+ files:
45
+ - .gitignore
46
+ - Gemfile
47
+ - README.md
48
+ - Rakefile
49
+ - bin/rassphrase
50
+ - features/diceware.feature
51
+ - features/generate_passphrase.feature
52
+ - features/roll_dice.feature
53
+ - features/step_definitions/diceware_steps.rb
54
+ - features/step_definitions/generate_passphrase_steps.rb
55
+ - features/step_definitions/roll_dice.rb
56
+ - features/support/env.rb
57
+ - lib/beale.wordlist
58
+ - lib/diceware.wordlist
59
+ - lib/rassphrase.rb
60
+ - lib/rassphrase/dice.rb
61
+ - lib/rassphrase/rassphrase.rb
62
+ - lib/rassphrase/version.rb
63
+ - lib/rassphrase/wordlist_parser.rb
64
+ - rassphrase.gemspec
65
+ - spec/rassphrase/dice_spec.rb
66
+ - spec/rassphrase/rassphrase_spec.rb
67
+ - spec/rassphrase/wordlist_parser_spec.rb
68
+ - spec/spec_helper.rb
69
+ homepage: https://github.com/guzart/rassphrase
70
+ licenses: []
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project: rassphrase
89
+ rubygems_version: 1.8.11
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: A Ruby implementation of Diceware passphrase
93
+ test_files:
94
+ - features/diceware.feature
95
+ - features/generate_passphrase.feature
96
+ - features/roll_dice.feature
97
+ - features/step_definitions/diceware_steps.rb
98
+ - features/step_definitions/generate_passphrase_steps.rb
99
+ - features/step_definitions/roll_dice.rb
100
+ - features/support/env.rb
101
+ - spec/rassphrase/dice_spec.rb
102
+ - spec/rassphrase/rassphrase_spec.rb
103
+ - spec/rassphrase/wordlist_parser_spec.rb
104
+ - spec/spec_helper.rb