Skip to content

Commit

Permalink
Reduce the time complexity of all Faker::Crypto methods
Browse files Browse the repository at this point in the history
  • Loading branch information
alextaujenis committed Apr 18, 2024
1 parent 6db1e8f commit 7d416d6
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions lib/faker/default/crypto.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ class << self
#
# @faker.version 1.6.4
def md5
OpenSSL::Digest::MD5.hexdigest(Lorem.characters)
# The MD5 algorithm will experience a collision much sooner
# than Lorem.characters(number: 25) will. Setting the lorem
# character number lower than the default of 255 reduces the
# time complexity of this method while still returning
# deterministically unique values. Mathematical proof:
# 36^24 < 16^32 by -317830109213583906223287396307975536640
# 36^25 > 16^32 by 467998910543825597179764993024768081920
OpenSSL::Digest::MD5.hexdigest(Lorem.characters(number: 25))
end

##
Expand All @@ -28,7 +35,9 @@ def md5
#
# @faker.version 1.6.4
def sha1
OpenSSL::Digest::SHA1.hexdigest(Lorem.characters)
# 36^30 < 16^40 by -1412627959350213660714362080442508415790271692800
# 36^31 > 16^40 by 297950769973910351411934249139602719507858063360
OpenSSL::Digest::SHA1.hexdigest(Lorem.characters(number: 31))
end

##
Expand All @@ -41,7 +50,9 @@ def sha1
#
# @faker.version 1.6.4
def sha256
OpenSSL::Digest::SHA256.hexdigest(Lorem.characters)
# 36^49 < 16^64 by -97644349695647559143107366476519635060571548263614039829927807164315986821120
# 36^50 > 16^64 by 537526534262754710673119282149369912683873725807314307503614382361584011837440
OpenSSL::Digest::SHA256.hexdigest(Lorem.characters(number: 50))
end

##
Expand All @@ -54,7 +65,9 @@ def sha256
#
# @faker.version next
def sha512
OpenSSL::Digest::SHA512.hexdigest(Lorem.characters)
# 36^99 < 16^128 by -1551551712941835966324722456017686895729678449634353669515809251079738209250776448494314872034761836111898095223260222216129848353124479880210993674977280
# 36^100 > 16^128 by 413417415882084803697400866520567886215509379533897036117755517491391165519546192042270265042590193869122783608489643780100711357705648672437581942913761280
OpenSSL::Digest::SHA512.hexdigest(Lorem.characters(number: 100))
end
end
end
Expand Down

0 comments on commit 7d416d6

Please sign in to comment.