2024-09-24 16:40:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2024-10-19 17:03:35 -04:00
|
|
|
# This code fixes failed to get urandom for running Ruby on Docker for Synology.
|
2024-09-24 16:40:34 -04:00
|
|
|
class Random
|
|
|
|
|
class << self
|
|
|
|
|
private
|
2024-10-19 17:03:35 -04:00
|
|
|
|
2024-09-24 16:40:34 -04:00
|
|
|
# :stopdoc:
|
|
|
|
|
|
|
|
|
|
# Implementation using OpenSSL
|
|
|
|
|
def gen_random_openssl(n)
|
2024-10-19 17:03:35 -04:00
|
|
|
OpenSSL::Random.random_bytes(n)
|
2024-09-24 16:40:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
begin
|
|
|
|
|
# Check if Random.urandom is available
|
|
|
|
|
Random.urandom(1)
|
|
|
|
|
rescue RuntimeError
|
|
|
|
|
begin
|
|
|
|
|
require 'openssl'
|
|
|
|
|
rescue NoMethodError
|
2024-10-19 17:03:35 -04:00
|
|
|
raise NotImplementedError, 'No random device'
|
2024-09-24 16:40:34 -04:00
|
|
|
else
|
|
|
|
|
alias urandom gen_random_openssl
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
public :urandom
|
|
|
|
|
end
|
|
|
|
|
end
|