Граф коммитов

74 Коммитов

Автор SHA1 Сообщение Дата
Hiroshi SHIBATA 2b3fc0407a
[ruby/securerandom] Bump up v0.3.1
https://github.com/ruby/securerandom/commit/890d659970
2023-12-16 12:10:38 +08:00
Nobuyoshi Nakada e411bd5b36 [ruby/securerandom] [DOC] Satisfy RDoc coverage
https://github.com/ruby/securerandom/commit/ddf2672e47
2023-12-12 06:40:44 +00:00
Nobuyoshi Nakada dbd704ae6f [ruby/securerandom] [DOC] Add documents of methods and constants
https://github.com/ruby/securerandom/commit/9a99978135
2023-12-12 06:40:43 +00:00
Hiroshi SHIBATA c3a11f1cfc [ruby/securerandom] Bump up 0.3.0
https://github.com/ruby/securerandom/commit/12c4ef3ea1
2023-11-07 13:21:23 +09:00
Hiroshi SHIBATA ebe620def6 [ruby/securerandom] Expose SecureRandom::VERSION
https://github.com/ruby/securerandom/commit/2e6434331d
2023-04-14 01:43:47 +00:00
卜部昌平 64e503eb62 avoid seeding
OpenSSL's man page previously stated that "the application is
responsible for seeding the PRNG by calling RAND_add" (see [1]).
So we had this code.  However things changed.  They no longer
say so, instead "manual (re-)seeding of the default OpenSSL
random generator is not necessary" now (see [2]).  It seems all
OpenSSL versions that we support now already behaves like this.
Let's follow that.

