tokogen 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 02fc56804395ffd4dd1fc6ea69dcfd1e7eb139f3
4
+ data.tar.gz: 5acb5afe794ea5e25772d6814b5e120373beaa7a
5
+ SHA512:
6
+ metadata.gz: 58f0a285af9ffd681177fe4781a3a30aacf441c3d20ebfbe35cd49e024ea22fbb91fbb349df3672a7b1179c71022dad253d8f32de0249f371eb13f187b50cbf9
7
+ data.tar.gz: 0d8084452c90d40adaecf855a6edb4cab771857fabe226743e9c77ccee4502901bb0df12d7c111be70230782e7066121d862094e5ff181ef3ad99e9b579e7119
data/.editorconfig ADDED
@@ -0,0 +1,9 @@
1
+ root = true
2
+
3
+ [*]
4
+ indent_style = space
5
+ indent_size = 2
6
+ end_of_line = lf
7
+ charset = utf-8
8
+ trim_trailing_whitespace = true
9
+ insert_final_newline = true
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,38 @@
1
+ inherit_from: .rubocop_todo.yml
2
+ AllCops:
3
+ TargetRubyVersion: 2.3
4
+ Include:
5
+ - '**/*.gemspec'
6
+ - '**/Gemfile'
7
+ - '**/Rakefile'
8
+ - '**/*.rake'
9
+ Rails:
10
+ Enabled: false
11
+ Metrics/LineLength:
12
+ Enabled: false
13
+ Documentation:
14
+ Enabled: false
15
+ Style/ClassAndModuleChildren:
16
+ Enabled: false
17
+ Style/ClassCheck:
18
+ Enabled: false
19
+ Style/SignalException:
20
+ Enabled: false
21
+ Style/IndentArray:
22
+ EnforcedStyle: consistent
23
+ Style/IndentHash:
24
+ EnforcedStyle: consistent
25
+ Style/FirstParameterIndentation:
26
+ EnforcedStyle: consistent
27
+ Style/CaseIndentation:
28
+ IndentWhenRelativeTo: end
29
+ Style/Alias:
30
+ EnforcedStyle: prefer_alias_method
31
+ Lint/EndAlignment:
32
+ AlignWith: variable
33
+ Style/MultilineMethodCallIndentation:
34
+ EnforcedStyle: indented
35
+ Style/SpaceInLambdaLiteral:
36
+ Enabled: false
37
+ Bundler/OrderedGems:
38
+ Enabled: false
data/.rubocop_todo.yml ADDED
File without changes
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.6
6
+ install:
7
+ - bundle install --retry=3
8
+ - gem install rubocop
9
+ script:
10
+ - rubocop -D
11
+ - bundle exec rake
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ source 'https://rubygems.org'
3
+
4
+ # Specify your gem's dependencies in tokogen.gemspec
5
+ gemspec
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # Tokogen
2
+
3
+ A ruby gem that allows you to generate customizeable random tokens.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'tokogen'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install tokogen
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ Tokogen.token
25
+ ```
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/MOZGIII/tokogen.
36
+
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+ require 'bundler/gem_tasks'
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'tokogen'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+ module Tokogen
3
+ class Generator
4
+ DEFAULT_ALPHABET = (('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a).join.freeze
5
+
6
+ attr_reader :randomness_source
7
+
8
+ def initialize(randomness_source:, alphabet: DEFAULT_ALPHABET)
9
+ @randomness_source = randomness_source
10
+ @alphabet = alphabet
11
+ end
12
+
13
+ def generate(length)
14
+ token_bits_amount = length * bits_per_char
15
+ bytes_to_read = full_bytes_in_bits(token_bits_amount)
16
+ bytes = random_bytes(bytes_to_read)
17
+ bits = bytes.unpack('b*')[0]
18
+ # It's possible we've read a couple exta bits of randomness,
19
+ # since randomness is rounded to bytes.
20
+ # Here we only take first `length` of bit that we need.
21
+ bit_string_split(bits, bits_per_char)
22
+ .take(length)
23
+ .map { |index| alphabet_char(index) }
24
+ .join
25
+ end
26
+
27
+ def random_bytes(size)
28
+ @randomness_source.random_bytes(size)
29
+ end
30
+
31
+ def max_char_index
32
+ @alphabet.size - 1
33
+ end
34
+
35
+ def bits_per_char
36
+ max_char_index.bit_length
37
+ end
38
+
39
+ def full_bytes_in_bits(bits)
40
+ (bits + 7) >> 3
41
+ end
42
+
43
+ private
44
+
45
+ def bit_string_split(bits, bits_per_char, &block) # rubocop:disable Metrics/MethodLength
46
+ top = max_char_index
47
+ curry = 0
48
+ last_curry = 0
49
+ bits.each_char.each_slice(bits_per_char).map do |binary_ord|
50
+ val = binary_ord.join.to_i(2) + curry
51
+ last_curry = curry
52
+ if val <= top
53
+ current = val
54
+ curry = 0
55
+ else
56
+ current = top
57
+ curry = val % top
58
+ end
59
+ current
60
+ end.each(&block)
61
+ end
62
+
63
+ def alphabet_char(index)
64
+ @alphabet[index]
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ module Tokogen
3
+ VERSION = '0.1.1'
4
+ end
data/lib/tokogen.rb ADDED
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+ require 'securerandom'
3
+ require 'tokogen/version'
4
+ require 'tokogen/generator'
5
+
6
+ module Tokogen
7
+ def self.token(length = 32)
8
+ default_generator.generate(length)
9
+ end
10
+
11
+ def self.default_generator
12
+ generator # calling explicitly without arguments
13
+ end
14
+
15
+ def self.generator(randomness_source: SecureRandom, **options)
16
+ Generator.new(randomness_source: randomness_source, **options)
17
+ end
18
+ end
data/tokogen.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'tokogen/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'tokogen'
9
+ spec.version = Tokogen::VERSION
10
+ spec.authors = ['MOZGIII']
11
+ spec.email = ['mike-n@narod.ru']
12
+
13
+ spec.summary = 'Generates random tokens with fixed length and customizeable alphabet.'
14
+ spec.description = 'A ruby gem that allows you to generate customizeable random tokens.'
15
+ spec.homepage = 'https://github.com/MOZGIII/tokogen'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.13'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec', '~> 3.0'
27
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tokogen
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - MOZGIII
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-11 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
+ description: A ruby gem that allows you to generate customizeable random tokens.
56
+ email:
57
+ - mike-n@narod.ru
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".editorconfig"
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".rubocop.yml"
66
+ - ".rubocop_todo.yml"
67
+ - ".travis.yml"
68
+ - Gemfile
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - lib/tokogen.rb
74
+ - lib/tokogen/generator.rb
75
+ - lib/tokogen/version.rb
76
+ - tokogen.gemspec
77
+ homepage: https://github.com/MOZGIII/tokogen
78
+ licenses: []
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.6.8
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Generates random tokens with fixed length and customizeable alphabet.
100
+ test_files: []