Skip to content

Commit b8b70ad

Browse files
authored
Merge pull request rails#52473 from r7kamura/securerandom-base36
Declare Active Support dependency on securerandom >= 0.3
2 parents b6a848f + 33baa07 commit b8b70ad

File tree

3 files changed

+11
-24
lines changed

3 files changed

+11
-24
lines changed

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ PATH
9595
i18n (>= 1.6, < 2)
9696
logger (>= 1.4.2)
9797
minitest (>= 5.1)
98+
securerandom (>= 0.3)
9899
tzinfo (~> 2.0, >= 2.0.5)
99100
rails (8.0.0.alpha)
100101
actioncable (= 8.0.0.alpha)
@@ -515,6 +516,7 @@ GEM
515516
google-protobuf (~> 3.25)
516517
sass-embedded (1.69.6-x86_64-linux-gnu)
517518
google-protobuf (~> 3.25)
519+
securerandom (0.3.1)
518520
selenium-webdriver (4.22.0)
519521
base64 (~> 0.2)
520522
logger (~> 1.4)

activesupport/activesupport.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,5 @@ Gem::Specification.new do |s|
4343
s.add_dependency "drb"
4444
s.add_dependency "bigdecimal"
4545
s.add_dependency "logger", ">= 1.4.2"
46+
s.add_dependency "securerandom", ">= 0.3"
4647
end

activesupport/lib/active_support/core_ext/securerandom.rb

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,8 @@ module SecureRandom
1616
#
1717
# p SecureRandom.base58 # => "4kUgL2pdQMSCQtjE"
1818
# p SecureRandom.base58(24) # => "77TMHrHJFvFDwodq8w7Ev2m7"
19-
if RUBY_VERSION >= "3.3"
20-
def self.base58(n = 16)
21-
SecureRandom.alphanumeric(n, chars: BASE58_ALPHABET)
22-
end
23-
else
24-
def self.base58(n = 16)
25-
SecureRandom.random_bytes(n).unpack("C*").map do |byte|
26-
idx = byte % 64
27-
idx = SecureRandom.random_number(58) if idx >= 58
28-
BASE58_ALPHABET[idx]
29-
end.join
30-
end
19+
def self.base58(n = 16)
20+
SecureRandom.alphanumeric(n, chars: BASE58_ALPHABET)
3121
end
3222

3323
# SecureRandom.base36 generates a random base36 string in lowercase.
@@ -41,17 +31,11 @@ def self.base58(n = 16)
4131
#
4232
# p SecureRandom.base36 # => "4kugl2pdqmscqtje"
4333
# p SecureRandom.base36(24) # => "77tmhrhjfvfdwodq8w7ev2m7"
44-
if RUBY_VERSION >= "3.3"
45-
def self.base36(n = 16)
46-
SecureRandom.alphanumeric(n, chars: BASE36_ALPHABET)
47-
end
48-
else
49-
def self.base36(n = 16)
50-
SecureRandom.random_bytes(n).unpack("C*").map do |byte|
51-
idx = byte % 64
52-
idx = SecureRandom.random_number(36) if idx >= 36
53-
BASE36_ALPHABET[idx]
54-
end.join
55-
end
34+
def self.base36(n = 16)
35+
SecureRandom.random_bytes(n).unpack("C*").map do |byte|
36+
idx = byte % 64
37+
idx = SecureRandom.random_number(36) if idx >= 36
38+
BASE36_ALPHABET[idx]
39+
end.join
5640
end
5741
end

0 commit comments

Comments
 (0)