Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ Changes:
it is given.
* `Array#delete_first` now returns the block's result when the item
isn't found, as its docs say and as `Array#delete` does.
* `Crypt3.generate_salt`, and so the salt `Crypt3.crypt` makes when
given none, now comes from `SecureRandom`. It came from `rand`, which
isn't a cryptographic source.

* Documentation

Expand Down
6 changes: 4 additions & 2 deletions lib/standard/facets/crypt3.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'securerandom'

# Crypt3 is a pure Ruby version of crypt(3), a salted one-way hashing
# of a password.
#
Expand Down Expand Up @@ -112,15 +114,15 @@ def self.check(password, hash, algo = :md5)
self.crypt(password, algo, salt, magic) == hash
end

# Generate a random salt of the given size.
# Generate a random salt of the given size, from SecureRandom.
#
# Crypt3.generate_salt(8) #=> "xK3d9Wq2"
#
# size - The size of the salt. [Integer]
#
# Returns random salt. [String]
def self.generate_salt(size)
(1..size).map { ITOA64[rand(ITOA64.size)] }.join
(1..size).map { ITOA64[SecureRandom.random_number(ITOA64.size)] }.join
end

end
8 changes: 8 additions & 0 deletions test/standard/test_crypt3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@
Crypt3.generate_salt(16).size.assert == 16
end

test "does not come from Kernel#rand" do
srand(1)
a = Crypt3.generate_salt(16)
srand(1)
b = Crypt3.generate_salt(16)
a.refute == b
end

test "uses only the crypt(3) base 64 characters" do
Crypt3.generate_salt(200).delete(Crypt3::ITOA64).assert == ''
end
Expand Down
Loading