ushahidi_swift /system/vendor/swift/Swift/Authenticator/CRAMMD5.php

Language PHP Lines 74
MD5 Hash fe5ff3f695359b2bda22ebd792f370d1
Repository https://github.com/mjvanderveen/ushahidi_swift.git View Raw File View Project SPDX
 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
<?php

/**
 * Swift Mailer CRAM-MD5 Authenticator Mechanism
 * Please read the LICENSE file
 * @author Chris Corbyn <chris@w3style.co.uk>
 * @package Swift_Authenticator
 * @license GNU Lesser General Public License
 */

require_once dirname(__FILE__) . "/../ClassLoader.php";
Swift_ClassLoader::load("Swift_Authenticator");

/**
 * Swift CRAM-MD5 Authenticator
 * This form of authentication is a secure challenge-response method
 * @package Swift_Authenticator
 * @author Chris Corbyn <chris@w3style.co.uk>
 */
class Swift_Authenticator_CRAMMD5 implements Swift_Authenticator
{
  /**
   * Try to authenticate using the username and password
   * Returns false on failure
   * @param string The username
   * @param string The password
   * @param Swift The instance of Swift this authenticator is used in
   * @return boolean
   */
  public function isAuthenticated($user, $pass, Swift $swift)
  {
    try {
      $encoded_challenge = substr($swift->command("AUTH CRAM-MD5", 334)->getString(), 4);
      $challenge = base64_decode($encoded_challenge);
      $response = base64_encode($user . " " . self::generateCRAMMD5Hash($pass, $challenge));
      $swift->command($response, 235);
    } catch (Swift_ConnectionException $e) {
      $swift->reset();
      return false;
    }
    return true;
  }
  /**
   * Return the name of the AUTH extension this is for
   * @return string
   */
  public function getAuthExtensionName()
  {
    return "CRAM-MD5";
  }
  /**
   * Generate a CRAM-MD5 hash from a challenge
   * @param string The string to get a hash from
   * @param string The challenge to use to make the hash
   * @return string
   */
  public static function generateCRAMMD5Hash($password, $challenge)
  {
    if (strlen($password) > 64)
      $password = pack('H32', md5($password));

    if (strlen($password) < 64)
      $password = str_pad($password, 64, chr(0));

    $k_ipad = substr($password, 0, 64) ^ str_repeat(chr(0x36), 64);
    $k_opad = substr($password, 0, 64) ^ str_repeat(chr(0x5C), 64);

    $inner  = pack('H32', md5($k_ipad.$challenge));
    $digest = md5($k_opad.$inner);

    return $digest;
  }
}
Back to Top