md5

Elrick

Member
Licensed User
Hello! I need to encrypt a string using md5 algorithm. "Crypto" library can't encrypt it in pure md5 (it uses "secret key") How can i do this?
 

LineCutter

Active Member
Licensed User
Wait for it!

You can find the pseudocode for MD5 on wikipedia & roll your own... or wait about 45 seconds & Agraham will produce a library that does MD5, SHA, SHA1 & introduces a new "make tea" routine using an optional sugar argument :)

:sign0013:
 

Elrick

Member
Licensed User
Nice :) I never doubted Agraham's surprising abilities:) Maybe he can make additional Basic4PPC library what have only two function - make our world better and make me rich? :sign0060: Anyway, are there only two ways which were offered by LineCutter?
 

Elrick

Member
Licensed User
Hmm... Agraham, you really did it! Maybe you'll think about my offer (library for Basic4PPC what makes me rich and famous)? :) Nevertheless, there are difference between results of your library and standard php function, but it is the other question, the question about codings :) Thank you, Agraham:sign0188:
 

agraham

Expert
Licensed User
Nevertheless, there are difference between results of your library and standard php function,
I found this php example

B4X:
<?php
$str = 'apple';

if (md5($str) === '1f3870be274f6c49b3e31a0c6728957f') {
    echo "Would you like a green or red apple?";
    exit;
}
?>
My test program returns "7f952867c1ae3b34960c4f27be70381f". Note that this is actually correct but is in the opposite byte order. It looks from this example that the byte order convention for the php MD5() function is opposite to what I assumed so my HashToHex function should be

B4X:
Sub HashToHex
  str = ""
  For i = 0 To ArrayLen(hash())-1
    If hash(i) < 16 Then
      str = str & "0"
    End If
    str = str & bit.DecToHex(hash(i))
  Next
  Return str
End Sub
Which returns "1f3870be274f6c49b3e31a0c6728957f" for the input string "apple"

EDIT :- Amended HashToHex code as I noticed that bit.DecToHex() doesn't always return two characters.
 
Last edited:

Elrick

Member
Licensed User
Agraham, you answered my question before i asked it :) This is exactly what i'm looking for. Thank you again!
 

TWELVE

Active Member
Licensed User
Hello agraham,

thanks for the lib.Works great.I use now this procedure / sub to calculate the MD5 of a string:

B4X:
Sub Globals
...
Dim data()
Dim hash()
...
End Sub

Sub StringToMD5(string)
   
   Bit.New1
   MD5.New1
   data() = bit.StringToBytes(string, 0, StrLength(string))   
   ' ComputeHash(byte array, start index, count)
   hash() = MD5.ComputeHash(data(), 0, ArrayLen(data()))
     
   str = ""
    For i = 0 To ArrayLen(hash())-1
     If hash(i) < 16 Then
      str = str & "0"
     End If
    str = str & bit.DecToHex(hash(i))
   Next
   Return str

So this is then like a blackbox.I don't want to know what's in there, just need to know what comes out if i put in something..:)) Just like this:

MD5HashOfSomeThing = StringToMD5(string)


Question: can you include this into the lib..? It would be handy if one just calls the lib function this way, without this code around, like this:


B4X:
Sub MySub   
   
             Bit.New1
   MD5.New1
             MD5HashStringofmyString = MD5.StringToMD5(string)
End Sub

Another question:

The auto completion revealed to me your MD5 lib has these functions:

md5.New1
md5.ComputeHash
md5.ToString
md5.Dispose

What does the .ToString do..? Is it the same like this code does...? :

B4X:
   str = ""
    For i = 0 To ArrayLen(hash())-1
     If hash(i) < 16 Then
      str = str & "0"
     End If
    str = str & bit.DecToHex(hash(i))
   Next
   Return str

Nevertheless your lib and the code for conversion to Hex string works fine so far..!


Cheers


TWELVE
 

agraham

Expert
Licensed User
Question: can you include this into the lib..?
I could but I probably won't as I believe that for flexibility a library should only implement the core functionality missing from B4PPC. As long as the additional coding required can be efficiently done in B4PPC then I don't like "freezing" it in a library.

What does the .ToString do..? Is it the same like this code does...? :
All objects from libraries have this method. It is a .NET thing and if you invoke it like "MsgBox(MD5.ToString)" it usually returns the type name of the object. Dispose gets rid of the object, another .NET thing - you will probably never use it.
 
Top