Search code examples
phpencryptionbase64md5encode

I get the SAME output for the md5 hash, but the base64_encode result is not the same


I am trying to connect my site to a payment site that requires a certain validation key that is first md5 hashed, then base64 encoded. So the instructions pdf gave an example given a certain input string:

input EX123123456100.00
md5(input) => 231cd7f8e0151f6e0c4a60b33752a1e7
base64_encode(md5(input)) => IxzX+0AVH24MSmCzN1Kh5w==

So when I try this sample input, I get the SAME output for the md5 hash. But the base64_encode result is not the same. The instructions says that the base64_encode needs to encode a value in hex format for this to work. And I guess my base64_encode in my php script is encoding a string which is why I am getting a different result from the pdf file. So how do I make the base64_encode encode a hex value? Or is there another reason I am getting a different value?


Solution

  • It doesn't make a lot of sense to base64 encode something that is already a perfectly good ASCII string. If you pass a true as the second parameter to md5, it will return the raw binary data instead of a hex string. Base64 encoding that gives you the proper example value:

    base64_encode(md5($key, true))
    

    Either you have misunderstood the instructions, or the instructions are misleading.