passphrase 0.0.2 → 0.0.3

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.
@@ -4,9 +4,8 @@ require 'passphrase/generator'
4
4
 
5
5
  p = Passphrase::Generator.new(ARGV)
6
6
  p.run
7
- if p.odd_char
8
- puts "unmixed phrase => #{p.unmixed_phrase}"
9
- puts "odd character => #{p.odd_char}"
7
+ if p.mix?
8
+ puts "unmixed phrase => #{p.unmixed_phrase}"
10
9
  puts "passphrase => #{p.phrase}"
11
10
  else
12
11
  puts "passphrase => #{p.phrase}"
@@ -7,46 +7,74 @@ require File.join(File.dirname(__FILE__), 'random')
7
7
  module Passphrase
8
8
  class Generator
9
9
 
10
- attr_reader :num_words
11
- attr_reader :odd_char
10
+ NON_ALPHANUM_REGEX = /[~!#\$%\^&\*\(\)\-=\+\[\]\\\{\}:;"'<>\?\/]/
11
+ NON_ALPHANUM_ARRAY = %w( ~ ! # $ % ^ & * \( \) - = + [ ] \\ { } : ; " ' < > ? / )
12
+
12
13
  attr_reader :phrase
13
14
  attr_reader :unmixed_phrase
14
- attr_reader :words
15
15
 
16
16
  def initialize(argv)
17
17
  @options = Options.new(argv)
18
- @num_words = @options.num_words
19
18
  @words = []
20
19
  end
21
20
 
22
21
  def run
22
+ num_words = @options.num_words
23
23
  word_list = WordList.create
24
- list_selector = Random.new(@num_words, 0, word_list.length - 1).rand_array
25
- @num_words.times do |iword|
26
- word_hash = Random.new(5, 1, 6).rand_array.join.to_sym
24
+ list_selector = Random.new(num_words, 0, word_list.length - 1).to_array
25
+ num_words.times do |iword|
26
+ word_hash = Random.new(5, 1, 6).to_array.join.to_sym
27
27
  @words << word_list[list_selector[iword]][word_hash]
28
28
  end
29
- @phrase = @unmixed_phrase = @words.join(' ')
30
- mix_odd_char if @options.mix
29
+ @unmixed_phrase = @words.join(" ")
30
+ @phrase = @unmixed_phrase.clone
31
+ mix_phrase if mix?
32
+ end
33
+
34
+ def mix?
35
+ @options.mix
31
36
  end
32
37
 
33
38
  private
34
39
 
35
- def mix_odd_char
36
- odd =
37
- %w( ~ & + : ? 4 ) +
38
- %w( ! * [ ; / 5 ) +
39
- %w( # \( ] " 0 6 ) +
40
- %w( $ \) \\ ' 1 7 ) +
41
- %w( % - { < 2 8 ) +
42
- %w( ^ = } > 3 9 )
43
- odd_char_index = Random.new(1, 0, odd.length - 1).rand_array.shift
44
- word_index = Random.new(1, 0, @num_words - 1).rand_array.shift
45
- word_length = @words[word_index].length
46
- char_index = Random.new(1, 0, word_length - 1).rand_array.shift unless word_length.zero?
47
- char_index ||= 0
48
- @words[word_index][char_index] = @odd_char = odd[odd_char_index]
49
- @phrase = @words.join(' ')
40
+ def mix_phrase
41
+ mixin_capital unless @phrase =~ /[A-Z]/
42
+ mixin_number unless @phrase =~ /\d/
43
+ mixin_nonalphanum unless @phrase =~ NON_ALPHANUM_REGEX
44
+ end
45
+
46
+ def mixin_capital
47
+ index, character = make_character_selection
48
+ @phrase[index] = character.upcase
49
+ end
50
+
51
+ def mixin_number
52
+ index, character = make_character_selection
53
+ numbers = ("0".."9").to_a
54
+ number = numbers[one_random_number(numbers.length - 1)]
55
+ @phrase[index] = number
56
+ end
57
+
58
+ def mixin_nonalphanum
59
+ index, character = make_character_selection
60
+ len = NON_ALPHANUM_ARRAY.length
61
+ non_alphanum = NON_ALPHANUM_ARRAY[one_random_number(len - 1)]
62
+ @phrase[index] = non_alphanum
63
+ end
64
+
65
+ def make_character_selection
66
+ return [] unless @phrase =~ /[a-z]/
67
+ index = one_random_number(@phrase.length - 1)
68
+ character = @phrase.slice(index, 1)
69
+ until character =~ /[a-z]/
70
+ index = index.succ.modulo(@phrase.length)
71
+ character = @phrase.slice(index, 1)
72
+ end
73
+ [index, character]
74
+ end
75
+
76
+ def one_random_number(max)
77
+ Random.new(1, 0, max).to_array.shift
50
78
  end
51
79
  end
52
80
  end
@@ -29,14 +29,14 @@ module Passphrase
29
29
  "Desired number of words (#{NUM_WORDS_RANGE.to_s}), default #{DEFAULT_NUM_WORDS}") do |num|
30
30
  @num_words = num
31
31
  end
32
- opts.on("-x", "--[no-]mix", "Mix in odd character, default mix") do |m|
32
+ opts.on("-x", "--[no-]mix", "Mix in cap, num, non-alphanum, default mix") do |m|
33
33
  @mix = m
34
34
  end
35
- opts.on_tail("-h", "--help", "Show this message") do
35
+ opts.on_tail("-h", "--help", "Show this message and exit") do
36
36
  puts opts
37
37
  exit
38
38
  end
39
- opts.on_tail("-v", "--version", "Show version") do
39
+ opts.on_tail("-v", "--version", "Show version and exit") do
40
40
  puts "#{File.basename($PROGRAM_NAME)}, version #{Passphrase::Version::STRING}"
41
41
  exit
42
42
  end
@@ -6,35 +6,35 @@ require 'securerandom'
6
6
  module Passphrase
7
7
  class Random
8
8
 
9
- RANDOM_ORG = "www.random.org"
10
-
11
- attr_reader :num
12
- attr_reader :min
13
- attr_reader :max
14
- attr_reader :via_random_org
15
- attr_reader :rand_array
16
-
17
9
  def initialize(num, min, max)
18
10
  @num, @min, @max = num, min, max
19
- @rand_array = []
20
- generate_rand_array
11
+ @array_of_rands = []
12
+ generate_array_of_rands
13
+ end
14
+
15
+ def via_random_org?
16
+ @via_random_org
17
+ end
18
+
19
+ def to_array
20
+ @array_of_rands
21
21
  end
22
22
 
23
23
  private
24
24
 
25
- def generate_rand_array
25
+ def generate_array_of_rands
26
26
  query = "/integers/?col=1&base=10&format=plain&rnd=new" +
27
27
  "&num=#{@num}&min=#{@min}&max=#{@max}"
28
- site = Net::HTTP.new(RANDOM_ORG)
28
+ site = Net::HTTP.new("www.random.org")
29
29
  response, data = site.get(query)
30
30
  raise unless response.code == "200"
31
- @rand_array = data.split.collect {|num| num.to_i}
31
+ @array_of_rands = data.split.collect {|num| num.to_i}
32
32
  @via_random_org = true
33
33
  rescue
34
34
  max = @max - @min + 1
35
35
  offset = @min
36
36
  @num.times do
37
- @rand_array << (SecureRandom.random_number(max) + offset)
37
+ @array_of_rands << (SecureRandom.random_number(max) + offset)
38
38
  end
39
39
  @via_random_org = false
40
40
  end
@@ -7,7 +7,7 @@ module Passphrase
7
7
  # http://docs.rubygems.org/read/chapter/7
8
8
  MAJOR = 0
9
9
  MINOR = 0
10
- BUILD = 2
10
+ BUILD = 3
11
11
 
12
12
  STRING = [MAJOR, MINOR, BUILD].compact.join('.')
13
13
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: passphrase
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.2
5
+ version: 0.0.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Edmund Sumbar
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-01-22 00:00:00 -07:00
13
+ date: 2011-03-12 00:00:00 -07:00
14
14
  default_executable: passphrase
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency