2

How is this possible? I can't get Ruby to give me the same MD5 as PHP does and can't figure this out... I thought encoding, but it seems I've managed to keep every file in the same format.

PHP Code: (result: bfa7df865d9f4aff62c4643d0e1cb33b)

echo md5("9jdaksjdfosdjsljz91m1wms0zkzd0k0askd1m2l3!@3@#@akskd9");

Ruby Code with Digest-MD5: (result: bd53907a1b47e9a381ee663ec75b59f4)

require 'digest/md5'
puts Digest::MD5.hexdigest("9jdaksjdfosdjsljz91m1wms0zkzd0k0askd1m2l3!@3@#@akskd9")

Ruby Code with HMAC-MD5: (result: d41d8cd98f00b204e9800998ecf8427e)

require 'hmac-md5'
puts HMAC::MD5.new("9jdaksjdfosdjsljz91m1wms0zkzd0k0askd1m2l3!@3@#@akskd9").hexdigest
James Stone
  • 487
  • 7
  • 16

2 Answers2

7

Use single quotes instead of double quotes

Digest::MD5.hexdigest('9jdaksjdfosdjsljz91m1wms0zkzd0k0askd1m2l3!@3@#@akskd9')
Colin M
  • 13,010
  • 3
  • 38
  • 58
  • 1
    @RomanMittermayr Then there must be a slight difference between the PHP and Ruby input in your code. I just ran this on my computer and got `bfa7df865d9f4aff62c4643d0e1cb33b` – Colin M Feb 15 '13 at 17:58
  • Yes, but I realized I was using single quotes and it makes a difference. Check my answer now. – Colin M Feb 15 '13 at 18:01
  • Yes single quotes it is!!! Why is that?? (I'll accept this in a minute or two when it let's me, but this fixes it). – James Stone Feb 15 '13 at 18:01
  • 1
    Because the string is evaluated differently inside of double quotes and the hash `#` character is throwing it off. To be honest, I'm not entirely sure why (since I believe ruby control sequence for including variables in strings is `#{...}` (notice the curly braces). – Colin M Feb 15 '13 at 18:02
  • 6
    `#@akskd9` will try and include the instance variable named `akskd9`. The critical part is the `#@` together, not just the `#`. – Tomdarkness Feb 15 '13 at 18:12
  • @Tomdarkness Gah! Forgot about the `@` sign. Thanks for commenting on this one - personally not a ruby guy. – Colin M Feb 15 '13 at 18:12
  • A bit more elaboration on the #@ and @## feature here http://stackoverflow.com/questions/10091156/why-does-this-string-interpolation-work-in-ruby – bradgonesurfing Feb 15 '13 at 19:18
  • 1
    Pro tip: don't use double quotes for strings unless you are interpolating or need the special escapes, default to single quotes so that double quotes will give you a heads up to check the string for interpolation. – mu is too short Feb 15 '13 at 19:47
  • @muistooshort Yeah, I use that philosophy in PHP as well. Actually, even when I could use double quotes, I use single and concat. Then it's VERY explicit about what I want. – Colin M Feb 15 '13 at 19:55
2

It is the combination of "#@" that makes it want to treat "@akskd9" at the end of the string as a variable.

For example:

@akskd9 = "foobar"
puts "9jdaksjdfosdjsljz91m1wms0zkzd0k0askd1m2l3!@3@#@akskd9"

=> 9jdaksjdfosdjsljz91m1wms0zkzd0k0askd1m2l3!@3@foobar

..without the # in there, then it ignores the @ symbol:

@akskd9 = "foobar"
puts "9jdaksjdfosdjsljz91m1wms0zkzd0k0askd1m2l3!@3@@akskd9"

=> 9jdaksjdfosdjsljz91m1wms0zkzd0k0askd1m2l3!@3@@akskd9

...interesting though, without the {...} after the #, it treats the rest of the string as a variable.

@ak = "foobar"
puts "9jdaksjdfosdjsljz91m1wms0zkzd0k0askd1m2l3!@3@#@akskd9"

=> 9jdaksjdfosdjsljz91m1wms0zkzd0k0askd1m2l3!@3@
CCinkosky
  • 121
  • 1
  • 4