ibsenphrase 0.1.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,41 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ibsenphrase/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ibsenphrase"
8
+ spec.version = Ibsenphrase::VERSION
9
+ spec.authors = ["Sigve Indregard"]
10
+ spec.email = ["sigve@indregard.no"]
11
+
12
+ spec.summary = "Generates diceware passphrases with words from Ibsen."
13
+ spec.description = "Passphrases with random words picked from a large wordlist are safer and easier to remember than random characters. This gem lets you create such passphrases with words from harvested from the writings of playwright Henrik Ibsen."
14
+ spec.homepage = "https://github.com/sigvei/ibsenphrase"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ spec.metadata['yard_run'] = 'yri'
22
+ else
23
+ raise "RubyGems 2.0 or newer is required to protect against " \
24
+ "public gem pushes."
25
+ end
26
+
27
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
28
+ f.match(%r{^(test|spec|features)/})
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ spec.add_development_dependency "bundler", "~> 1.13"
35
+ spec.add_development_dependency "rake", "~> 10.0"
36
+ spec.add_development_dependency "rspec", "~> 3.0"
37
+ spec.add_development_dependency "byebug", "~> 9.0"
38
+ spec.add_development_dependency "yard", "~> 0.9"
39
+
40
+ spec.add_dependency 'unicode', '~> 0.4'
41
+ end
@@ -0,0 +1,55 @@
1
+ require 'unicode'
2
+
3
+ module Ibsenphrase
4
+ # A generator for passphrases
5
+ class Generator
6
+ # Generates a passphrase of words
7
+ # @num_words [Integer] Number of words in passphrase
8
+ # @opts (see #initialize)
9
+ def self.passphrase(num_words=7, opts={})
10
+ gen = Generator.new(opts)
11
+ gen.passphrase(num_words)
12
+ end
13
+
14
+ # Initialize a generator instance
15
+ #
16
+ # @option opts [String] :wordlist 'words' The wordlist (in data/wordslists)
17
+ # @option opts [Symbol] :separation ':space' The type of separation, either
18
+ # :space (for lists like this: 'word word word') or :capitalize (
19
+ # WordWordWord).
20
+ def initialize(opts={})
21
+ default_options = { wordlist: 'words', separation: :space }
22
+ @options = default_options.merge(opts)
23
+
24
+ unless [:space, :capitalize].include?(@options[:separation])
25
+ raise ArgumentError, 'option :separation must be :space or :capitalize'
26
+ end
27
+
28
+ @wordlist = Wordlist.new(@options[:wordlist])
29
+ end
30
+
31
+ # Generate a passphrase, that is, a sequence of words.
32
+ # Please note that the method generates a new passphrase each time it
33
+ # is called.
34
+ #
35
+ # Whether to use space-separated or capitalized passphrases is determined
36
+ # by options passed.
37
+ #
38
+ # @param num_words [Integer] Number of words in passphrase
39
+ # @return [String] The passphrase
40
+ def passphrase(num_words=7)
41
+ unless num_words.is_a?(Numeric) && num_words.to_i > 0
42
+ raise ArgumentError, 'num_words must be integer > 0'
43
+ end
44
+
45
+ words = Array.new(num_words).map{ @wordlist.draw_word }
46
+
47
+ if @options[:separation] == :space
48
+ words.join(' ')
49
+ else
50
+ words.map{|w| Unicode::capitalize(w)}.join
51
+ end
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,12 @@
1
+ module Ibsenphrase
2
+ # This encapsulates the pseudo-random number generator (PRNG), essentially
3
+ # turning it into a singleton class. This is done to avoid instantiating a
4
+ # new PRNG every time we need it, as that would make us dependent on the
5
+ # randomness of the operating system's seed, instead of the random number
6
+ # generator code in Ruby.
7
+ class Random
8
+ def self.prng
9
+ @@rng ||= Random.new
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module Ibsenphrase
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,38 @@
1
+ module Ibsenphrase
2
+ class Wordlist
3
+ # A new wordlist object. It loads the file from disk on instantiation.
4
+ # @param filename [String] The filename without path. The file is looked
5
+ # for in data/wordlists.
6
+ def initialize(filename)
7
+ @words = []
8
+ load_words(filename)
9
+ end
10
+
11
+ # Picks a random word from the wordlist
12
+ #
13
+ # @return [String] A word
14
+ def draw_word
15
+ @words.sample(random: Ibsenphrase::Random.prng)
16
+ end
17
+
18
+ # @return [Array<String>] All the words in the wordlist
19
+ def words
20
+ @words
21
+ end
22
+
23
+ # @return [Integer] Number of words in wordlist
24
+ def num_words
25
+ @words.size
26
+ end
27
+
28
+ private
29
+
30
+ def load_words(filename)
31
+ path = File.join(Ibsenphrase.root, 'data', 'wordlists', filename)
32
+
33
+ File.foreach path do |l|
34
+ @words << l.strip
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,12 @@
1
+ require "ibsenphrase/version"
2
+ require "ibsenphrase/generator"
3
+ require "ibsenphrase/wordlist"
4
+ require "ibsenphrase/random"
5
+
6
+ module Ibsenphrase
7
+ class << self
8
+ def root
9
+ @@ROOT ||= File.expand_path(File.join(File.dirname(__FILE__), '..'))
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ require 'unicode'
2
+
3
+ words = []
4
+ line = 0
5
+ File.foreach(ARGV[0]) do |l|
6
+ line = line + 1
7
+ orig = l
8
+ l = Unicode::downcase l
9
+ l.gsub!(/[^a-zæøå]/, ' ')
10
+ l.strip!
11
+ l.split.each do |w|
12
+ if w == "ngden"
13
+ STDERR.puts "#{line}: ngden: #{orig}"
14
+ end
15
+ words << w if w.length > 1
16
+ end
17
+ end
18
+
19
+ puts words.uniq.sort.join "\n"
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ibsenphrase
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Sigve Indregard
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-09-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '9.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '9.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: yard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.9'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.9'
83
+ - !ruby/object:Gem::Dependency
84
+ name: unicode
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.4'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.4'
97
+ description: Passphrases with random words picked from a large wordlist are safer
98
+ and easier to remember than random characters. This gem lets you create such passphrases
99
+ with words from harvested from the writings of playwright Henrik Ibsen.
100
+ email:
101
+ - sigve@indregard.no
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - ".gitignore"
107
+ - ".rspec"
108
+ - ".travis.yml"
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - bin/setup
115
+ - data/wordlists/originaler.txt
116
+ - data/wordlists/test
117
+ - data/wordlists/words
118
+ - ibsenphrase.gemspec
119
+ - lib/ibsenphrase.rb
120
+ - lib/ibsenphrase/generator.rb
121
+ - lib/ibsenphrase/random.rb
122
+ - lib/ibsenphrase/version.rb
123
+ - lib/ibsenphrase/wordlist.rb
124
+ - util/make-words.rb
125
+ homepage: https://github.com/sigvei/ibsenphrase
126
+ licenses:
127
+ - MIT
128
+ metadata:
129
+ allowed_push_host: https://rubygems.org
130
+ yard_run: yri
131
+ post_install_message:
132
+ rdoc_options: []
133
+ require_paths:
134
+ - lib
135
+ required_ruby_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ requirements: []
146
+ rubyforge_project:
147
+ rubygems_version: 2.5.1
148
+ signing_key:
149
+ specification_version: 4
150
+ summary: Generates diceware passphrases with words from Ibsen.
151
+ test_files: []