Shop OBEX P1 Docs P2 Docs Learn Events
md5 object problem — Parallax Forums

md5 object problem

Bobb FwedBobb Fwed Posts: 1,119
edited 2009-09-15 16:46 in Propeller 1
I can't get the MD5 object to work, there are no samples or notes on how to use it, so this is what I am trying (looks correct anyway):


CON
  MAX_STR_LEN = 25

OBJ
  MD5 : "MD5"

VAR
  byte nd[noparse][[/noparse]MAX_STR_LEN]
  byte nstr[noparse][[/noparse]MAX_STR_LEN]

PUB do_hash
  bytemove(@nd, string("abcdefghijklmn 1234567890"), MAX_STR_LEN)
  MD5.hash(@nd, MAX_STR_LEN, @nstr)



and what I get in nstr is: one line down, and 11 or so in: 8vA_ Ұ
not, the 16 byte string I was expecting.
I've gotten other weird results with other input strings. What am I doing wrong?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-10-06 17:00
    VAR
       byte nd[noparse][[/noparse]MAX_STR_LEN]
       byte nstr[noparse][[/noparse]MD5#HASH_LENGTH]
    
    PUB do_hash | str
       str := string("abcdefghijklmn 1234567890") ' Save the address of the constant string
       bytemove(@nd,str,strsize(str)) ' Use the actual length of the string
       MD5.hash(@nd,strsize(str),@nstr) ' Again use the actual length
    



    The do_hash call will create a 16 byte hash in nstr. Note that this hash is not in displayable form.
    It consists of 16 bytes with values from 0 to 255. You have to display the bytes in hexadecimal or
    decimal form to make any sense of them. There are hexadecimal display routines in most of the
    display objects (serial.hex(<value>,<#digits>) for example).
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2008-10-06 17:32
    My problem wasn't the coding (so much) but the return, I have always just seen printable versions of an MD5 checksum, now it makes more sense, thanks.
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2008-10-06 17:39
    so I display it with
    REPEAT MD5#HASH_LENGTH
        DEBUG.hex(nstr[noparse][[/noparse]i++], 2)
    



    It seems to return something legit, but it doesn't match the MD5 I know works (on PSPad and PHP).
    Prop MD5: B1963AC8BCB9905FE513D45A98633210
    PSPad MD5: 63d7f5da7e0c01fec14ca1cf9016541f

    any ideas?

    Post Edited (Bobb Fwed) : 10/6/2008 5:49:46 PM GMT
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2008-10-06 17:44
    Also, the last bytes from the Prop are always $32, $10, and the one before those two seems to usually end in 3
    Something doesn't seem right.

    Post Edited (Bobb Fwed) : 10/6/2008 5:51:26 PM GMT
  • hippyhippy Posts: 1,981
    edited 2008-10-07 10:43
    Did you change your code to how Mike suggested ? If not it would be calculating MD5 using data other than it should be.
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2008-10-07 14:20
    Yes I did.
  • Farmer ScottFarmer Scott Posts: 30
    edited 2009-09-15 16:14
    I'm not sure I understand why, in Mike's comment, he changed the routine to assign a string value to str, and then used bytemove to copy str to nd.

    I've done it both ways, and see the bytemove is required to get the correct hash, just don't understand why.

    Can anyone elaborte?

    Thanks,

    Scott
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2009-09-15 16:46
    This works without the move.
    CON
    
      _CLKMODE      = XTAL1
      _XINFREQ      = 5_000_000                          ' 5MHz Crystal
      
    OBJ
    
      DEBUG  : "FullDuplexSerial"
      MD5    : "MD5"
    
    VAR
    
      byte nstr[noparse][[/noparse]MD5#HASH_LENGTH]
    
    PUB Main
    
      DEBUG.start(31, 30, 0, 57600)
      waitcnt(clkfreq + cnt)  
      DEBUG.tx($0D)
    
      do_hash
    
    PUB do_hash | str, i
      str := string("abcdefghijklmn 1234567890")
      MD5.hash(str, strsize(str), @nstr)
    
      i~
      REPEAT MD5#HASH_LENGTH
        DEBUG.hex(nstr[noparse][[/noparse]i++], 2)
      DEBUG.tx($D)
    
    



    But this is probably what should be used.
    CON
    
      _CLKMODE      = XTAL1
      _XINFREQ      = 5_000_000                          ' 5MHz Crystal
    
      MAX_STR_LEN = 64
      
    OBJ
    
      DEBUG  : "FullDuplexSerial"
      MD5    : "MD5"
    
    VAR
    
      byte nd[noparse][[/noparse]MAX_STR_LEN]
      byte nstr[noparse][[/noparse]MD5#HASH_LENGTH]
    
    PUB Main
    
      DEBUG.start(31, 30, 0, 57600)
      waitcnt(clkfreq + cnt)  
      DEBUG.tx($0D)
    
      do_hash
    
    PUB do_hash | str, i
      str := string("abcdefghijklmn 1234567890")
      bytemove(@nd, str, strsize(str))
      MD5.hash(@nd, strsize(str), @nstr)
    
      i~
      REPEAT MD5#HASH_LENGTH
        DEBUG.hex(nstr[noparse][[/noparse]i++], 2)
      DEBUG.tx($D)
    
    



    Both output the correct value: "63D7F5DA7E0C01FEC14CA1CF9016541F"

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    April, 2008: when I discovered the answers to all my micro-computational-botherations!
Sign In or Register to comment.