nanoid 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/nanoid.rb +45 -0
  3. data/lib/nanoid/version.rb +3 -0
  4. metadata +115 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0b4bc9b383a529f747be9817cd0c3c6991b4f23a
4
+ data.tar.gz: c78ba7699645c46b0e54ffde520946b42a9ae241
5
+ SHA512:
6
+ metadata.gz: 88ebff8d8b2785e0e1102b25d173f20bc57605124bb3a6227ac202ed84f3ebb17360d6ab0648ad638326fd58bd8c44637eb87acd9aa0e702a37d7d3ac1a5219e
7
+ data.tar.gz: c5524dcda652475be03f045c4c0f41ffbac0ed7335fa1bafe8e376a09e996e24547fbe6d67339213aacccdc8bff8f0b8ae022dfa36901ef551437587b5eeea6d
@@ -0,0 +1,45 @@
1
+ require 'securerandom'
2
+
3
+ module Nanoid
4
+ MASKS = [15, 31, 63, 127, 255].freeze
5
+ SAFE_ALPHABET = '_~0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.freeze
6
+
7
+ def self.generate(size: 22, alphabet: SAFE_ALPHABET)
8
+ return simple_generate(size: size) if alphabet == SAFE_ALPHABET
9
+
10
+ complex_generate(size: size, alphabet: alphabet)
11
+ end
12
+
13
+ def self.simple_generate(size:)
14
+ bytes = random_bytes(size)
15
+
16
+ (0...size).inject('') do |acc, i|
17
+ acc + SAFE_ALPHABET[bytes[i] & 63]
18
+ end
19
+ end
20
+
21
+ def self.complex_generate(size:, alphabet:)
22
+ mask = MASKS.find { |i| i >= alphabet.size - 1 }
23
+ step = (1.6 * mask * size / alphabet.size).ceil
24
+
25
+ id = ''
26
+ catch (:computed) do
27
+ loop do
28
+ bytes = random_bytes(size)
29
+ (0...step).each do |i|
30
+ byte = bytes[i] & mask
31
+ if byte && alphabet[byte]
32
+ id += alphabet[byte]
33
+ throw :computed if id.size == size
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ id
40
+ end
41
+
42
+ def self.random_bytes(size)
43
+ SecureRandom.random_bytes(size).bytes
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module NanoId
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nanoid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Radovan Smitala
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-09-16 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: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: reek
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - lib/nanoid.rb
90
+ - lib/nanoid/version.rb
91
+ homepage: https://github.com/radeno/nanoid.rb
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '2.0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.6.11
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Ruby implementation of Nanoid, secure URL-friendly unique ID generator
115
+ test_files: []