securerandom 0.2.2 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5ae0b7af17b7e1ae5e5538a9faf771856d7052e570dd2edb7f3876b44ff73caa
4
- data.tar.gz: 99bbfb8ac5dfa0f2089e4b8ab18a9dcf3628bb1b2564778b14045056e6473879
3
+ metadata.gz: 154280692de39feb39b719344f5c47fd256decdb5bd71cf80cb54b87ef155bd3
4
+ data.tar.gz: 0aab5a7dc6e671530e0327bcad3bf9d4caae862fed9925bc9cdcc47a57eae68f
5
5
  SHA512:
6
- metadata.gz: 5a6b61eecc2cf9681ea54abc84909635dca596e1ff453c5af84e75e87b042345e5d9b55f30a99149c61f7407e9238993dbc836efc9feb03067cb0538770cfa5d
7
- data.tar.gz: c458cecffdea575d9eea0ec41341520d1465d359dda792b8448149a5c32b6e76ccf8f85074c919a97d916dfe379e41f97d461e5dd242a155708808cf99dc7e07
6
+ metadata.gz: 99c06212cd3c4908e1bf9c2a459e7f3cc851fbb38396256932787b9725b7aaad7873c7b3ac00bfde458bd2316f1e38e02e62734d0df43d1d1c6ca29c2905ea3c
7
+ data.tar.gz: c8680a7e9566fcd2d064560c46046f7bc97d1e3b257613c7396306912fac01dcb810f20604e87c4914c327cc1d0f66c3ae8fc952b40f75bec58f520da2f7c627
data/.document ADDED
@@ -0,0 +1,4 @@
1
+ LICENSE.txt
2
+ README.md
3
+ docs/
4
+ lib/
@@ -1,17 +1,24 @@
1
- name: build
1
+ name: test
2
2
 
3
3
  on: [push, pull_request]
4
4
 
5
5
  jobs:
6
- build:
6
+ ruby-versions:
7
+ uses: ruby/actions/.github/workflows/ruby_versions.yml@master
8
+ with:
9
+ engine: cruby
10
+ min_version: 2.6
11
+
12
+ test:
13
+ needs: ruby-versions
7
14
  name: build (${{ matrix.ruby }} / ${{ matrix.os }})
8
15
  strategy:
9
16
  matrix:
10
- ruby: [ head, '3.0', '2.7', '2.6' ]
17
+ ruby: ${{ fromJson(needs.ruby-versions.outputs.versions) }}
11
18
  os: [ ubuntu-latest, macos-latest, windows-latest ]
12
19
  runs-on: ${{ matrix.os }}
13
20
  steps:
14
- - uses: actions/checkout@v3
21
+ - uses: actions/checkout@v4
15
22
  - name: Set up Ruby
16
23
  uses: ruby/setup-ruby@v1
17
24
  with:
data/docs/random.rb ADDED
@@ -0,0 +1,10 @@
1
+ # This file is only for RDoc
2
+
3
+ # Random provides an interface to Ruby's pseudo-random number generator, or
4
+ # PRNG.
5
+ #
6
+ # See also Random::Formatter module that adds convenience methods to generate
7
+ # various forms of random data.
8
+
9
+ class Random
10
+ end
@@ -174,6 +174,125 @@ module Random::Formatter
174
174
  "%08x-%04x-%04x-%04x-%04x%08x" % ary
175
175
  end
176
176
 
