Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance: Reduce the time complexity of Faker::Crypto generators #2938

Merged
merged 5 commits into from
May 16, 2024
Merged
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
17 changes: 13 additions & 4 deletions lib/faker/default/crypto.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
module Faker
class Crypto < Base
class << self
# Setting the lorem character number lower than the default of
# 255 reduces the time complexity of each hash algorithm while
# still returning deterministically unique values. See
# https://github.com/faker-ruby/faker/pull/2938 for more info.
MD5_MIN_NUMBER_OF_CHARACTERS = 25
SHA1_MIN_NUMBER_OF_CHARACTERS = 31
SHA256_MIN_NUMBER_OF_CHARACTERS = 50
SHA512_MIN_NUMBER_OF_CHARACTERS = 100

##
# Produces an MD5 hash.
#
Expand All @@ -15,7 +24,7 @@ class << self
#
# @faker.version 1.6.4
def md5
OpenSSL::Digest::MD5.hexdigest(Lorem.characters)
OpenSSL::Digest::MD5.hexdigest(Lorem.characters(number: MD5_MIN_NUMBER_OF_CHARACTERS))
end

##
Expand All @@ -28,7 +37,7 @@ def md5
#
# @faker.version 1.6.4
def sha1
OpenSSL::Digest::SHA1.hexdigest(Lorem.characters)
OpenSSL::Digest::SHA1.hexdigest(Lorem.characters(number: SHA1_MIN_NUMBER_OF_CHARACTERS))
end

##
Expand All @@ -41,7 +50,7 @@ def sha1
#
# @faker.version 1.6.4
def sha256
OpenSSL::Digest::SHA256.hexdigest(Lorem.characters)
OpenSSL::Digest::SHA256.hexdigest(Lorem.characters(number: SHA256_MIN_NUMBER_OF_CHARACTERS))
end

##
Expand All @@ -54,7 +63,7 @@ def sha256
#
# @faker.version next
def sha512
OpenSSL::Digest::SHA512.hexdigest(Lorem.characters)
OpenSSL::Digest::SHA512.hexdigest(Lorem.characters(number: SHA512_MIN_NUMBER_OF_CHARACTERS))
end
end
end
Expand Down
Loading