[1]: https://www.openssl.org/docs/man1.0.2/man3/RAND_add.html
[2]: https://www.openssl.org/docs/manmaster/man3/RAND_add.html
2023-04-13 19:50:00 +09:00
Yusuke Endoh b9851c7e1b lib/securerandom.rb: Fix the check of availability of Random.urandom
Random.urandom raises a RuntimeError if it is unavailable.
[Bug #13885]
2022-02-16 16:32:28 +09:00
Nobuyoshi Nakada 12a0a89e22 [ruby/securerandom] Split Random::Formatter from SecureRandom [Feature #18190]
https://github.com/ruby/securerandom/commit/1e57277b9e
2021-12-09 20:26:44 +09:00
Nobuyoshi Nakada b62ed309f0 [ruby/securerandom] [DOC] fix a code mark up [ci skip]
https://github.com/ruby/securerandom/commit/de47532707
2021-08-24 19:29:54 +09:00
Aaron Patterson 475c8701d7 Make SecureRandom support Ractor
SecureRandom lazily defines `get_random`.  Accessing the mutex to define
the `get_random` method is not supported inside a Ractor.  This commit
defines `gen_random` when `securerandom` is required and makes it
suppore Ractor (as well as thread safe).

Here is a test program:

```ruby
require "securerandom"

r = Ractor.new do
  loop do
    Ractor.yield SecureRandom.hex
  end
end

p r.take
```

Before this commit:

```
$ make runruby
./miniruby -I./lib -I. -I.ext/common  ./tool/runruby.rb --extout=.ext  -- --disable-gems ./test.rb
<internal:ractor>:38: warning: Ractor is experimental, and the behavior may change in future versions of Ruby! Also there are many implementation issues.
/Users/aaron/git/ruby/lib/securerandom.rb:94:in `gen_random': can not access instance variables of classes/modules from non-main Ractors (RuntimeError)
<internal:ractor>:124:in `take': thrown by remote Ractor. (Ractor::RemoteError)
	from ./test.rb:9:in `<main>'
/Users/aaron/git/ruby/lib/securerandom.rb:94:in `gen_random': can not access instance variables of classes/modules from non-main Ractors (RuntimeError)
	from /Users/aaron/git/ruby/lib/securerandom.rb:155:in `random_bytes'
	from /Users/aaron/git/ruby/lib/securerandom.rb:176:in `hex'
	from ./test.rb:5:in `block (2 levels) in <main>'
	from ./test.rb:4:in `loop'
	from ./test.rb:4:in `block in <main>'
make: *** [runruby] Error
```

After this commit:

```
$ make runruby
./miniruby -I./lib -I. -I.ext/common  ./tool/runruby.rb --extout=.ext  -- --disable-gems ./test.rb
<internal:ractor>:38: warning: Ractor is experimental, and the behavior may change in future versions of Ruby! Also there are many implementation issues.
"3fc8885157e3911bab4b5d7619bb0308"
```
2020-09-09 12:45:43 -07:00
Urabe, Shyouhei 5bab1304af fix visibility of SecureRandom.gen_random
Aliasing a method preserves its visibility.  These aliases turn
formerly-public methods into private.  Should make them public
again.  [Bug #15847]
2019-05-14 11:44:20 +09:00
stomar f284eb3d54 lib/securerandom.rb: [DOC] small grammar fixes
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66885 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-01-20 15:06:11 +00:00
akr b7c2ca7a32 [DOC] SecureRandom is extended by Random::Formatter.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66586 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-27 12:42:45 +00:00
stomar 88aaf88923 lib/securerandom.rb: improve docs
* lib/securerandom.rb: [DOC] add alphanumeric example to module docs.
  [Fix GH-1812]

From: Justin Bull <me@justinbull.ca>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63640 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-06-12 20:22:43 +00:00
stomar 4b8d33a323 lib/securerandom.rb: improve docs
* lib/securerandom.rb: [DOC] drop unnecessary `p' from code examples.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63402 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-05-11 20:07:32 +00:00
nobu d32763eca0 securerandom.rb: [DOC] require in examples
* lib/securerandom.rb: added `require 'securerandom'` to each
  example, to state these methods are defined in this library and
  require it explicitly.  [ruby-core:85933] [Bug #14576]

[ci skip]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63341 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-05-05 03:12:20 +00:00
nobu 7ed3aae2fe securerandom.rb: fix an example of choose [ci skip]
* lib/securerandom.rb (Random::Formatter#choose): [DOC] fix an
  example, `n` is not optional.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60420 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-10-25 07:28:22 +00:00
akr 6cba29b422 less random generations in Random::Formatter#choose.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60309 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-10-21 16:12:46 +00:00
akr b867882a1c SecureRandom.alphanumeric implemented.
[ruby-core:68098] [Feature #10849] proposed by Andrew Butterfield.

SecureRandom.choose and SecureRandom.graph is not included.
(The implementation has SecureRandom.choose but it is private.)

I feel the method name, SecureRandom.choose, doesn't represent
the behavior well.

The actual use cases of SecureRandom.graph is not obvious.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60297 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-10-21 15:21:26 +00:00
mame 3b08df649e lib/securerandom.rb: test one byte to determine urandom or openssl
`SecureRandom#gen_random` determines whether urandom is available or not
by trying `Random.urandom(n)`.  But, when n = 0, `Random.urandom(0)`
always succeeds even if urandom is not available, which leads to a wrong
decision.

When failed, `Random.urandom` returns nil instead of returning a shorter
string than required.  So the check for `ret.length != n` is not needed.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59840 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-09-11 12:44:51 +00:00
hsbt f7b3d2be57 Remove commented out code of SecureRandom.random_number.
[Misc #13870][ruby-core:82654] Patch by @aycabta.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59835 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-09-11 06:11:37 +00:00
rhe a0acd82f2a securerandom: fix up r57384
SecureRandom.gen_random_openssl still refers to Random.raw_seed, which
is renamed to Random.urandom by r57384.  [Bug #9569]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57707 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-02-24 11:33:06 +00:00
shyouhei abae70d6ed SecureRandom should try /dev/urandom first [Bug #9569]
* random.c (InitVM_Random): rename Random.raw_seed to
	  Random.urandom.  A quick search seems there are no practical use
	  of this method than securerandom.rb so I think it's OK to rename
	  but if there are users of it, this hunk is subject to revert.

	* test/ruby/test_rand.rb (TestRand#test_urandom): test for it.

	* lib/securerandom.rb (SecureRandom.gen_random): Prefer OS-
	  provided CSPRNG if available. Otherwise falls back to OpenSSL.
	  Current preference is:

	  1. CSPRNG routine that the OS has; one of
	     - getrandom(2),
	     - arc4random(3), or
	     - CryptGenRandom()
	  2. /dev/urandom device
	  3. OpenSSL's RAND_bytes(3)

	  If none of above random number generators are available, you
	  cannot use this module.  An exception is raised that case.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57384 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-01-20 08:00:00 +00:00
nobu b402cc7161 random.c: use bytes
* random.c (obj_random_bytes): base on bytes method instead of
  rand method, not to call toplevel rand method.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54968 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-05-10 05:57:11 +00:00
naruse 6545327e24 * lib/securerandom.rb (gen_random): to avoid blocking on Windows.
On Windows OpenSSL RAND_bytes (underlying implementation is
  RAND_poll in crypto/rand/rand_win.c) may be blocked at
  NetStatisticsGet.
  https://wiki.openssl.org/index.php/Random_Numbers#Windows_Issues
  Instead of this, use Random.raw_seed directory (whose implementation
  CryptGenRandom is one of the source of
  entropy of RAND_poll on Windows).
  https://wiki.openssl.org/index.php/Random_Numbers
  Note: CryptGenRandom function is PRNG and doesn't check its entropy,
  so it won't block. [Bug #12139]
  https://msdn.microsoft.com/ja-jp/library/windows/desktop/aa379942.aspx
  https://tools.ietf.org/html/rfc4086#section-7.1.3
  https://eprint.iacr.org/2007/419.pdf
  http://www.cs.huji.ac.il/~dolev/pubs/thesis/msc-thesis-leo.pdf

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54144 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-03-17 06:01:11 +00:00
nobu f9fc092f90 securerandom.rb: remove to_s call
* lib/securerandom.rb (gen_random): Array#join returns a String,
  no to_s is needed.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53999 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-03-04 13:39:46 +00:00
normal 778bbac8ac stdlib: avoid extra calls to eliminate "\n" from Base64
We may use the '0' (zero) to avoid adding the line feed.
Furthermore, the '*' (asterisk) modifier is not needed for
a single-element arrays.

* ext/psych/lib/psych/visitors/yaml_tree.rb (visit_String):
  eliminate chomp
* lib/net/http.rb (connect): eliminate delete
* lib/net/http/header.rb (basic_encode): ditto
* lib/net/imap.rb (authenticate): eliminate gsub
  (self.encode_utf7): shorten delete arg
* lib/net/smtp.rb (base64_encode): eliminate gsub
* lib/open-uri.rb (OpenURI.open_http): eliminate delete
* lib/rss/rss.rb: ditto
* lib/securerandom.rb (base64): ditto
  (urlsafe_base64): eliminate delete!
* lib/webrick/httpauth/digestauth.rb (split_param_value):
  eliminate chop
* lib/webrick/httpproxy.rb (do_CONNECT): eliminate delete
  (setup_upstream_proxy_authentication): ditto
  [ruby-core:72666] [Feature #11938]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53488 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-01-10 00:35:43 +00:00
kosaki 7104a473ea * lib/securerandom.rb (SecureRandom::gen_random): use /dev/urandom
for initialize OpenSSL's rand.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52811 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-11-30 20:29:22 +00:00
akr f4fbc7d2a6 [DOC]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52583 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-11-16 03:34:53 +00:00
akr 68ebbbfebe * lib/open-uri.rb: Remove indicator for "frozen_string_literal: true".
* lib/pp.rb: Ditto.

* lib/prettyprint.rb: Ditto.

* lib/resolv.rb: Ditto.

* lib/securerandom.rb: Ditto.

* lib/tmpdir.rb: Ditto.

* lib/unicode_normalize/tables.rb: Ditto.

* test/net/ftp/test_buffered_socket.rb: Ditto.

* test/net/ftp/test_mlsx_entry.rb: Ditto.

* test/open-uri/test_open-uri.rb: Ditto.

* test/open-uri/test_ssl.rb: Ditto.

* test/pathname/test_pathname.rb: Ditto.

* test/test_pp.rb: Ditto.

* test/test_prettyprint.rb: Ditto.

* tool/transcode-tblgen.rb: Ditto.

* ext/pathname/lib/pathname.rb: Ditto.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52526 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-11-10 11:48:14 +00:00
akr 3d61b259ff * lib/securerandom.rb: Specify frozen_string_literal: true.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52174 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-10-18 03:58:34 +00:00
nobu b1adbd14e5 random.c: rand_random_number
* random.c (rand_random_number): add a method to return a random
  number like SecureRandom to Random::Formatter.
* lib/securerandom.rb (random_bytes): move to Random::Formatter,
  the base method of the module.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49596 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-02-14 03:20:04 +00:00
nobu 1f13a179d3 securerandom.rb: Random::Formatter
* lib/securerandom.rb (Random::Formatter): extract random number
  formatting methods into a module.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49595 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-02-14 03:02:32 +00:00
nobu eeeb7ae8c0 random.c: Random.raw_seed
* random.c (random_raw_seed): extract platform dependent random
  seed initialization function as a new method Random.raw_seed.
* lib/securerandom.rb (SecureRandom): use Random.raw_seed.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49593 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-02-14 03:01:36 +00:00
hsbt 045de8a9c5 * lib/securerandom.rb: improve syntax and grammar of documentation.
[fix GH-796][ci skip] Patch by @Erol

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49100 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-01-02 06:36:00 +00:00
nobu 93ccab82c5 securerandom.rb: separate implementations
* lib/securerandom.rb (SecureRandom.gen_random): separate
  implementation details and select at the load time.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48334 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-11-09 03:16:24 +00:00
nobu 7d7bdcc5c6 securerandom.rb: fix substring of FormatMessage result
* lib/securerandom.rb: set the script encoding to make a string
  literal in SecureRandom::Kernel32.last_error_message single byte
  encoding so msg[] works in bytes, since FormatMessage() returns
  the size in TCHARs, not in characters.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48328 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-11-09 01:31:05 +00:00
usa dc79945a2b * lib/securerandom.rb (initialize): call the special method for Win32
before cheking `/dev/urandom` because we know windows doesn't have it.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48324 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-11-08 20:01:24 +00:00
usa 6999a86600 * lib/securerandom.rb: more refactoring.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48323 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-11-08 19:59:01 +00:00
usa b2247be993 * lib/securerandom.rb (SecureRandom::AdvApi32): split from `initialize`.
thanks @zzak to remember it.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48322 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-11-08 19:02:48 +00:00
usa 07f460829a * lib/securerandom.rb (SecureRandom.random_bytes): use fiddle directly
instead of using Win32API.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48269 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-11-04 12:20:59 +00:00
tenderlove 07308c4d30 * ext/dl/*: remove DL as it is replaced by Fiddle.
[Feature #5458] Thanks to Jonan Scheffler <jonanscheffler@gmail.com>
  for this patch

* test/dl/*: ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48217 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-10-31 21:13:09 +00:00
glass 0a22f4c168 * lib/securerandom.rb: use OpenSSL::BN for performance improvement.
* benchmark/bm_securerandom.rb: benchmark script.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47104 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-08-08 11:00:47 +00:00
zzak 09c3f84566 * lib/securerandom.rb: [DOC] Add note on require for examples
Based on a patch by @schneems [Fixes GH-518] [ci skip]
  https://github.com/ruby/ruby/pull/518


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44782 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-01-31 21:12:49 +00:00
zzak d24903f063 commit miss from r43573
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43575 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-11-07 18:04:05 +00:00
zzak 9408bba58b * lib/securerandom.rb: [DOC] SecureRandom.hex length argument
[Fixes GH-394] Patch by @avdi https://github.com/ruby/ruby/pull/394


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42991 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-09-20 14:40:08 +00:00
akr 7a4feec795 * lib/securerandom.rb (random_bytes): Use Process.clock_gettime.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42740 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-08-31 05:07:56 +00:00
zzak 84247a63ce * lib/securerandom.rb: Refactor conditions by Rafal Chmiel
[Fixes GH-326] https://github.com/ruby/ruby/pull/326


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41914 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-07-11 04:01:47 +00:00
zzak e3436d2957 * lib/securerandom.rb: Update position of overview for RDoc
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40853 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-05-20 11:37:04 +00:00
akr c3c4ffa972 * lib/securerandom.rb (SecureRandom.random_bytes): Use
OpenSSL::Random.random_add instead of OpenSSL::Random.seed and
  specify 0.0 as the entropy.
  [ruby-core:47308] [Bug #6928]



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40072 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-02 15:09:36 +00:00