177
+ alias uuid_v4 uuid
178
+
179
+ # Generate a random v7 UUID (Universally Unique IDentifier).
180
+ #
181
+ # require 'random/formatter'
182
+ #
183
+ # Random.uuid_v7 # => "0188d4c3-1311-7f96-85c7-242a7aa58f1e"
184
+ # Random.uuid_v7 # => "0188d4c3-16fe-744f-86af-38fa04c62bb5"
185
+ # Random.uuid_v7 # => "0188d4c3-1af8-764f-b049-c204ce0afa23"
186
+ # Random.uuid_v7 # => "0188d4c3-1e74-7085-b14f-ef6415dc6f31"
187
+ # # |<--sorted-->| |<----- random ---->|
188
+ #
189
+ # # or
190
+ # prng = Random.new
191
+ # prng.uuid_v7 # => "0188ca51-5e72-7950-a11d-def7ff977c98"
192
+ #
193
+ # The version 7 UUID starts with the least significant 48 bits of a 64 bit
194
+ # Unix timestamp (milliseconds since the epoch) and fills the remaining bits
195
+ # with random data, excluding the version and variant bits.
196
+ #
197
+ # This allows version 7 UUIDs to be sorted by creation time. Time ordered
198
+ # UUIDs can be used for better database index locality of newly inserted
199
+ # records, which may have a significant performance benefit compared to random
200
+ # data inserts.
201
+ #
202
+ # The result contains 74 random bits (9.25 random bytes).
203
+ #
204
+ # Note that this method cannot be made reproducable because its output
205
+ # includes not only random bits but also timestamp.
206
+ #
207
+ # See draft-ietf-uuidrev-rfc4122bis[https://datatracker.ietf.org/doc/draft-ietf-uuidrev-rfc4122bis/]
208
+ # for details of UUIDv7.
209
+ #
210
+ # ==== Monotonicity
211
+ #
212
+ # UUIDv7 has millisecond precision by default, so multiple UUIDs created
213
+ # within the same millisecond are not issued in monotonically increasing
214
+ # order. To create UUIDs that are time-ordered with sub-millisecond
215
+ # precision, up to 12 bits of additional timestamp may added with
216
+ # +extra_timestamp_bits+. The extra timestamp precision comes at the expense
217
+ # of random bits. Setting <tt>extra_timestamp_bits: 12</tt> provides ~244ns
218
+ # of precision, but only 62 random bits (7.75 random bytes).
219
+ #
220
+ # prng = Random.new
221
+ # Array.new(4) { prng.uuid_v7(extra_timestamp_bits: 12) }
222
+ # # =>
223
+ # ["0188d4c7-13da-74f9-8b53-22a786ffdd5a",
224
+ # "0188d4c7-13da-753b-83a5-7fb9b2afaeea",
225
+ # "0188d4c7-13da-754a-88ea-ac0baeedd8db",
226
+ # "0188d4c7-13da-7557-83e1-7cad9cda0d8d"]
227
+ # # |<--- sorted --->| |<-- random --->|
228
+ #
229
+ # Array.new(4) { prng.uuid_v7(extra_timestamp_bits: 8) }
230
+ # # =>
231
+ # ["0188d4c7-3333-7a95-850a-de6edb858f7e",
232
+ # "0188d4c7-3333-7ae8-842e-bc3a8b7d0cf9", # <- out of order
233
+ # "0188d4c7-3333-7ae2-995a-9f135dc44ead", # <- out of order
234
+ # "0188d4c7-3333-7af9-87c3-8f612edac82e"]
235
+ # # |<--- sorted -->||<---- random --->|
236
+ #
237
+ # Any rollbacks of the system clock will break monotonicity. UUIDv7 is based
238
+ # on UTC, which excludes leap seconds and can rollback the clock. To avoid
239
+ # this, the system clock can synchronize with an NTP server configured to use
240
+ # a "leap smear" approach. NTP or PTP will also be needed to synchronize
241
+ # across distributed nodes.
242
+ #
243
+ # Counters and other mechanisms for stronger guarantees of monotonicity are
244
+ # not implemented. Applications with stricter requirements should follow
245
+ # {Section 6.2}[https://www.ietf.org/archive/id/draft-ietf-uuidrev-rfc4122bis-07.html#monotonicity_counters]
246
+ # of the specification.
247
+ #
248
+ def uuid_v7(extra_timestamp_bits: 0)
249
+ case (extra_timestamp_bits = Integer(extra_timestamp_bits))
250
+ when 0 # min timestamp precision
251
+ ms = Process.clock_gettime(Process::CLOCK_REALTIME, :millisecond)
252
+ rand = random_bytes(10)
253
+ rand.setbyte(0, rand.getbyte(0) & 0x0f | 0x70) # version
254
+ rand.setbyte(2, rand.getbyte(2) & 0x3f | 0x80) # variant
255
+ "%08x-%04x-%s" % [
256
+ (ms & 0x0000_ffff_ffff_0000) >> 16,
257
+ (ms & 0x0000_0000_0000_ffff),
258
+ rand.unpack("H4H4H12").join("-")
259
+ ]
260
+
261
+ when 12 # max timestamp precision
262
+ ms, ns = Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond)
263
+ .divmod(1_000_000)
264
+ extra_bits = ns * 4096 / 1_000_000
265
+ rand = random_bytes(8)
266
+ rand.setbyte(0, rand.getbyte(0) & 0x3f | 0x80) # variant
267
+ "%08x-%04x-7%03x-%s" % [
268
+ (ms & 0x0000_ffff_ffff_0000) >> 16,
269
+ (ms & 0x0000_0000_0000_ffff),
270
+ extra_bits,
271
+ rand.unpack("H4H12").join("-")
272
+ ]
273
+
274
+ when (0..12) # the generic version is slower than the special cases above
275
+ rand_a, rand_b1, rand_b2, rand_b3 = random_bytes(10).unpack("nnnN")
276
+ rand_mask_bits = 12 - extra_timestamp_bits
277
+ ms, ns = Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond)
278
+ .divmod(1_000_000)
279
+ "%08x-%04x-%04x-%04x-%04x%08x" % [
280
+ (ms & 0x0000_ffff_ffff_0000) >> 16,
281
+ (ms & 0x0000_0000_0000_ffff),
282
+ 0x7000 |
283
+ ((ns * (1 << extra_timestamp_bits) / 1_000_000) << rand_mask_bits) |
284
+ rand_a & ((1 << rand_mask_bits) - 1),
285
+ 0x8000 | (rand_b1 & 0x3fff),
286
+ rand_b2,
287
+ rand_b3
288
+ ]
289
+
290
+ else
291
+ raise ArgumentError, "extra_timestamp_bits must be in 0..12"
292
+ end
293
+ end
294
+
295
+ # Internal interface to Random; Generate random data _n_ bytes.
177
296
  private def gen_random(n)
