add seed_pass_hash() and seed_pass_verify()

This commit is contained in:
Uwe Steinmann 2020-07-30 10:55:13 +02:00
parent 245e54f893
commit 05dcde6096

View File

@ -598,7 +598,7 @@ function getBaseUrl() { /* {{{ */
return "http".((isset($_SERVER['HTTPS']) && (strcmp($_SERVER['HTTPS'],'off')!=0)) ? "s" : "")."://".$_SERVER['HTTP_HOST']; return "http".((isset($_SERVER['HTTPS']) && (strcmp($_SERVER['HTTPS'],'off')!=0)) ? "s" : "")."://".$_SERVER['HTTP_HOST'];
} /* }}} */ } /* }}} */
function getToken($length){ function getToken($length){ /* {{{ */
$token = ""; $token = "";
$codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$codeAlphabet.= "abcdefghijklmnopqrstuvwxyz"; $codeAlphabet.= "abcdefghijklmnopqrstuvwxyz";
@ -610,21 +610,41 @@ function getToken($length){
} }
return $token; return $token;
} } /* }}} */
class SeedDMS_CSRF { /**
* Hash a password
*
* @param string $password
* @return string hashed password
*/
function seed_pass_hash($password) { /* {{{ */
return md5($password);
} /* }}} */
/**
* Verify a password
*
* @param string $password
* @return string hashed password
*/
function seed_pass_verify($password, $hash) { /* {{{ */
return $hash == md5($password);
} /* }}} */
class SeedDMS_CSRF { /* {{{ */
protected $secret; protected $secret;
public function __construct($secret) { public function __construct($secret) { /* {{{ */
$this->secret = $secret; $this->secret = $secret;
} } /* }}} */
public function create_api_key() { public function create_api_key() { /* {{{ */
return base64_encode($this->encrypt(time().'|'.$_SERVER['REMOTE_ADDR'])); // !change if you dont want IP check return base64_encode($this->encrypt(time().'|'.$_SERVER['REMOTE_ADDR'])); // !change if you dont want IP check
} } /* }}} */
public function check_api_key($key, $timeout = 5) { public function check_api_key($key, $timeout = 5) { /* {{{ */
if (empty($key)) exit('Invalid Key'); if (empty($key)) exit('Invalid Key');
$keys = explode('|', $this->decrypt(base64_decode($key))); $keys = explode('|', $this->decrypt(base64_decode($key)));
@ -634,9 +654,9 @@ class SeedDMS_CSRF {
$keys[0] >= (time() - $timeout) && $keys[0] >= (time() - $timeout) &&
$keys[1] == $_SERVER['REMOTE_ADDR'] // !change if you dont want IP check $keys[1] == $_SERVER['REMOTE_ADDR'] // !change if you dont want IP check
); );
} } /* }}} */
public function encrypt($string, $key = 'PrivateKey', $method = 'AES-256-CBC') { public function encrypt($string, $key = 'PrivateKey', $method = 'AES-256-CBC') { /* {{{ */
// hash // hash
$key = hash('sha256', $key); $key = hash('sha256', $key);
// create iv - encrypt method AES-256-CBC expects 16 bytes // create iv - encrypt method AES-256-CBC expects 16 bytes
@ -645,9 +665,9 @@ class SeedDMS_CSRF {
$output = openssl_encrypt($string, $method, $key, 0, $iv); $output = openssl_encrypt($string, $method, $key, 0, $iv);
// encode // encode
return base64_encode($output); return base64_encode($output);
} } /* }}} */
public function decrypt($string, $key = 'PrivateKey', $method = 'AES-256-CBC') { public function decrypt($string, $key = 'PrivateKey', $method = 'AES-256-CBC') { /* {{{ */
// hash // hash
$key = hash('sha256', $key); $key = hash('sha256', $key);
// create iv - encrypt method AES-256-CBC expects 16 bytes // create iv - encrypt method AES-256-CBC expects 16 bytes
@ -656,8 +676,8 @@ class SeedDMS_CSRF {
$string = base64_decode($string); $string = base64_decode($string);
// decrypt // decrypt
return openssl_decrypt($string, $method, $key, 0, $iv); return openssl_decrypt($string, $method, $key, 0, $iv);
} } /* }}} */
} } /* }}} */
//$CSRF = new SeedDMS_CSRF($settings->_encryptionKey); //$CSRF = new SeedDMS_CSRF($settings->_encryptionKey);
//$kkk = $CSRF->create_api_key(); //$kkk = $CSRF->create_api_key();