securerandom 0.2.0 → 0.2.2
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 +4 -4
- data/.github/dependabot.yml +6 -0
- data/.github/workflows/test.yml +1 -1
- data/lib/random/formatter.rb +45 -17
- data/lib/securerandom.rb +5 -4
- data/securerandom.gemspec +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5ae0b7af17b7e1ae5e5538a9faf771856d7052e570dd2edb7f3876b44ff73caa
|
4
|
+
data.tar.gz: 99bbfb8ac5dfa0f2089e4b8ab18a9dcf3628bb1b2564778b14045056e6473879
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a6b61eecc2cf9681ea54abc84909635dca596e1ff453c5af84e75e87b042345e5d9b55f30a99149c61f7407e9238993dbc836efc9feb03067cb0538770cfa5d
|
7
|
+
data.tar.gz: c458cecffdea575d9eea0ec41341520d1465d359dda792b8448149a5c32b6e76ccf8f85074c919a97d916dfe379e41f97d461e5dd242a155708808cf99dc7e07
|
data/.github/workflows/test.yml
CHANGED
data/lib/random/formatter.rb
CHANGED
@@ -1,9 +1,14 @@
|
|
1
1
|
# -*- coding: us-ascii -*-
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
|
-
# == Random number formatter.
|
4
|
+
# == \Random number formatter.
|
5
5
|
#
|
6
|
-
# Formats generated random numbers in many manners.
|
6
|
+
# Formats generated random numbers in many manners. When <tt>'random/formatter'</tt>
|
7
|
+
# is required, several methods are added to empty core module <tt>Random::Formatter</tt>,
|
8
|
+
# making them available as Random's instance and module methods.
|
9
|
+
#
|
10
|
+
# Standard library SecureRandom is also extended with the module, and the methods
|
11
|
+
# described below are available as a module methods in it.
|
7
12
|
#
|
8
13
|
# === Examples
|
9
14
|
#
|
@@ -11,34 +16,45 @@
|
|
11
16
|
#
|
12
17
|
# require 'random/formatter'
|
13
18
|
#
|
19
|
+
# prng = Random.new
|
14
20
|
# prng.hex(10) #=> "52750b30ffbc7de3b362"
|
15
21
|
# prng.hex(10) #=> "92b15d6c8dc4beb5f559"
|
16
22
|
# prng.hex(13) #=> "39b290146bea6ce975c37cfc23"
|
23
|
+
# # or just
|
24
|
+
# Random.hex #=> "1aed0c631e41be7f77365415541052ee"
|
17
25
|
#
|
18
26
|
# Generate random base64 strings:
|
19
27
|
#
|
20
28
|
# prng.base64(10) #=> "EcmTPZwWRAozdA=="
|
21
29
|
# prng.base64(10) #=> "KO1nIU+p9DKxGg=="
|
22
30
|
# prng.base64(12) #=> "7kJSM/MzBJI+75j8"
|
31
|
+
# Random.base64(4) #=> "bsQ3fQ=="
|
23
32
|
#
|
24
33
|
# Generate random binary strings:
|
25
34
|
#
|
26
35
|
# prng.random_bytes(10) #=> "\016\t{\370g\310pbr\301"
|
27
36
|
# prng.random_bytes(10) #=> "\323U\030TO\234\357\020\a\337"
|
37
|
+
# Random.random_bytes(6) #=> "\xA1\xE6Lr\xC43"
|
28
38
|
#
|
29
39
|
# Generate alphanumeric strings:
|
30
40
|
#
|
31
41
|
# prng.alphanumeric(10) #=> "S8baxMJnPl"
|
32
42
|
# prng.alphanumeric(10) #=> "aOxAg8BAJe"
|
43
|
+
# Random.alphanumeric #=> "TmP9OsJHJLtaZYhP"
|
33
44
|
#
|
34
45
|
# Generate UUIDs:
|
35
46
|
#
|
36
47
|
# prng.uuid #=> "2d931510-d99f-494a-8c67-87feb05e1594"
|
37
48
|
# prng.uuid #=> "bad85eb9-0713-4da7-8d36-07a8e4b00eab"
|
49
|
+
# Random.uuid #=> "f14e0271-de96-45cc-8911-8910292a42cd"
|
50
|
+
#
|
51
|
+
# All methods are available in the standard library SecureRandom, too:
|
52
|
+
#
|
53
|
+
# SecureRandom.hex #=> "05b45376a30c67238eb93b16499e50cf"
|
38
54
|
|
39
55
|
module Random::Formatter
|
40
56
|
|
41
|
-
#
|
57
|
+
# Generate a random binary string.
|
42
58
|
#
|
43
59
|
# The argument _n_ specifies the length of the result string.
|
44
60
|
#
|
@@ -49,14 +65,16 @@ module Random::Formatter
|
|
49
65
|
#
|
50
66
|
# require 'random/formatter'
|
51
67
|
#
|
52
|
-
#
|
68
|
+
# Random.random_bytes #=> "\xD8\\\xE0\xF4\r\xB2\xFC*WM\xFF\x83\x18\xF45\xB6"
|
69
|
+
# # or
|
70
|
+
# prng = Random.new
|
53
71
|
# prng.random_bytes #=> "m\xDC\xFC/\a\x00Uf\xB2\xB2P\xBD\xFF6S\x97"
|
54
72
|
def random_bytes(n=nil)
|
55
73
|
n = n ? n.to_int : 16
|
56
74
|
gen_random(n)
|
57
75
|
end
|
58
76
|
|
59
|
-
#
|
77
|
+
# Generate a random hexadecimal string.
|
60
78
|
#
|
61
79
|
# The argument _n_ specifies the length, in bytes, of the random number to be generated.
|
62
80
|
# The length of the resulting hexadecimal string is twice of _n_.
|
@@ -68,13 +86,15 @@ module Random::Formatter
|
|
68
86
|
#
|
69
87
|
# require 'random/formatter'
|
70
88
|
#
|
71
|
-
#
|
89
|
+
# Random.hex #=> "eb693ec8252cd630102fd0d0fb7c3485"
|
90
|
+
# # or
|
91
|
+
# prng = Random.new
|
72
92
|
# prng.hex #=> "91dc3bfb4de5b11d029d376634589b61"
|
73
93
|
def hex(n=nil)
|
74
94
|
random_bytes(n).unpack1("H*")
|
75
95
|
end
|
76
96
|
|
77
|
-
#
|
97
|
+
# Generate a random base64 string.
|
78
98
|
#
|
79
99
|
# The argument _n_ specifies the length, in bytes, of the random number
|
80
100
|
# to be generated. The length of the result string is about 4/3 of _n_.
|
@@ -86,7 +106,9 @@ module Random::Formatter
|
|
86
106
|
#
|
87
107
|
# require 'random/formatter'
|
88
108
|
#
|
89
|
-
#
|
109
|
+
# Random.base64 #=> "/2BuBuLf3+WfSKyQbRcc/A=="
|
110
|
+
# # or
|
111
|
+
# prng = Random.new
|
90
112
|
# prng.base64 #=> "6BbW0pxO0YENxn38HMUbcQ=="
|
91
113
|
#
|
92
114
|
# See RFC 3548 for the definition of base64.
|
@@ -94,7 +116,7 @@ module Random::Formatter
|
|
94
116
|
[random_bytes(n)].pack("m0")
|
95
117
|
end
|
96
118
|
|
97
|
-
#
|
119
|
+
# Generate a random URL-safe base64 string.
|
98
120
|
#
|
99
121
|
# The argument _n_ specifies the length, in bytes, of the random number
|
100
122
|
# to be generated. The length of the result string is about 4/3 of _n_.
|
@@ -112,7 +134,9 @@ module Random::Formatter
|
|
112
134
|
#
|
113
135
|
# require 'random/formatter'
|
114
136
|
#
|
115
|
-
#
|
137
|
+
# Random.urlsafe_base64 #=> "b4GOKm4pOYU_-BOXcrUGDg"
|
138
|
+
# # or
|
139
|
+
# prng = Random.new
|
116
140
|
# prng.urlsafe_base64 #=> "UZLdOkzop70Ddx-IJR0ABg"
|
117
141
|
#
|
118
142
|
# prng.urlsafe_base64(nil, true) #=> "i0XQ-7gglIsHGV2_BNPrdQ=="
|
@@ -126,12 +150,14 @@ module Random::Formatter
|
|
126
150
|
s
|
127
151
|
end
|
128
152
|
|
129
|
-
#
|
153
|
+
# Generate a random v4 UUID (Universally Unique IDentifier).
|
130
154
|
#
|
131
155
|
# require 'random/formatter'
|
132
156
|
#
|
133
|
-
#
|
134
|
-
#
|
157
|
+
# Random.uuid #=> "2d931510-d99f-494a-8c67-87feb05e1594"
|
158
|
+
# Random.uuid #=> "bad85eb9-0713-4da7-8d36-07a8e4b00eab"
|
159
|
+
# # or
|
160
|
+
# prng = Random.new
|
135
161
|
# prng.uuid #=> "62936e70-1815-439b-bf89-8492855a7e6b"
|
136
162
|
#
|
137
163
|
# The version 4 UUID is purely random (except the version).
|
@@ -139,7 +165,7 @@ module Random::Formatter
|
|
139
165
|
#
|
140
166
|
# The result contains 122 random bits (15.25 random bytes).
|
141
167
|
#
|
142
|
-
# See
|
168
|
+
# See RFC4122[https://datatracker.ietf.org/doc/html/rfc4122] for details of UUID.
|
143
169
|
#
|
144
170
|
def uuid
|
145
171
|
ary = random_bytes(16).unpack("NnnnnN")
|
@@ -152,7 +178,7 @@ module Random::Formatter
|
|
152
178
|
self.bytes(n)
|
153
179
|
end
|
154
180
|
|
155
|
-
#
|
181
|
+
# Generate a string that randomly draws from a
|
156
182
|
# source array of characters.
|
157
183
|
#
|
158
184
|
# The argument _source_ specifies the array of characters from which
|
@@ -196,7 +222,7 @@ module Random::Formatter
|
|
196
222
|
end
|
197
223
|
|
198
224
|
ALPHANUMERIC = [*'A'..'Z', *'a'..'z', *'0'..'9']
|
199
|
-
#
|
225
|
+
# Generate a random alphanumeric string.
|
200
226
|
#
|
201
227
|
# The argument _n_ specifies the length, in characters, of the alphanumeric
|
202
228
|
# string to be generated.
|
@@ -208,7 +234,9 @@ module Random::Formatter
|
|
208
234
|
#
|
209
235
|
# require 'random/formatter'
|
210
236
|
#
|
211
|
-
#
|
237
|
+
# Random.alphanumeric #=> "2BuBuLf3WfSKyQbR"
|
238
|
+
# # or
|
239
|
+
# prng = Random.new
|
212
240
|
# prng.alphanumeric(10) #=> "i6K93NdqiH"
|
213
241
|
def alphanumeric(n=nil)
|
214
242
|
n = 16 if n.nil?
|
data/lib/securerandom.rb
CHANGED
@@ -72,8 +72,11 @@ module SecureRandom
|
|
72
72
|
ret
|
73
73
|
end
|
74
74
|
|
75
|
-
|
76
|
-
|
75
|
+
begin
|
76
|
+
# Check if Random.urandom is available
|
77
|
+
Random.urandom(1)
|
78
|
+
alias gen_random gen_random_urandom
|
79
|
+
rescue RuntimeError
|
77
80
|
begin
|
78
81
|
require 'openssl'
|
79
82
|
rescue NoMethodError
|
@@ -81,8 +84,6 @@ module SecureRandom
|
|
81
84
|
else
|
82
85
|
alias gen_random gen_random_openssl
|
83
86
|
end
|
84
|
-
else
|
85
|
-
alias gen_random gen_random_urandom
|
86
87
|
end
|
87
88
|
|
88
89
|
public :gen_random
|
data/securerandom.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: securerandom
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tanaka Akira
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-12-14 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Interface for secure random number generator.
|
14
14
|
email:
|
@@ -17,6 +17,7 @@ executables: []
|
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
|
+
- ".github/dependabot.yml"
|
20
21
|
- ".github/workflows/test.yml"
|
21
22
|
- ".gitignore"
|
22
23
|
- Gemfile
|