securerandom 0.3.1 → 0.4.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 +4 -4
- data/{LICENSE.txt → BSDL} +3 -3
- data/COPYING +56 -0
- data/lib/securerandom.rb +7 -1
- metadata +6 -17
- data/.document +0 -4
- data/.github/dependabot.yml +0 -6
- data/.github/workflows/test.yml +0 -29
- data/.gitignore +0 -8
- data/Gemfile +0 -4
- data/Rakefile +0 -10
- data/bin/console +0 -14
- data/bin/setup +0 -8
- data/docs/random.rb +0 -10
- data/lib/random/formatter.rb +0 -373
- data/rakelib/epoch.rake +0 -5
- data/securerandom.gemspec +0 -29
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4d1e553e66963e86b8c31aeaca8030543f034770d4c91652e905e7d24101a267
|
4
|
+
data.tar.gz: b9088d38e62d77718b8cdf44f12e5027763bfc49d08e98e2190cac40217a53e7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dd175de6c33318073d7eeae18ed42bbd797f425ebcfcddcfd4d91cc9256fcd9b4ee96d9ef409e2817a49cdfc5f3a85496018a5642ce62124d09212c8b48348c8
|
7
|
+
data.tar.gz: 51914a9cfa12a128634135d650b9213a5aab22005542ad1fd79fa74b1456a3388def2ba1d13255262437a22e971fd888eeaaac63121111fa5a245b2429d99889
|
data/{LICENSE.txt → BSDL}
RENAMED
@@ -4,10 +4,10 @@ Redistribution and use in source and binary forms, with or without
|
|
4
4
|
modification, are permitted provided that the following conditions
|
5
5
|
are met:
|
6
6
|
1. Redistributions of source code must retain the above copyright
|
7
|
-
notice, this list of conditions and the following disclaimer.
|
7
|
+
notice, this list of conditions and the following disclaimer.
|
8
8
|
2. Redistributions in binary form must reproduce the above copyright
|
9
|
-
notice, this list of conditions and the following disclaimer in the
|
10
|
-
documentation and/or other materials provided with the distribution.
|
9
|
+
notice, this list of conditions and the following disclaimer in the
|
10
|
+
documentation and/or other materials provided with the distribution.
|
11
11
|
|
12
12
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
13
13
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
data/COPYING
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
Ruby is copyrighted free software by Yukihiro Matsumoto <matz@netlab.jp>.
|
2
|
+
You can redistribute it and/or modify it under either the terms of the
|
3
|
+
2-clause BSDL (see the file BSDL), or the conditions below:
|
4
|
+
|
5
|
+
1. You may make and give away verbatim copies of the source form of the
|
6
|
+
software without restriction, provided that you duplicate all of the
|
7
|
+
original copyright notices and associated disclaimers.
|
8
|
+
|
9
|
+
2. You may modify your copy of the software in any way, provided that
|
10
|
+
you do at least ONE of the following:
|
11
|
+
|
12
|
+
a. place your modifications in the Public Domain or otherwise
|
13
|
+
make them Freely Available, such as by posting said
|
14
|
+
modifications to Usenet or an equivalent medium, or by allowing
|
15
|
+
the author to include your modifications in the software.
|
16
|
+
|
17
|
+
b. use the modified software only within your corporation or
|
18
|
+
organization.
|
19
|
+
|
20
|
+
c. give non-standard binaries non-standard names, with
|
21
|
+
instructions on where to get the original software distribution.
|
22
|
+
|
23
|
+
d. make other distribution arrangements with the author.
|
24
|
+
|
25
|
+
3. You may distribute the software in object code or binary form,
|
26
|
+
provided that you do at least ONE of the following:
|
27
|
+
|
28
|
+
a. distribute the binaries and library files of the software,
|
29
|
+
together with instructions (in the manual page or equivalent)
|
30
|
+
on where to get the original distribution.
|
31
|
+
|
32
|
+
b. accompany the distribution with the machine-readable source of
|
33
|
+
the software.
|
34
|
+
|
35
|
+
c. give non-standard binaries non-standard names, with
|
36
|
+
instructions on where to get the original software distribution.
|
37
|
+
|
38
|
+
d. make other distribution arrangements with the author.
|
39
|
+
|
40
|
+
4. You may modify and include the part of the software into any other
|
41
|
+
software (possibly commercial). But some files in the distribution
|
42
|
+
are not written by the author, so that they are not under these terms.
|
43
|
+
|
44
|
+
For the list of those files and their copying conditions, see the
|
45
|
+
file LEGAL.
|
46
|
+
|
47
|
+
5. The scripts and library files supplied as input to or produced as
|
48
|
+
output from the software do not automatically fall under the
|
49
|
+
copyright of the software, but belong to whomever generated them,
|
50
|
+
and may be sold commercially, and may be aggregated with this
|
51
|
+
software.
|
52
|
+
|
53
|
+
6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
|
54
|
+
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
55
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
56
|
+
PURPOSE.
|
data/lib/securerandom.rb
CHANGED
@@ -41,7 +41,7 @@ require 'random/formatter'
|
|
41
41
|
module SecureRandom
|
42
42
|
|
43
43
|
# The version
|
44
|
-
VERSION = "0.
|
44
|
+
VERSION = "0.4.1"
|
45
45
|
|
46
46
|
class << self
|
47
47
|
# Returns a random binary string containing +size+ bytes.
|
@@ -51,6 +51,12 @@ module SecureRandom
|
|
51
51
|
return gen_random(n)
|
52
52
|
end
|
53
53
|
|
54
|
+
# Compatibility methods for Ruby 3.2, we can remove this after dropping to support Ruby 3.2
|
55
|
+
def alphanumeric(n = nil, chars: ALPHANUMERIC)
|
56
|
+
n = 16 if n.nil?
|
57
|
+
choose(chars, n)
|
58
|
+
end if RUBY_VERSION < '3.3'
|
59
|
+
|
54
60
|
private
|
55
61
|
|
56
62
|
# :stopdoc:
|
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.
|
4
|
+
version: 0.4.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:
|
11
|
+
date: 2024-12-16 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Interface for secure random number generator.
|
14
14
|
email:
|
@@ -17,21 +17,10 @@ executables: []
|
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
|
-
-
|
21
|
-
-
|
22
|
-
- ".github/workflows/test.yml"
|
23
|
-
- ".gitignore"
|
24
|
-
- Gemfile
|
25
|
-
- LICENSE.txt
|
20
|
+
- BSDL
|
21
|
+
- COPYING
|
26
22
|
- README.md
|
27
|
-
- Rakefile
|
28
|
-
- bin/console
|
29
|
-
- bin/setup
|
30
|
-
- docs/random.rb
|
31
|
-
- lib/random/formatter.rb
|
32
23
|
- lib/securerandom.rb
|
33
|
-
- rakelib/epoch.rake
|
34
|
-
- securerandom.gemspec
|
35
24
|
homepage: https://github.com/ruby/securerandom
|
36
25
|
licenses:
|
37
26
|
- Ruby
|
@@ -47,14 +36,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
47
36
|
requirements:
|
48
37
|
- - ">="
|
49
38
|
- !ruby/object:Gem::Version
|
50
|
-
version:
|
39
|
+
version: 3.1.0
|
51
40
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
41
|
requirements:
|
53
42
|
- - ">="
|
54
43
|
- !ruby/object:Gem::Version
|
55
44
|
version: '0'
|
56
45
|
requirements: []
|
57
|
-
rubygems_version: 3.5.
|
46
|
+
rubygems_version: 3.5.11
|
58
47
|
signing_key:
|
59
48
|
specification_version: 4
|
60
49
|
summary: Interface for secure random number generator.
|
data/.document
DELETED
data/.github/dependabot.yml
DELETED
data/.github/workflows/test.yml
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
name: test
|
2
|
-
|
3
|
-
on: [push, pull_request]
|
4
|
-
|
5
|
-
jobs:
|
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
|
14
|
-
name: build (${{ matrix.ruby }} / ${{ matrix.os }})
|
15
|
-
strategy:
|
16
|
-
matrix:
|
17
|
-
ruby: ${{ fromJson(needs.ruby-versions.outputs.versions) }}
|
18
|
-
os: [ ubuntu-latest, macos-latest, windows-latest ]
|
19
|
-
runs-on: ${{ matrix.os }}
|
20
|
-
steps:
|
21
|
-
- uses: actions/checkout@v4
|
22
|
-
- name: Set up Ruby
|
23
|
-
uses: ruby/setup-ruby@v1
|
24
|
-
with:
|
25
|
-
ruby-version: ${{ matrix.ruby }}
|
26
|
-
- name: Install dependencies
|
27
|
-
run: bundle install
|
28
|
-
- name: Run test
|
29
|
-
run: rake test
|
data/.gitignore
DELETED
data/Gemfile
DELETED
data/Rakefile
DELETED
data/bin/console
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require "bundler/setup"
|
4
|
-
require "securerandom"
|
5
|
-
|
6
|
-
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
-
# with your gem easier. You can also use a different console, if you like.
|
8
|
-
|
9
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
# require "pry"
|
11
|
-
# Pry.start
|
12
|
-
|
13
|
-
require "irb"
|
14
|
-
IRB.start(__FILE__)
|
data/bin/setup
DELETED
data/docs/random.rb
DELETED
data/lib/random/formatter.rb
DELETED
@@ -1,373 +0,0 @@
|
|
1
|
-
# -*- coding: us-ascii -*-
|
2
|
-
# frozen_string_literal: true
|
3
|
-
|
4
|
-
# == \Random number formatter.
|
5
|
-
#
|
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.
|
12
|
-
#
|
13
|
-
# === Examples
|
14
|
-
#
|
15
|
-
# Generate random hexadecimal strings:
|
16
|
-
#
|
17
|
-
# require 'random/formatter'
|
18
|
-
#
|
19
|
-
# prng = Random.new
|
20
|
-
# prng.hex(10) #=> "52750b30ffbc7de3b362"
|
21
|
-
# prng.hex(10) #=> "92b15d6c8dc4beb5f559"
|
22
|
-
# prng.hex(13) #=> "39b290146bea6ce975c37cfc23"
|
23
|
-
# # or just
|
24
|
-
# Random.hex #=> "1aed0c631e41be7f77365415541052ee"
|
25
|
-
#
|
26
|
-
# Generate random base64 strings:
|
27
|
-
#
|
28
|
-
# prng.base64(10) #=> "EcmTPZwWRAozdA=="
|
29
|
-
# prng.base64(10) #=> "KO1nIU+p9DKxGg=="
|
30
|
-
# prng.base64(12) #=> "7kJSM/MzBJI+75j8"
|
31
|
-
# Random.base64(4) #=> "bsQ3fQ=="
|
32
|
-
#
|
33
|
-
# Generate random binary strings:
|
34
|
-
#
|
35
|
-
# prng.random_bytes(10) #=> "\016\t{\370g\310pbr\301"
|
36
|
-
# prng.random_bytes(10) #=> "\323U\030TO\234\357\020\a\337"
|
37
|
-
# Random.random_bytes(6) #=> "\xA1\xE6Lr\xC43"
|
38
|
-
#
|
39
|
-
# Generate alphanumeric strings:
|
40
|
-
#
|
41
|
-
# prng.alphanumeric(10) #=> "S8baxMJnPl"
|
42
|
-
# prng.alphanumeric(10) #=> "aOxAg8BAJe"
|
43
|
-
# Random.alphanumeric #=> "TmP9OsJHJLtaZYhP"
|
44
|
-
#
|
45
|
-
# Generate UUIDs:
|
46
|
-
#
|
47
|
-
# prng.uuid #=> "2d931510-d99f-494a-8c67-87feb05e1594"
|
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"
|
54
|
-
|
55
|
-
module Random::Formatter
|
56
|
-
|
57
|
-
# Generate a random binary string.
|
58
|
-
#
|
59
|
-
# The argument _n_ specifies the length of the result string.
|
60
|
-
#
|
61
|
-
# If _n_ is not specified or is nil, 16 is assumed.
|
62
|
-
# It may be larger in future.
|
63
|
-
#
|
64
|
-
# The result may contain any byte: "\x00" - "\xff".
|
65
|
-
#
|
66
|
-
# require 'random/formatter'
|
67
|
-
#
|
68
|
-
# Random.random_bytes #=> "\xD8\\\xE0\xF4\r\xB2\xFC*WM\xFF\x83\x18\xF45\xB6"
|
69
|
-
# # or
|
70
|
-
# prng = Random.new
|
71
|
-
# prng.random_bytes #=> "m\xDC\xFC/\a\x00Uf\xB2\xB2P\xBD\xFF6S\x97"
|
72
|
-
def random_bytes(n=nil)
|
73
|
-
n = n ? n.to_int : 16
|
74
|
-
gen_random(n)
|
75
|
-
end
|
76
|
-
|
77
|
-
# Generate a random hexadecimal string.
|
78
|
-
#
|
79
|
-
# The argument _n_ specifies the length, in bytes, of the random number to be generated.
|
80
|
-
# The length of the resulting hexadecimal string is twice of _n_.
|
81
|
-
#
|
82
|
-
# If _n_ is not specified or is nil, 16 is assumed.
|
83
|
-
# It may be larger in the future.
|
84
|
-
#
|
85
|
-
# The result may contain 0-9 and a-f.
|
86
|
-
#
|
87
|
-
# require 'random/formatter'
|
88
|
-
#
|
89
|
-
# Random.hex #=> "eb693ec8252cd630102fd0d0fb7c3485"
|
90
|
-
# # or
|
91
|
-
# prng = Random.new
|
92
|
-
# prng.hex #=> "91dc3bfb4de5b11d029d376634589b61"
|
93
|
-
def hex(n=nil)
|
94
|
-
random_bytes(n).unpack1("H*")
|
95
|
-
end
|
96
|
-
|
97
|
-
# Generate a random base64 string.
|
98
|
-
#
|
99
|
-
# The argument _n_ specifies the length, in bytes, of the random number
|
100
|
-
# to be generated. The length of the result string is about 4/3 of _n_.
|
101
|
-
#
|
102
|
-
# If _n_ is not specified or is nil, 16 is assumed.
|
103
|
-
# It may be larger in the future.
|
104
|
-
#
|
105
|
-
# The result may contain A-Z, a-z, 0-9, "+", "/" and "=".
|
106
|
-
#
|
107
|
-
# require 'random/formatter'
|
108
|
-
#
|
109
|
-
# Random.base64 #=> "/2BuBuLf3+WfSKyQbRcc/A=="
|
110
|
-
# # or
|
111
|
-
# prng = Random.new
|
112
|
-
# prng.base64 #=> "6BbW0pxO0YENxn38HMUbcQ=="
|
113
|
-
#
|
114
|
-
# See RFC 3548 for the definition of base64.
|
115
|
-
def base64(n=nil)
|
116
|
-
[random_bytes(n)].pack("m0")
|
117
|
-
end
|
118
|
-
|
119
|
-
# Generate a random URL-safe base64 string.
|
120
|
-
#
|
121
|
-
# The argument _n_ specifies the length, in bytes, of the random number
|
122
|
-
# to be generated. The length of the result string is about 4/3 of _n_.
|
123
|
-
#
|
124
|
-
# If _n_ is not specified or is nil, 16 is assumed.
|
125
|
-
# It may be larger in the future.
|
126
|
-
#
|
127
|
-
# The boolean argument _padding_ specifies the padding.
|
128
|
-
# If it is false or nil, padding is not generated.
|
129
|
-
# Otherwise padding is generated.
|
130
|
-
# By default, padding is not generated because "=" may be used as a URL delimiter.
|
131
|
-
#
|
132
|
-
# The result may contain A-Z, a-z, 0-9, "-" and "_".
|
133
|
-
# "=" is also used if _padding_ is true.
|
134
|
-
#
|
135
|
-
# require 'random/formatter'
|
136
|
-
#
|
137
|
-
# Random.urlsafe_base64 #=> "b4GOKm4pOYU_-BOXcrUGDg"
|
138
|
-
# # or
|
139
|
-
# prng = Random.new
|
140
|
-
# prng.urlsafe_base64 #=> "UZLdOkzop70Ddx-IJR0ABg"
|
141
|
-
#
|
142
|
-
# prng.urlsafe_base64(nil, true) #=> "i0XQ-7gglIsHGV2_BNPrdQ=="
|
143
|
-
# prng.urlsafe_base64(nil, true) #=> "-M8rLhr7JEpJlqFGUMmOxg=="
|
144
|
-
#
|
145
|
-
# See RFC 3548 for the definition of URL-safe base64.
|
146
|
-
def urlsafe_base64(n=nil, padding=false)
|
147
|
-
s = [random_bytes(n)].pack("m0")
|
148
|
-
s.tr!("+/", "-_")
|
149
|
-
s.delete!("=") unless padding
|
150
|
-
s
|
151
|
-
end
|
152
|
-
|
153
|
-
# Generate a random v4 UUID (Universally Unique IDentifier).
|
154
|
-
#
|
155
|
-
# require 'random/formatter'
|
156
|
-
#
|
157
|
-
# Random.uuid #=> "2d931510-d99f-494a-8c67-87feb05e1594"
|
158
|
-
# Random.uuid #=> "bad85eb9-0713-4da7-8d36-07a8e4b00eab"
|
159
|
-
# # or
|
160
|
-
# prng = Random.new
|
161
|
-
# prng.uuid #=> "62936e70-1815-439b-bf89-8492855a7e6b"
|
162
|
-
#
|
163
|
-
# The version 4 UUID is purely random (except the version).
|
164
|
-
# It doesn't contain meaningful information such as MAC addresses, timestamps, etc.
|
165
|
-
#
|
166
|
-
# The result contains 122 random bits (15.25 random bytes).
|
167
|
-
#
|
168
|
-
# See RFC4122[https://datatracker.ietf.org/doc/html/rfc4122] for details of UUID.
|
169
|
-
#
|
170
|
-
def uuid
|
171
|
-
ary = random_bytes(16).unpack("NnnnnN")
|
172
|
-
ary[2] = (ary[2] & 0x0fff) | 0x4000
|
173
|
-
ary[3] = (ary[3] & 0x3fff) | 0x8000
|
174
|
-
"%08x-%04x-%04x-%04x-%04x%08x" % ary
|
175
|
-
end
|
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.
|
296
|
-
private def gen_random(n)
|
297
|
-
self.bytes(n)
|
298
|
-
end
|
299
|
-
|
300
|
-
# Generate a string that randomly draws from a
|
301
|
-
# source array of characters.
|
302
|
-
#
|
303
|
-
# The argument _source_ specifies the array of characters from which
|
304
|
-
# to generate the string.
|
305
|
-
# The argument _n_ specifies the length, in characters, of the string to be
|
306
|
-
# generated.
|
307
|
-
#
|
308
|
-
# The result may contain whatever characters are in the source array.
|
309
|
-
#
|
310
|
-
# require 'random/formatter'
|
311
|
-
#
|
312
|
-
# prng.choose([*'l'..'r'], 16) #=> "lmrqpoonmmlqlron"
|
313
|
-
# prng.choose([*'0'..'9'], 5) #=> "27309"
|
314
|
-
private def choose(source, n)
|
315
|
-
size = source.size
|
316
|
-
m = 1
|
317
|
-
limit = size
|
318
|
-
while limit * size <= 0x100000000
|
319
|
-
limit *= size
|
320
|
-
m += 1
|
321
|
-
end
|
322
|
-
result = ''.dup
|
323
|
-
while m <= n
|
324
|
-
rs = random_number(limit)
|
325
|
-
is = rs.digits(size)
|
326
|
-
(m-is.length).times { is << 0 }
|
327
|
-
result << source.values_at(*is).join('')
|
328
|
-
n -= m
|
329
|
-
end
|
330
|
-
if 0 < n
|
331
|
-
rs = random_number(limit)
|
332
|
-
is = rs.digits(size)
|
333
|
-
if is.length < n
|
334
|
-
(n-is.length).times { is << 0 }
|
335
|
-
else
|
336
|
-
is.pop while n < is.length
|
337
|
-
end
|
338
|
-
result.concat source.values_at(*is).join('')
|
339
|
-
end
|
340
|
-
result
|
341
|
-
end
|
342
|
-
|
343
|
-
# The default character list for #alphanumeric.
|
344
|
-
ALPHANUMERIC = [*'A'..'Z', *'a'..'z', *'0'..'9']
|
345
|
-
|
346
|
-
# Generate a random alphanumeric string.
|
347
|
-
#
|
348
|
-
# The argument _n_ specifies the length, in characters, of the alphanumeric
|
349
|
-
# string to be generated.
|
350
|
-
# The argument _chars_ specifies the character list which the result is
|
351
|
-
# consist of.
|
352
|
-
#
|
353
|
-
# If _n_ is not specified or is nil, 16 is assumed.
|
354
|
-
# It may be larger in the future.
|
355
|
-
#
|
356
|
-
# The result may contain A-Z, a-z and 0-9, unless _chars_ is specified.
|
357
|
-
#
|
358
|
-
# require 'random/formatter'
|
359
|
-
#
|
360
|
-
# Random.alphanumeric #=> "2BuBuLf3WfSKyQbR"
|
361
|
-
# # or
|
362
|
-
# prng = Random.new
|
363
|
-
# prng.alphanumeric(10) #=> "i6K93NdqiH"
|
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)
|
370
|
-
n = 16 if n.nil?
|
371
|
-
choose(chars, n)
|
372
|
-
end
|
373
|
-
end
|
data/rakelib/epoch.rake
DELETED
data/securerandom.gemspec
DELETED
@@ -1,29 +0,0 @@
|
|
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
|
-
|
8
|
-
Gem::Specification.new do |spec|
|
9
|
-
spec.name = name
|
10
|
-
spec.version = version
|
11
|
-
spec.authors = ["Tanaka Akira"]
|
12
|
-
spec.email = ["akr@fsij.org"]
|
13
|
-
|
14
|
-
spec.summary = %q{Interface for secure random number generator.}
|
15
|
-
spec.description = %q{Interface for secure random number generator.}
|
16
|
-
spec.homepage = "https://github.com/ruby/securerandom"
|
17
|
-
spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
|
18
|
-
spec.licenses = ["Ruby", "BSD-2-Clause"]
|
19
|
-
|
20
|
-
spec.metadata["homepage_uri"] = spec.homepage
|
21
|
-
spec.metadata["source_code_uri"] = spec.homepage
|
22
|
-
|
23
|
-
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
24
|
-
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
-
end
|
26
|
-
spec.bindir = "exe"
|
27
|
-
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
-
spec.require_paths = ["lib"]
|
29
|
-
end
|