New framework & MD5 question

Being a relative newcomer to Xojo, I’ve been taking the opportunity to check out some blog posts and run through the sample projects to see how to do things. While checking out this post (http://blog.xojo.com/dealing-with-the-problem-of-passwords), I noticed that the hashes presented to illustrate the concept of a hex digest representing an MD5 hash of a password didn’t look right.

[quote]For example, given a password “frenchfries”, MD5 generates this hash value (converted to hex):

D32447E202465467E38E24C[/quote]

The sample project is located here: http://41160df63757fc043cfd-66287f38a83954e31a54d1dbe33e0650.r4.cf2.rackcdn.com/BlogExamples/PasswordHashing.xojo_binary_project

Debugging the ConvertToHex method with a breakpoint on Next shows that mb.Int8Value(b).ToHex sometimes returns a single character instead of the usual two for some reason.

Dim hex As Text
  
  For b As Int8 = 0 To mb.Size - 1
    hex = hex + mb.Int8Value(b).ToHex
  Next
  
  Return hex

I tested the project and it gave the same result - 23 characters long rather than 32.

Is anyone able to shed light on why exactly this happens? Is it a bug in the new framework, or is it a bug in the code in the project? An equivalent test using the classic framework’s Crypto module and EncodeHex on the binary result gave me a 32 character digest as expected…

Any help or insight would really be appreciated. I know I can use the classic framework to get a correct result, but my OCD tendencies are kicking in and I’d like to understand why there’s a difference.

Thanks very much in advance.

Probably the leading zeros are missing. Check the docs. There is a minimumDigits parameter: ToHex(Optional minimumDigits As Integer) As Text.

Thank you, Eli. Worked like a charm. :slight_smile:

Keep in mind that ToHex is for converting numbers, whereas EncodeHex is for data.

I’ve updated the blog post and project to use ToHex(2) so the hex values look better.