md5

Discussion in 'Questions (Windows Mobile)' started by Elrick, Mar 12, 2008.

  1. Elrick

    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?
     
  2. LineCutter

    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:
     
  3. Leginus

    Leginus Member Licensed User

  4. Elrick

    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?
     
  5. agraham

    agraham Expert Licensed User

    45 minutes rather than seconds :)

    No guarantees but I think it works OK. Needs .NET 2.0 on device and desktop.
     

    Attached Files:

  6. Leginus

    Leginus Member Licensed User

    so what color is your cape? :)
     
  7. agraham

    agraham Expert Licensed User

    Capes are for cissies, I prefer 3 litre V6 Fords :)
     
  8. Elrick

    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:
     
  9. agraham

    agraham Expert Licensed User

    I found this php example

    Code:
    <?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

    Code:
    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: Mar 13, 2008
  10. Elrick

    Elrick Member Licensed User

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

    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:

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

    Sub StringToMD5(string)
       
       
    Bit.New1
       MD5.New1
       data() = 
    bit.StringToBytes(string0, 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:


    Code:
    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...? :

    Code:
    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
     
  12. agraham

    agraham Expert Licensed User

    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.

    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.
     
Loading...