-4

I'm trying to replicate the following python code to verify a md5 collision given in the answer to "What's the shortest pair of strings that causes an MD5 collision?".

>>> from array import array
>>> from hashlib import md5
>>> input1 = array('I',  [0x6165300e,0x87a79a55,0xf7c60bd0,0x34febd0b,0x6503cf04,
    0x854f709e,0xfb0fc034,0x874c9c65,0x2f94cc40,0x15a12deb,0x5c15f4a3,0x490786bb,
    0x6d658673,0xa4341f7d,0x8fd75920,0xefd18d5a])
>>> input2 = array('I', [x^y for x,y in zip(input1,
    [0, 0, 0, 0, 0, 1<<10, 0, 0, 0, 0, 1<<31, 0, 0, 0, 0, 0])])
>>> input1 == input2
False
>>> md5(input1).hexdigest()
'cee9a457e790cf20d4bdaa6d69f01e41'
>>> md5(input2).hexdigest()
'cee9a457e790cf20d4bdaa6d69f01e41'

How do I process input1 and input2 in Ruby? Digest::Md5 doesn't seem to accept arrays.

Community
  • 1
  • 1
Danny
  • 35
  • 3
  • What do you mean by "process"? What format is your original data in? The [`String#unpack`](http://apidock.com/ruby/String/unpack) function is probably a good place to start. – tadman Oct 15 '14 at 21:36
  • I mean to do it like the python code `md5(input1)`. It does not work with `Digest::MD5`It only accepts strings. – Danny Oct 15 '14 at 21:40
  • Then make a string out of your input. You can use `Array#pack` to construct those. – tadman Oct 15 '14 at 21:44

1 Answers1

0

As already mentioned, use Array#pack to build a binary string then pass to a hash function. Working code:

require 'digest/md5'

input1= [
  0x6165300e,0x87a79a55,0xf7c60bd0,0x34febd0b,0x6503cf04,0x854f709e,
  0xfb0fc034,0x874c9c65,0x2f94cc40,0x15a12deb,0x5c15f4a3,0x490786bb,
  0x6d658673,0xa4341f7d,0x8fd75920,0xefd18d5a].pack('I*')
p Digest::MD5.hexdigest input1
# "cee9a457e790cf20d4bdaa6d69f01e41"
David Unric
  • 7,421
  • 1
  • 37
  • 65