ruby-diceware 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,3 @@
1
+ #!/usr/bin/env ruby
2
+ require 'ruby-dice/cli'
3
+ RubyDice::CLI.start
@@ -0,0 +1,3 @@
1
+ require 'ruby-dice/version'
2
+ require 'ruby-dice/wordlist'
3
+ require 'ruby-dice/passphrase'
@@ -0,0 +1,22 @@
1
+ require 'thor'
2
+ require 'ruby-dice'
3
+
4
+ module RubyDice
5
+ class CLI < Thor
6
+ desc 'throw', 'Generate and copy a passphrase'
7
+ option :length, type: :numeric, default: 5, aliases: :l, desc: 'Amount of words to use'
8
+ option :print, type: :boolean, default: false, aliases: :p, desc: 'Output passphrase to terminal'
9
+ option :wordlist, type: :string, default: nil, aliases: :w, desc: 'Use a custom wordlist file', banner: 'filename'
10
+ def throw
11
+ passphrase_options = {}.tap do |o|
12
+ o[:words] = options['length']
13
+ o[:wordlist] = options['wordlist']
14
+ end
15
+
16
+ passphrase = RubyDice::Passphrase.generate passphrase_options
17
+ IO.popen('xsel --clipboard --input', 'r+') { |c| c.print passphrase }
18
+ puts passphrase if options['print']
19
+ end
20
+ default_task :throw
21
+ end
22
+ end
@@ -0,0 +1,14 @@
1
+ module RubyDice
2
+ class Passphrase
3
+ def self.generate(options = {})
4
+ default_options = { words: 5 }
5
+ options = default_options.merge(options)
6
+
7
+ wordlist_options = {}
8
+ wordlist_options[:wordlist] = options.delete(:wordlist)
9
+
10
+ wordlist = Wordlist.new(wordlist_options)
11
+ wordlist.random(options[:words]).join(' ')
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module RubyDice
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,44 @@
1
+ require 'securerandom'
2
+
3
+ module RubyDice
4
+ class Wordlist
5
+ attr_reader :words
6
+
7
+ # Damn ugly... lets refactor sometime
8
+ def initialize(filename_or_options = nil)
9
+ options = {}
10
+ filename = nil
11
+
12
+ if filename_or_options.is_a?(Hash)
13
+ options = filename_or_options
14
+ filename = options[:wordlist]
15
+ else
16
+ filename = filename_or_options
17
+ end
18
+
19
+ filename ||= 'diceware.wordlist.asc'
20
+ filename += '.txt' if File.extname(filename) == ''
21
+
22
+ search_paths = [
23
+ File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'assets')),
24
+ File.join(Dir.home, '.wordlists')
25
+ ]
26
+
27
+ # Too convoluted?
28
+ unless full_path = (File.exists?(filename) ? filename : nil)
29
+ path = search_paths.detect { |path| File.exists? File.join(path, filename) }
30
+ full_path = File.join(path, filename)
31
+ end
32
+
33
+ File.open(full_path, 'r:UTF-8') { |f| @words = f.read }
34
+ @words = @words.match(/(11111.+66666.+?)\n/m)[1].split("\n").map { |l| l.split(/[\t ]/)[1].strip }
35
+ end
36
+
37
+ def random(count = 5)
38
+ (1..count).inject([]) do |selected|
39
+ random = SecureRandom.random_number(@words.size)
40
+ selected << @words[random]
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ruby-dice/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'ruby-diceware'
8
+ spec.version = RubyDice::VERSION
9
+ spec.authors = ['Ivan Vega']
10
+ spec.email = ['']
11
+ spec.summary = %q{Ruby Diceware passphrase generator}
12
+ spec.description = %q{Simple Ruby script to generate "Diceware" passphrases.}
13
+ spec.homepage = 'https://github.com/ivanyv/ruby-dice'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'thor'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.3'
24
+ spec.add_development_dependency 'rake'
25
+ spec.add_development_dependency 'rspec', '~> 2.6'
26
+ end
@@ -0,0 +1,5 @@
1
+ 11111 mary
2
+ 22222 had
3
+ 33333 a
4
+ 44444 little
5
+ 66666 lamb
@@ -0,0 +1,42 @@
1
+ require 'ruby-dice'
2
+
3
+ describe RubyDice::Passphrase do
4
+ it 'returns a passphrase' do
5
+ passphrase = RubyDice::Passphrase.generate(words: 5)
6
+ passphrase.split(' ').size.should eql(5)
7
+ end
8
+
9
+ it 'returns a passphrase from a custom wordlist' do
10
+ file = File.join(File.dirname(__FILE__), 'fixtures', 'five')
11
+ passphrase = RubyDice::Passphrase.generate(wordlist: file)
12
+ passphrase.split(' ').size.should eql(5)
13
+ end
14
+ end
15
+
16
+ describe RubyDice::Wordlist do
17
+ context 'with the default wordlist' do
18
+ let(:wordlist) { RubyDice::Wordlist.new }
19
+
20
+ it 'loads the default wordlist' do
21
+ wordlist.words.size.should eql(7776)
22
+ end
23
+
24
+ it 'returns a random list of words' do
25
+ wordlist.random(5).should_not eql(wordlist.random(5))
26
+ end
27
+
28
+ it 'returns the specified amount of words' do
29
+ wordlist.random(7).size.should eql(7)
30
+ end
31
+ end
32
+
33
+ context 'with a custom wordlist' do
34
+ it 'returns a random list of words' do
35
+ file = File.join(File.dirname(__FILE__), 'fixtures', 'five')
36
+ wordlist = RubyDice::Wordlist.new(file)
37
+ wordlist.random(5).size.should eql(5)
38
+ wordlist.words.size.should eql(5)
39
+ (wordlist.words - %w[mary had a little lamb]).should eql([])
40
+ end
41
+ end
42
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-diceware
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ivan Vega
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: thor
16
+ type: :runtime
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ! '>='
27
+ - !ruby/object:Gem::Version
28
+ version: '0'
29
+ prerelease: false
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ type: :development
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: '1.3'
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: '1.3'
45
+ prerelease: false
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ type: :development
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ version_requirements: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ prerelease: false
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ type: :development
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ~>
69
+ - !ruby/object:Gem::Version
70
+ version: '2.6'
71
+ version_requirements: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: '2.6'
77
+ prerelease: false
78
+ description: Simple Ruby script to generate "Diceware" passphrases.
79
+ email:
80
+ - ''
81
+ executables:
82
+ - dice
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - Gemfile
88
+ - README.md
89
+ - Rakefile
90
+ - assets/dice.jpg
91
+ - assets/diceware.wordlist.asc
92
+ - bin/dice
93
+ - lib/ruby-dice.rb
94
+ - lib/ruby-dice/cli.rb
95
+ - lib/ruby-dice/passphrase.rb
96
+ - lib/ruby-dice/version.rb
97
+ - lib/ruby-dice/wordlist.rb
98
+ - ruby-dice.gemspec
99
+ - spec/fixtures/five.txt
100
+ - spec/ruby-dice_spec.rb
101
+ homepage: https://github.com/ivanyv/ruby-dice
102
+ licenses:
103
+ - MIT
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ segments:
115
+ - 0
116
+ hash: 3989073177033128539
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ segments:
124
+ - 0
125
+ hash: 3989073177033128539
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 1.8.23
129
+ signing_key:
130
+ specification_version: 3
131
+ summary: Ruby Diceware passphrase generator
132
+ test_files:
133
+ - spec/fixtures/five.txt
134
+ - spec/ruby-dice_spec.rb