43 lines
1.1 KiB
PHP
43 lines
1.1 KiB
PHP
|
<?php
|
||
|
|
||
|
$string = ' string to be encrypted ';
|
||
|
//hashPassword($string);
|
||
|
function hashPassword($value){
|
||
|
$key = 'password to (en/de)crypt';
|
||
|
$algo_ini = mcrypt_create_iv(
|
||
|
mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC),
|
||
|
MCRYPT_DEV_URANDOM
|
||
|
);
|
||
|
$hash_pass= base64_encode(
|
||
|
$algo_ini .
|
||
|
mcrypt_encrypt(
|
||
|
MCRYPT_RIJNDAEL_128,
|
||
|
hash('sha256', $key, true),
|
||
|
$value,
|
||
|
MCRYPT_MODE_CBC,
|
||
|
$algo_ini
|
||
|
)
|
||
|
);
|
||
|
return $hash_pass;
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
function unhashPassword($hash_pass)
|
||
|
{
|
||
|
$key = 'password to (en/de)crypt';
|
||
|
$data = base64_decode($hash_pass);
|
||
|
$algo_ini = substr($data, 0, mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC));
|
||
|
$unhash_pass = rtrim(
|
||
|
mcrypt_decrypt(
|
||
|
MCRYPT_RIJNDAEL_128,
|
||
|
hash('sha256', $key, true),
|
||
|
substr( $data, mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC)),
|
||
|
MCRYPT_MODE_CBC,
|
||
|
$algo_ini
|
||
|
),
|
||
|
"\0"
|
||
|
);
|
||
|
return $unhash_pass;
|
||
|
}
|