Skip to content

Commit

Permalink
Performance: Reduce the time complexity of all Faker::Crypto methods (#…
Browse files Browse the repository at this point in the history
…2938)

* Reduce the time complexity of all Faker::Crypto methods

* Store crypto lorem number of characters in a class constant
  • Loading branch information
alextaujenis authored May 16, 2024
1 parent 41f620d commit 7dc2f40
Showing 1 changed file with 13 additions and 4 deletions.
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

0 comments on commit 7dc2f40

Please sign in to comment.