authentication.anubis 3.1 KB
/*
 * Created by PyramIDE.
 * User: ricard
 * Date: 07/03/2009
 * Time: 21:42
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
                                 
read tools/basis.anubis
read system/bytearray.anubis
read system/string.anubis

 /** Give a timestamp according to RFC2822. this sort of timestamp 
  * is use for CRAM authentication (Challenge Reponse Authentication Mechanism)
  * this time timestamp must unique and not predictable to ensure security
  * mechanism of authentication.
  * @param  dummy One meaning nothing
  * @return Sring of the timestamp
  */ 
public define String
  get_time_stamp
  (
    String domain
  )=
  with time = (UTime) unow,
  "<"+virtual_machine_id+"."+time.seconds+"@mail."+domain+">".

 /** 
  */ 
public define Bool
  apop_md5
  (
    String          time_stamp,
    String          password,
    String          given_hash
  ) =
  with my_stamp = time_stamp + password,
  my_hash = to_lower(to_ascii(md5(to_byte_array(my_stamp)))),
//  print("time_stamp "+ time_stamp + "\n");
//  print("given_hash " + given_hash + "\n");
//  print("my_hash    " + my_hash + "\n");
  if to_lower(given_hash) = my_hash then
    true
  else
    false.



define String
  hmac_md5_compute
  (
    ByteArray  data,           //message to be crypted
    ByteArray  key             //this is share secret key (i.e. password)
  ) =
  with ba_data = constant_byte_array(64, 0),
       ba_ipad = constant_byte_array(64, 0x36),
       ba_opad = constant_byte_array(64, 0x5c),
  //put key into ByteArray
  //but if key is longer than 64 bytes, then we must apply md5 on it and put it into ByteArray
  with  ba_key = fill_ByteArray(ba_data, if length(key) > 64 then md5(key) else key),
  key_ipad = ba_key : ba_ipad,             // XOR key with ipad
  key_opad = ba_key : ba_opad,             // XOR key with opad
  to_lower(to_ascii(md5(key_opad + (md5(key_ipad + data))))).     // md5(K (+) ipad) and merge with data
  

public define Bool
  hmac_md5_check
  (
    ByteArray  data,           //message to be crypted
    ByteArray  key,            //this is share secret key (i.e. password)
    String     given_digest    //The hash given by the user who want to be authenticated 
  ) =
  if to_lower(given_digest) = hmac_md5_compute(data, key) then
//    print("HMAC-MD5 Success \n");
    true
  else
//    print("HMAC-MD5 Failed \n");
//    print("  My Hash = " + my_hash + "\n");
//    print("User Hash = " + given_digest + "\n");
    false.

public define Bool
  hmac_md5_check
  (
    String  data,           //message to be cripted
    String  key,            //this is share secret key (i.e. password)
    String  given_digest    //The hash given by the user who want to be authenticated 
  ) =
  hmac_md5_check(to_byte_array(data), to_byte_array(key), given_digest).

public define String
  hmac_md5_compute
  (
    String  data,           //message to be cripted
    String  key             //this is share secret key (i.e. password)
  ) =
  hmac_md5_compute(to_byte_array(data), to_byte_array(key)).