random_token 1.0.2 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/HISTORY.md +5 -1
- data/README.md +39 -1
- data/lib/random_token.rb +26 -6
- data/lib/random_token/random_token_error.rb +8 -0
- data/lib/random_token/version.rb +1 -1
- data/test/length/test_byte.rb +63 -0
- data/test/length/test_seed.rb +36 -36
- data/test/test_gen.rb +1 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7f1a3de562e161040be47ea06dc98cbdd4687761
|
4
|
+
data.tar.gz: f2b873c14adfaba4e1e93997e39ed24eb979f30f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d324f84c6c13fe3c1273762bdcc8eab135745437eb2305d35e222acf72d8d3e5adabdc36adbfd174787173559a4c6b63f58386949eaddfb85a642b815d76b7d1
|
7
|
+
data.tar.gz: 1f62569a4c40fb6af308de659cdd17f1d93772556bb65750fe785006c2819d9c192c6357e86bf9bb92da2993a9e982f6e94d78a476f99a2d59820a26cc418507
|
data/HISTORY.md
CHANGED
@@ -1,4 +1,8 @@
|
|
1
|
-
|
1
|
+
## v1.1.0 / 2014-10-01
|
2
|
+
|
3
|
+
* [Feature] Support the byte-length mode
|
4
|
+
|
5
|
+
## v1.0.2 / 2014-09-30
|
2
6
|
|
3
7
|
* [Dependence] Use new unit test syntex for testing, require minitest ~> 5.0
|
4
8
|
* [Change] Use "SecureRandom.random_number" instead of "rand"
|
data/README.md
CHANGED
@@ -191,6 +191,44 @@ With :hex seed, i.e, `s: :h`, the default case is changed to upper-case. You can
|
|
191
191
|
RandomToken.gen(32, s: :h, c: :d)
|
192
192
|
# "d331ce7dae87f3bb3dcb3975be9c430d"
|
193
193
|
|
194
|
+
### Byte-Length
|
195
|
+
|
196
|
+
Use `:b` (or `:byte`) option to enable byte-length mode. When byte-length mode is on, the length unit becomes "bytes". For example:
|
197
|
+
|
198
|
+
RandomToken.gen(32, s: :h)
|
199
|
+
# "066010B727E0619DF90635DBCFBBC9CA"
|
200
|
+
# length = 32
|
201
|
+
|
202
|
+
RandomToken.gen(32, s: :h, b: true)
|
203
|
+
# "7D8FC8241CB1E5EE32BFB61212275733485B73F2B25F61F3684411BC77ECE331"
|
204
|
+
# length = 32 * 2
|
205
|
+
|
206
|
+
RandomToken.gen(32, s: :o)
|
207
|
+
# "33660161373115273571641666235250"
|
208
|
+
# length = 32
|
209
|
+
|
210
|
+
RandomToken.gen(32, s: :o, b: true)
|
211
|
+
# "13247642533766132340045425736322366461201703655601042333031121363007327500236255017153153410577614717424243614637434623101413176"
|
212
|
+
# length = 32 * 4
|
213
|
+
|
214
|
+
RandomToken.gen(32, s: :b)
|
215
|
+
# "10110010010011001101011111001011"
|
216
|
+
# length = 32
|
217
|
+
|
218
|
+
RandomToken.gen(32, s: :b, b: true)
|
219
|
+
# "00110111000100101100001011010001100100101110011001101000101110110001110111110100010001000011110101011011001001101001001100110110000100111110100011100101101011010111100011001100000010101111000010111101000000110101001100110011011000000111101100110101101111100000000111110000000010010100010110101010001010111010101000100101010100110001101000111110110110111011011111100111000000010010110110011111101110101001101101101001101011111010100101100110100100100100010100110111100010010010100010010000111000010000000110100101"
|
220
|
+
# length = 32 * 16
|
221
|
+
|
222
|
+
Only `:binary`, `:oct`, `:hex` seeds support the byte-length mode.
|
223
|
+
|
224
|
+
RandomToken.gen(32, b: true)
|
225
|
+
RandomToken::RandomTokenError: RandomToken::RandomTokenError
|
226
|
+
|
227
|
+
The byte-length mode is not supported by format either.
|
228
|
+
|
229
|
+
RandomToken.gen("%32h", b: true)
|
230
|
+
RandomToken::RandomTokenError: RandomToken::RandomTokenError
|
231
|
+
|
194
232
|
## Use Cases
|
195
233
|
|
196
234
|
### Case 1
|
@@ -235,7 +273,7 @@ Uh, I don't think it is cool enough...
|
|
235
273
|
|
236
274
|
## Test
|
237
275
|
|
238
|
-
Go to random_token gem folder and run
|
276
|
+
Go to random_token gem folder and run (Note that you need minitest ~> 5.0 to run these tests)
|
239
277
|
|
240
278
|
ruby ./test/test_all.rb
|
241
279
|
|
data/lib/random_token.rb
CHANGED
@@ -28,11 +28,13 @@ module RandomToken
|
|
28
28
|
:seed => [NUMBERS, ALPHABETS],
|
29
29
|
:support_friendly => true,
|
30
30
|
:support_case => true,
|
31
|
-
:default_case => :mixed
|
31
|
+
:default_case => :mixed,
|
32
|
+
:support_byte => false,
|
33
|
+
:byte_mult => 1
|
32
34
|
},
|
33
35
|
:alphabet => {
|
34
36
|
:abbr => [:letter, :a, :l],
|
35
|
-
:seed => [ALPHABETS]
|
37
|
+
:seed => [ALPHABETS]
|
36
38
|
},
|
37
39
|
:number => {
|
38
40
|
:abbr => [:n, 1, 10],
|
@@ -43,19 +45,25 @@ module RandomToken
|
|
43
45
|
:abbr => [:b, 2],
|
44
46
|
:seed => [('0'..'1')],
|
45
47
|
:support_case => false,
|
46
|
-
:support_friendly => false
|
48
|
+
:support_friendly => false,
|
49
|
+
:support_byte => true,
|
50
|
+
:byte_mult => 16
|
47
51
|
},
|
48
52
|
:oct => {
|
49
53
|
:abbr => [:o, 8],
|
50
54
|
:seed => [('0'..'7')],
|
51
55
|
:support_case => false,
|
52
|
-
:support_friendly => false
|
56
|
+
:support_friendly => false,
|
57
|
+
:support_byte => true,
|
58
|
+
:byte_mult => 4
|
53
59
|
},
|
54
60
|
:hex => {
|
55
61
|
:abbr => [:h, 16],
|
56
62
|
:seed => [NUMBERS, ('A'..'F')],
|
57
63
|
:support_friendly => false,
|
58
|
-
:default_case => :up
|
64
|
+
:default_case => :up,
|
65
|
+
:support_byte => true,
|
66
|
+
:byte_mult => 2
|
59
67
|
}
|
60
68
|
}
|
61
69
|
|
@@ -74,6 +82,10 @@ module RandomToken
|
|
74
82
|
:friendly => {
|
75
83
|
:abbr => :f,
|
76
84
|
:value => [true, false]
|
85
|
+
},
|
86
|
+
:byte => {
|
87
|
+
:abbr => :b,
|
88
|
+
:value => [true, false]
|
77
89
|
}
|
78
90
|
}
|
79
91
|
|
@@ -186,7 +198,9 @@ module RandomToken
|
|
186
198
|
# Generate/count token with a given length.
|
187
199
|
def run_by_length(length, options)
|
188
200
|
if block_given?
|
189
|
-
|
201
|
+
final_opt = gen_seeds(options)
|
202
|
+
seeds = final_opt[:seed]
|
203
|
+
length = length * final_opt[:byte_mult] if final_opt[:byte]
|
190
204
|
yield(length, seeds)
|
191
205
|
else
|
192
206
|
raise RandomTokenError.new(
|
@@ -200,6 +214,9 @@ module RandomToken
|
|
200
214
|
raise RandomTokenError.new(
|
201
215
|
"No block is given when calling run_by_pattern.")
|
202
216
|
end
|
217
|
+
if options[:byte]
|
218
|
+
raise RandomTokenError.new(:format_not_support_byte, options)
|
219
|
+
end
|
203
220
|
in_arg = false
|
204
221
|
result = []
|
205
222
|
length = ''
|
@@ -314,6 +331,9 @@ module RandomToken
|
|
314
331
|
end
|
315
332
|
end
|
316
333
|
raise RandomTokenError.new(:mask_remove_all_seeds, opt) if opt[:seed] == []
|
334
|
+
if opt[:support_byte] == false && opt[:byte] != nil
|
335
|
+
raise RandomTokenError.new(:not_support_byte, opt)
|
336
|
+
end
|
317
337
|
opt
|
318
338
|
end
|
319
339
|
end
|
@@ -41,6 +41,14 @@ module RandomToken
|
|
41
41
|
:value => 9,
|
42
42
|
:msg => "The given option value is invalid."
|
43
43
|
},
|
44
|
+
:format_not_support_byte => {
|
45
|
+
:value => 10,
|
46
|
+
:msg => "The byte-length is not supported in the format mode."
|
47
|
+
},
|
48
|
+
:not_support_byte => {
|
49
|
+
:value => 11,
|
50
|
+
:msg => "The byte-length feature is not supported in this case, but the byte option is true."
|
51
|
+
},
|
44
52
|
}
|
45
53
|
|
46
54
|
attr_reader :code, :value, :msg, :info
|
data/lib/random_token/version.rb
CHANGED
@@ -0,0 +1,63 @@
|
|
1
|
+
require "minitest/autorun"
|
2
|
+
require "random_token"
|
3
|
+
|
4
|
+
class TestByte < Minitest::Test
|
5
|
+
def test_gen_should_create_a_random_with_16xlength_with_binary_seed
|
6
|
+
length = 32
|
7
|
+
token = RandomToken.gen(length, :seed => :binary, :byte => true)
|
8
|
+
assert_match(/^[0-1]{#{length*16}}$/, token)
|
9
|
+
token = RandomToken.gen(length, :seed => :binary, :b => true)
|
10
|
+
assert_match(/^[0-1]{#{length*16}}$/, token)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_gen_should_create_a_random_with_4xlength_with_oct_seed
|
14
|
+
length = 32
|
15
|
+
token = RandomToken.gen(length, :seed => :oct, :byte => true)
|
16
|
+
assert_match(/^[0-7]{#{length*4}}$/, token)
|
17
|
+
token = RandomToken.gen(length, :seed => :oct, :b => true)
|
18
|
+
assert_match(/^[0-7]{#{length*4}}$/, token)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_gen_should_create_a_random_with_2xlength_with_hex_seed
|
22
|
+
length = 32
|
23
|
+
token = RandomToken.gen(length, :seed => :hex, :byte => true)
|
24
|
+
assert_match(/^[A-Z0-9]{#{length*2}}$/, token)
|
25
|
+
token = RandomToken.gen(length, :seed => :hex, :b => true)
|
26
|
+
assert_match(/^[A-Z0-9]{#{length*2}}$/, token)
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_gen_should_not_support_byte_feature_when_use_format
|
30
|
+
e = assert_raises(RandomToken::RandomTokenError) { RandomToken.gen("%b", :byte => true) }
|
31
|
+
assert(e.code == :format_not_support_byte)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_gen_should_not_support_byte_feature_when_creating_alphabet_number_random
|
35
|
+
length = 10000
|
36
|
+
e = assert_raises(RandomToken::RandomTokenError) { RandomToken.gen(length, :byte => true) }
|
37
|
+
assert(e.code == :not_support_byte)
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_gen_should_not_support_byte_feature_when_creating_alphabet_random
|
41
|
+
length = 10000
|
42
|
+
e = assert_raises(RandomToken::RandomTokenError) { RandomToken.gen(length, :seed => :a, :byte => true) }
|
43
|
+
assert(e.code == :not_support_byte)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_gen_should_not_support_byte_feature_when_creating_number_random
|
47
|
+
length = 10000
|
48
|
+
e = assert_raises(RandomToken::RandomTokenError) { RandomToken.gen(length, :seed => :n, :byte => true) }
|
49
|
+
assert(e.code == :not_support_byte)
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_gen_should_not_support_byte_feature_when_creating_random_with_seed_array
|
53
|
+
length = 10000
|
54
|
+
e = assert_raises(RandomToken::RandomTokenError) { RandomToken.gen(length, :seed => ['a', 'b'], :byte => true) }
|
55
|
+
assert(e.code == :not_support_byte)
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_gen_should_not_support_byte_feature_when_creating_random_with_seed_hash
|
59
|
+
length = 10000
|
60
|
+
e = assert_raises(RandomToken::RandomTokenError) { RandomToken.gen(length, :seed => {'a' => 1, 'b' => 2}, :byte => true) }
|
61
|
+
assert(e.code == :not_support_byte)
|
62
|
+
end
|
63
|
+
end
|
data/test/length/test_seed.rb
CHANGED
@@ -5,89 +5,89 @@ class TestSeed < Minitest::Test
|
|
5
5
|
def test_gen_should_create_a_random_with_alphabets_only
|
6
6
|
length = 10000
|
7
7
|
token = RandomToken.gen(length, :seed => :a)
|
8
|
-
assert_match(/^[A-Za-z]
|
8
|
+
assert_match(/^[A-Za-z]{#{length}}$/, token)
|
9
9
|
token = RandomToken.gen(length, :seed => :alphabet)
|
10
|
-
assert_match(/^[A-Za-z]
|
10
|
+
assert_match(/^[A-Za-z]{#{length}}$/, token)
|
11
11
|
token = RandomToken.gen(length, :seed => :l)
|
12
|
-
assert_match(/^[A-Za-z]
|
12
|
+
assert_match(/^[A-Za-z]{#{length}}$/, token)
|
13
13
|
token = RandomToken.gen(length, :seed => :letter)
|
14
|
-
assert_match(/^[A-Za-z]
|
14
|
+
assert_match(/^[A-Za-z]{#{length}}$/, token)
|
15
15
|
token = RandomToken.gen(length, :s => :a)
|
16
|
-
assert_match(/^[A-Za-z]
|
16
|
+
assert_match(/^[A-Za-z]{#{length}}$/, token)
|
17
17
|
token = RandomToken.gen(length, :s => :alphabet)
|
18
|
-
assert_match(/^[A-Za-z]
|
18
|
+
assert_match(/^[A-Za-z]{#{length}}$/, token)
|
19
19
|
token = RandomToken.gen(length, :s => :l)
|
20
|
-
assert_match(/^[A-Za-z]
|
20
|
+
assert_match(/^[A-Za-z]{#{length}}$/, token)
|
21
21
|
token = RandomToken.gen(length, :s => :letter)
|
22
|
-
assert_match(/^[A-Za-z]
|
22
|
+
assert_match(/^[A-Za-z]{#{length}}$/, token)
|
23
23
|
end
|
24
24
|
|
25
25
|
def test_gen_should_create_a_random_with_numbers_only
|
26
26
|
length = 10000
|
27
27
|
token = RandomToken.gen(length, :seed => :n)
|
28
|
-
assert_match(/^[0-9]
|
28
|
+
assert_match(/^[0-9]{#{length}}$/, token)
|
29
29
|
token = RandomToken.gen(length, :seed => :number)
|
30
|
-
assert_match(/^[0-9]
|
30
|
+
assert_match(/^[0-9]{#{length}}$/, token)
|
31
31
|
token = RandomToken.gen(length, :seed => 1)
|
32
|
-
assert_match(/^[0-9]
|
32
|
+
assert_match(/^[0-9]{#{length}}$/, token)
|
33
33
|
token = RandomToken.gen(length, :seed => 10)
|
34
|
-
assert_match(/^[0-9]
|
34
|
+
assert_match(/^[0-9]{#{length}}$/, token)
|
35
35
|
token = RandomToken.gen(length, :s => :n)
|
36
|
-
assert_match(/^[0-9]
|
36
|
+
assert_match(/^[0-9]{#{length}}$/, token)
|
37
37
|
token = RandomToken.gen(length, :s => :number)
|
38
|
-
assert_match(/^[0-9]
|
38
|
+
assert_match(/^[0-9]{#{length}}$/, token)
|
39
39
|
token = RandomToken.gen(length, :s => 1)
|
40
|
-
assert_match(/^[0-9]
|
40
|
+
assert_match(/^[0-9]{#{length}}$/, token)
|
41
41
|
token = RandomToken.gen(length, :s => 10)
|
42
|
-
assert_match(/^[0-9]
|
42
|
+
assert_match(/^[0-9]{#{length}}$/, token)
|
43
43
|
end
|
44
44
|
|
45
45
|
def test_gen_should_create_a_random_with_0_and_1_only
|
46
46
|
length = 10000
|
47
47
|
token = RandomToken.gen(length, :seed => :b)
|
48
|
-
assert_match(/^[01]
|
48
|
+
assert_match(/^[01]{#{length}}$/, token)
|
49
49
|
token = RandomToken.gen(length, :seed => :binary)
|
50
|
-
assert_match(/^[01]
|
50
|
+
assert_match(/^[01]{#{length}}$/, token)
|
51
51
|
token = RandomToken.gen(length, :seed => 2)
|
52
|
-
assert_match(/^[01]
|
52
|
+
assert_match(/^[01]{#{length}}$/, token)
|
53
53
|
token = RandomToken.gen(length, :s => :b)
|
54
|
-
assert_match(/^[01]
|
54
|
+
assert_match(/^[01]{#{length}}$/, token)
|
55
55
|
token = RandomToken.gen(length, :s => :binary)
|
56
|
-
assert_match(/^[01]
|
56
|
+
assert_match(/^[01]{#{length}}$/, token)
|
57
57
|
token = RandomToken.gen(length, :s => 2)
|
58
|
-
assert_match(/^[01]
|
58
|
+
assert_match(/^[01]{#{length}}$/, token)
|
59
59
|
end
|
60
60
|
|
61
61
|
def test_gen_should_create_a_random_with_octal_digits
|
62
62
|
length = 10000
|
63
63
|
token = RandomToken.gen(length, :seed => :o)
|
64
|
-
assert_match(/^[0-7]
|
64
|
+
assert_match(/^[0-7]{#{length}}$/, token)
|
65
65
|
token = RandomToken.gen(length, :seed => :oct)
|
66
|
-
assert_match(/^[0-7]
|
66
|
+
assert_match(/^[0-7]{#{length}}$/, token)
|
67
67
|
token = RandomToken.gen(length, :seed => 8)
|
68
|
-
assert_match(/^[0-7]
|
68
|
+
assert_match(/^[0-7]{#{length}}$/, token)
|
69
69
|
token = RandomToken.gen(length, :s => :o)
|
70
|
-
assert_match(/^[0-7]
|
70
|
+
assert_match(/^[0-7]{#{length}}$/, token)
|
71
71
|
token = RandomToken.gen(length, :s => :oct)
|
72
|
-
assert_match(/^[0-7]
|
72
|
+
assert_match(/^[0-7]{#{length}}$/, token)
|
73
73
|
token = RandomToken.gen(length, :s => 8)
|
74
|
-
assert_match(/^[0-7]
|
74
|
+
assert_match(/^[0-7]{#{length}}$/, token)
|
75
75
|
end
|
76
76
|
|
77
77
|
def test_gen_should_create_a_random_with_hexadecimal_digits
|
78
78
|
length = 10000
|
79
79
|
token = RandomToken.gen(length, :seed => :h)
|
80
|
-
assert_match(/^[0-9A-F]
|
80
|
+
assert_match(/^[0-9A-F]{#{length}}$/, token)
|
81
81
|
token = RandomToken.gen(length, :seed => :hex)
|
82
|
-
assert_match(/^[0-9A-F]
|
82
|
+
assert_match(/^[0-9A-F]{#{length}}$/, token)
|
83
83
|
token = RandomToken.gen(length, :seed => 16)
|
84
|
-
assert_match(/^[0-9A-F]
|
84
|
+
assert_match(/^[0-9A-F]{#{length}}$/, token)
|
85
85
|
token = RandomToken.gen(length, :s => :h)
|
86
|
-
assert_match(/^[0-9A-F]
|
86
|
+
assert_match(/^[0-9A-F]{#{length}}$/, token)
|
87
87
|
token = RandomToken.gen(length, :s => :hex)
|
88
|
-
assert_match(/^[0-9A-F]
|
88
|
+
assert_match(/^[0-9A-F]{#{length}}$/, token)
|
89
89
|
token = RandomToken.gen(length, :s => 16)
|
90
|
-
assert_match(/^[0-9A-F]
|
90
|
+
assert_match(/^[0-9A-F]{#{length}}$/, token)
|
91
91
|
end
|
92
92
|
|
93
93
|
def test_gen_should_create_a_random_with_given_seeds
|
@@ -98,9 +98,9 @@ class TestSeed < Minitest::Test
|
|
98
98
|
'.', '>', '/', '?', '`', '~']
|
99
99
|
token = RandomToken.gen(length, :seed => seeds)
|
100
100
|
targets = "[#{Regexp.escape(seeds.join)}]"
|
101
|
-
assert_match(/^#{targets}
|
101
|
+
assert_match(/^#{targets}{#{length}}$/, token)
|
102
102
|
token = RandomToken.gen(length, :s => seeds)
|
103
103
|
targets = "[#{Regexp.escape(seeds.join)}]"
|
104
|
-
assert_match(/^#{targets}
|
104
|
+
assert_match(/^#{targets}{#{length}}$/, token)
|
105
105
|
end
|
106
106
|
end
|
data/test/test_gen.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: random_token
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sibevin Wang
|
@@ -46,6 +46,7 @@ files:
|
|
46
46
|
- test/get/test_length.rb
|
47
47
|
- test/get/test_mask.rb
|
48
48
|
- test/get/test_seed.rb
|
49
|
+
- test/length/test_byte.rb
|
49
50
|
- test/length/test_case.rb
|
50
51
|
- test/length/test_friendly.rb
|
51
52
|
- test/length/test_hash_seed.rb
|
@@ -96,6 +97,7 @@ test_files:
|
|
96
97
|
- test/get/test_length.rb
|
97
98
|
- test/get/test_mask.rb
|
98
99
|
- test/get/test_seed.rb
|
100
|
+
- test/length/test_byte.rb
|
99
101
|
- test/length/test_case.rb
|
100
102
|
- test/length/test_friendly.rb
|
101
103
|
- test/length/test_hash_seed.rb
|