Calculate MD5 hash in logic

Discussion in 'C-Touch/HomeGate/SchedulePlus/PICED Software' started by ssaunders, May 31, 2013.

  1. ssaunders

    ssaunders

    Joined:
    Dec 17, 2008
    Messages:
    243
    Likes Received:
    35
    Location:
    Melbourne
    Hoping someone has solved this before...

    In C or C++ I can implement code to calculate an MD5 hash from a string quite easily. In Pascal logic the algorithms I have in my kit bag can't translate... The issue is that unsigned 32-bit integer values are used in the calculation, but the implementation of Pascal in logic does not support this.

    Use is on a C-Touch, so running an external program is out as an option.

    Any brainy ideas?
     
    Last edited by a moderator: May 31, 2013
    ssaunders, May 31, 2013
    #1
  2. ssaunders

    ashleigh Moderator

    Joined:
    Aug 4, 2004
    Messages:
    2,398
    Likes Received:
    26
    Location:
    Adelaide, South Australia
    Perhaps you can start with why you want to do this in logic code?
     
    ashleigh, May 31, 2013
    #2
  3. ssaunders

    ssaunders

    Joined:
    Dec 17, 2008
    Messages:
    243
    Likes Received:
    35
    Location:
    Melbourne
    Great question.

    I'm requesting information from a web site that requires a user ID along with an MD5 hash of a string for a successful request. Essentially the MD5 hash is an encoded password/key.

    Unfortunately the 'password' changes on a daily basis (date is part of the input string to the hash), so a fixed MD5 constant won't do it. Well, it will do it for 24h ;)

    My thinking so far outside the square: Install PHP on a local web server and use a script there to generate a key using md5() based on an HTTP post from the CTouch, then retrieve the key with a get. This would only need to happen once a day when the clock rolls over. Down-side is reliance on a server external to the touch. Installing PHP as I write to try it out...
     
    ssaunders, May 31, 2013
    #3
  4. ssaunders

    ashleigh Moderator

    Joined:
    Aug 4, 2004
    Messages:
    2,398
    Likes Received:
    26
    Location:
    Adelaide, South Australia
    Hmmmm...

    What you suggest could probably be done very easily by a small extension to the logic engine to allow it to calculate MD5 hashes - certainly for things with big processors - PICED, Colour C-Touch. For the others, probably no-go.

    Doing it in logic may also be possible but messy and slow.

    Perhaps if you post the C code that you would use, somebody can suggest suitable techniques or hacks. Who knows.... somebody might have done this - or be able to give some pointers as to how to do it.
     
    ashleigh, May 31, 2013
    #4
  5. ssaunders

    ssaunders

    Joined:
    Dec 17, 2008
    Messages:
    243
    Likes Received:
    35
    Location:
    Melbourne
    Small extension would be grand. Don't believe I can do that, though... (Can I?)

    The C code I base my MD5 calc on is an RSA implementation (a reference is http://dollar.ecom.cmu.edu/sec/cryptosource.htm). Would do my head in to try and attempt this with a 16 bit code change or fudged +/- translations of the constants involved. Not even sure it would be possible given the algorithm. It references 32-bit uint constants like '4294925233', which is well beyond the signed reach of the logic engine.
     
    ssaunders, May 31, 2013
    #5
  6. ssaunders

    ssaunders

    Joined:
    Dec 17, 2008
    Messages:
    243
    Likes Received:
    35
    Location:
    Melbourne
    Should work...

    Put the following on a local (or remote!) web server at /md5/phpinfo.php:

    <html>
    <body>
    <?php
    $md5ed = md5($_GET['key']);
    echo $md5ed;
    ?>
    </body>

    Bang this in the request URL in logic:

    http://(webserver)/md5/phpinfo.php?key=1934754732mypass

    Expect the reply:

    5927cde6104903fcc431be36b3db3dd3

    Job's done.

    Would have been nice to do it in the Touch Colour all by itself without relying on an external web server, but it is what it is. Unfortunately not everyone is going to have a handy (open) web server on site they can use for this kind of stuff.

    (PS... BRILLIANT work in putting HTTP functions in the Wiser now, guys! Love it. Doing this current piece on the CTouch, though, but lining up my Wiser for some HTTP lovin' :D)
     
    ssaunders, May 31, 2013
    #6
  7. ssaunders

    ssaunders

    Joined:
    Dec 17, 2008
    Messages:
    243
    Likes Received:
    35
    Location:
    Melbourne
    Did work.

    Final code:

    executed daily -
    Code:
      DecodeDate(date, current_year,current_month,current_day);
      current_year := current_year mod 100;
      Format(s,{date_coded_function}:1,'apassword');
      Format(s,'http://192.168.1.2/md5/gethash.php?key=',s);
      GetHTTPData(s);
      delay(3);
      ReadHTTPData(s);
      Format(credentials_for_website,'&uid=','{userid},'&key=',s);
    
    contents of gethash.php -
    Code:
    <?php
    $md5ed = md5($_GET['key']);
    echo $md5ed;
    ?>
    
    :D

    Just need to add some contingency code should the web server go down...

    In the words of Borat: Great success.
     
    ssaunders, May 31, 2013
    #7
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.