note 43328 deleted from function.mail by aidan
| From: | aidan@php.net | Date: | Sun, 08 Aug 2004 15:35:14 +0000 |
| Subject: | note 43328 deleted from function.mail by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-74233@lists.php.net to get a copy of this message | ||
Note Submitter: just_somedood@yahoo.com
----
For those that have thier own SMTP socket functions, and need to use SMTP AUTH, but are having a
hard time with the CRAM-MD5 authentication mechanism, here's some (hopefully) helpful code:
<?php
function make_ipad($size)
{
//as defined in RFC #2104
$str = "";
for ($i=0;$i<$size;$i++)
$str .= chr(0x36);
return ($str);
}
function make_opad($size)
{
//as defined in RFC #2104
$str = "";
for ($i=0;$i<$size;$i++)
$str .= chr(0x5C);
return($str);
}
function padSecret ($secret)
{
//as defined in RFC #2104
for ($i=strlen($secret);$i<64;$i++)
{
//just null pad (character 0) the string
$secret .= chr(0x00);
}
return ($secret);
}
function keyedMD5($key,$data)
{
//this does a 'keyed' MD5 hash, as per the HMAC RFC #2104
$innerText = padSecret($key)^make_ipad(64);
$innerHash = pack("H*",md5($innerText . $data));
$done = md5 ((padSecret($key)^make_opad(64)) . $innerHash);
return ($done);
}
function cramMD5_encode ($user,$pass,$text)
{
//as defined in RFC 2195
//text should NOT be base64 encoded (decode before it comes into this function)
//result is the CRAM-MD5 string, encoded in base64
$encoded = keyedMD5($pass,$text);
return (base64_encode($user . " " . $encoded));
}
?>
The SMTP server i use won't do plaintext AUTH, and CRAM-MD5 seems to be widely used. Anyway,
it works for me :)