178
297
  self.bytes(n)
179
298
  end
@@ -221,16 +340,20 @@ module Random::Formatter
221
340
  result
222
341
  end
223
342
 
343
+ # The default character list for #alphanumeric.
224
344
  ALPHANUMERIC = [*'A'..'Z', *'a'..'z', *'0'..'9']
345
+
225
346
  # Generate a random alphanumeric string.
226
347
  #
227
348
  # The argument _n_ specifies the length, in characters, of the alphanumeric
228
349
  # string to be generated.
350
+ # The argument _chars_ specifies the character list which the result is
351
+ # consist of.
229
352
  #
230
353
  # If _n_ is not specified or is nil, 16 is assumed.
231
354
  # It may be larger in the future.
232
355
  #
233
- # The result may contain A-Z, a-z and 0-9.
356
+ # The result may contain A-Z, a-z and 0-9, unless _chars_ is specified.
234
357
  #
235
358
  # require 'random/formatter'
236
359
  #
@@ -238,8 +361,13 @@ module Random::Formatter
238
361
  # # or
239
362
  # prng = Random.new
240
363
  # prng.alphanumeric(10) #=> "i6K93NdqiH"
241
- def alphanumeric(n=nil)
364
+ #
365
+ # Random.alphanumeric(4, chars: [*"0".."9"]) #=> "2952"
366
+ # # or
367
+ # prng = Random.new
368
+ # prng.alphanumeric(10, chars: [*"!".."/"]) #=> ",.,++%/''."
369
+ def alphanumeric(n = nil, chars: ALPHANUMERIC)
242
370
  n = 16 if n.nil?
243
- choose(ALPHANUMERIC, n)
371
+ choose(chars, n)
244
372
  end
245
373
  end
data/lib/securerandom.rb CHANGED
@@ -39,28 +39,28 @@ require 'random/formatter'
39
39
  # +NotImplementedError+ is raised.
40
40
 
41
41
  module SecureRandom
42
+
43
+ # The version
44
+ VERSION = "0.3.1"
45
+
42
46
  class << self
47
+ # Returns a random binary string containing +size+ bytes.
48
+ #
49
+ # See Random.bytes
43
50
  def bytes(n)
44
51
  return gen_random(n)
45
52
  end
46
53
 
47
54
  private
48
55
 
56
+ # :stopdoc:
57
+
58
+ # Implementation using OpenSSL
49
59
  def gen_random_openssl(n)
50
- @pid = 0 unless defined?(@pid)
51
- pid = $$
52
- unless @pid == pid
53
- now = Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond)
54
- OpenSSL::Random.random_add([now, @pid, pid].join(""), 0.0)
55
- seed = Random.urandom(16)
56
- if (seed)
57
- OpenSSL::Random.random_add(seed, 16)
58
- end
59
- @pid = pid
60
- end
61
60
  return OpenSSL::Random.random_bytes(n)
62
61
  end
63
62
 
63
+ # Implementation using system random device
64
64
  def gen_random_urandom(n)
65
65
  ret = Random.urandom(n)
66
66
  unless ret
@@ -86,6 +86,9 @@ module SecureRandom
86
86
  end
87
87
  end
88
88
 
89
+ # :startdoc:
90
+
91
+ # Generate random data bytes for Random::Formatter
89
92
  public :gen_random
90
93
  end
91
94
  end
data/securerandom.gemspec CHANGED
@@ -1,6 +1,13 @@
1
+ name = File.basename(__FILE__, ".gemspec")
2
+ version = ["lib", Array.new(name.count("-")+1).join("/")].find do |dir|
3
+ break File.foreach(File.join(__dir__, dir, "#{name.tr('-', '/')}.rb")) do |line|
4
+ /^\s*VERSION\s*=\s*"(.*)"/ =~ line and break $1
5
+ end rescue nil
6
+ end
7
+
1
8
  Gem::Specification.new do |spec|
2
- spec.name = "securerandom"
3
- spec.version = "0.2.2"
9
+ spec.name = name
10
+ spec.version = version
4
11
  spec.authors = ["Tanaka Akira"]
5
12
  spec.email = ["akr@fsij.org"]
6
13
 
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.2
4
+ version: 0.3.1
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-12-14 00:00:00.000000000 Z
11
+ date: 2023-12-16 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
+ - ".document"
20
21
  - ".github/dependabot.yml"
21
22
  - ".github/workflows/test.yml"
22
23
  - ".gitignore"
@@ -26,6 +27,7 @@ files:
26
27
  - Rakefile
27
28
  - bin/console
28
29
  - bin/setup
30
+ - docs/random.rb
29
31
  - lib/random/formatter.rb
30
32
  - lib/securerandom.rb
31
33
  - rakelib/epoch.rake
@@ -52,7 +54,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
54
  - !ruby/object:Gem::Version
53
55
  version: '0'
54
56
  requirements: []
55
- rubygems_version: 3.4.0.dev
57
+ rubygems_version: 3.5.0.dev
56
58
  signing_key:
57
59
  specification_version: 4
58
60
  summary: Interface for secure random number generator.