authentication.anubis
3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* 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)).