Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many times can i compose a md5 function with itself?

For studying purposes it would be usefull to find out how many times can i compose a md5 function with itself without getting the same value.

This is a paralell/complementary approach to the salt, because this way the value gets harder to crack using brute force.

like image 960
Andras Gyomrey Avatar asked Sep 15 '25 07:09

Andras Gyomrey


1 Answers

Seemingly infinite. However MD5 has been shown to not be collision resistant so at some point you will have a duplicate.

The following Ruby code will cyclicly apply the MD5 hashing algorithm until a duplicate has been detected, at which point it will print the number of cycles required to reach the duplication point. The original string is randomly generated from alphabetical characters.

require 'set'
require 'digest'

keys = Set.new

o = [('a'..'z'), ('A'..'Z')].map { |i| i.to_a }.flatten
string = (0...10).map{ o[rand(o.length)] }.join
count = 0

while !keys.include?(string) do
  count += 1
  puts count
  keys << string
  string = Digest::MD5.digest(string)
end

puts "#{count}"

This continues to run past 15mil cycles... I will update once a duplicate has been found.

Update: due to the limited resources of my machine I had to halt the above script after 75,933,338 cycles without a collision (the set had allocated ~8 GB in memory)

like image 157
aren55555 Avatar answered Sep 17 '25 19:09

aren55555