123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- define('SHA1_RESULTLEN', (160/8));
- define('SHA256_RESULTLEN', (256 / 8));
- define('CRAM_MD5_CONTEXTLEN', 32);
- define('MD5_RESULTLEN', (128/8));
- define('MD4_RESULTLEN', (128/8));
- define('LM_HASH_SIZE', 16);
- define('NTLMSSP_HASH_SIZE', 16);
- class DovecotCrypt extends Crypt {
- private $errormsg = [];
- private $salt_chars = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
-
- public $password_schemes = array(
- 'CRYPT' => array('NONE', 0, 'crypt_verify', 'crypt_generate'),
- 'MD5' => array('NONE', 0, 'md5_verify', 'md5_generate'),
-
- 'SHA' => array('BASE64', SHA1_RESULTLEN, null, 'sha1_generate'),
- 'SHA1' => array('BASE64', SHA1_RESULTLEN, null, 'sha1_generate'),
-
-
-
-
- 'PLAIN' => array('NONE', 0, null, 'plain_generate'),
- 'CLEARTEXT' => array('NONE', 0, null, 'plain_generate'),
- 'CRAM-MD5' => array('HEX', CRAM_MD5_CONTEXTLEN, null, 'cram_md5_generate'),
-
-
-
-
-
-
-
-
-
-
- );
- public function crypt($algorithm) {
- if (!array_key_exists($algorithm, $this->password_schemes)) {
- $this->errormsg[] = "This password scheme isn't supported. Check our Wiki!";
- return false;
- }
- $scheme = $this->password_schemes[$algorithm];
- $func = '__'.$scheme[3];
- $this->password = $this->$func($this->plain);
-
- return true;
- }
- public function verify($algorithm, $password) {
- if (!array_key_exists($algorithm, $this->password_schemes)) {
- $this->errormsg[] = "This password scheme isn't supported. Check our Wiki!";
- return false;
- }
- $scheme = $this->password_schemes[$algorithm];
- if ($scheme[2] == null) {
- $this->errormsg[] = "This password scheme doesn't support verification";
- return false;
- }
- $func = '__'.$scheme[2];
- return $this->$func($this->plain, $password);
- }
- private function __crypt_verify($plaintext, $password) {
- $crypted = crypt($plaintext, $password);
- return strcmp($crypted, $password) == 0;
- }
- private function __crypt_generate($plaintext) {
- $password = crypt($plaintext);
- return $password;
- }
- private function __md5_generate($plaintext) {
- return $plaintext;
- }
- private function __sha1_generate() {
- }
- private function __plain_generate() {
- }
- private function __cram_md5_generate($plaintext) {
-
-
-
- $salt = $plaintext;
- if (function_exists('hash_hmac')) {
- return hash_hmac('md5', $plaintext, $salt);
- } else {
- return custom_hmac('md5', $plaintext, $salt);
- }
- }
-
- public function custom_hmac($algo, $data, $key, $raw_output = false) {
- $algo = strtolower($algo);
- $pack = 'H'.strlen($algo('test'));
- $size = 64;
- $opad = str_repeat(chr(0x5C), $size);
- $ipad = str_repeat(chr(0x36), $size);
- if (strlen($key) > $size) {
- $key = str_pad(pack($pack, $algo($key)), $size, chr(0x00));
- } else {
- $key = str_pad($key, $size, chr(0x00));
- }
- for ($i = 0; $i < strlen($key) - 1; $i++) {
- $opad[$i] = $opad[$i] ^ $key[$i];
- $ipad[$i] = $ipad[$i] ^ $key[$i];
- }
- $output = $algo($opad.pack($pack, $algo($ipad.$data)));
- return ($raw_output) ? pack($pack, $output) : $output;
- }
- }
|