|
|
|
@ -21,27 +21,27 @@ function initialiser_cle(){
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function chiffrer($clair){ |
|
|
|
|
$cipher="AES-128-CBC"; |
|
|
|
|
function chiffrer($clair, $cle=false, $cipher="AES-128-CBC"){ |
|
|
|
|
$cle = ( $cle ) ? $cle : $GLOBALS['cle_secrete']; |
|
|
|
|
$ivlen = openssl_cipher_iv_length($cipher); |
|
|
|
|
$iv = openssl_random_pseudo_bytes($ivlen); |
|
|
|
|
$chiffre_raw = openssl_encrypt($clair, $cipher, $GLOBALS['cle_secrete'], $options=OPENSSL_RAW_DATA, $iv); |
|
|
|
|
$hmac = hash_hmac('sha256', $chiffre_raw, $GLOBALS['cle_secrete'], $as_binary=true); |
|
|
|
|
$chiffre_raw = openssl_encrypt($clair, $cipher, $cle, $options=OPENSSL_RAW_DATA, $iv); |
|
|
|
|
$hmac = hash_hmac('sha256', $chiffre_raw, $cle, $as_binary=true); |
|
|
|
|
$chiffre = base64_encode( $iv.$hmac.$chiffre_raw ); |
|
|
|
|
spip_log("chiffrer($clair)=$chiffre", _LOG_DEBUG); |
|
|
|
|
return $chiffre; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function dechiffrer($chiffre){ |
|
|
|
|
$cipher="AES-128-CBC"; |
|
|
|
|
function dechiffrer($chiffre, $cle=false, $cipher="AES-128-CBC"){ |
|
|
|
|
$cle = ( $cle ) ? $cle : $GLOBALS['cle_secrete']; |
|
|
|
|
$c = base64_decode($chiffre); |
|
|
|
|
$ivlen = openssl_cipher_iv_length($cipher); |
|
|
|
|
$iv = substr($c, 0, $ivlen); |
|
|
|
|
$hmac = substr($c, $ivlen, $sha2len=32); |
|
|
|
|
$chiffre_raw = substr($c, $ivlen+$sha2len); |
|
|
|
|
$clair = openssl_decrypt($chiffre_raw, $cipher, $GLOBALS['cle_secrete'], $options=OPENSSL_RAW_DATA, $iv); |
|
|
|
|
$clair = openssl_decrypt($chiffre_raw, $cipher, $cle, $options=OPENSSL_RAW_DATA, $iv); |
|
|
|
|
spip_log("dechiffrer($chiffre)=$clair", _LOG_DEBUG); |
|
|
|
|
$calcmac = hash_hmac('sha256', $chiffre_raw, $GLOBALS['cle_secrete'], $as_binary=true); |
|
|
|
|
$calcmac = hash_hmac('sha256', $chiffre_raw, $cle, $as_binary=true); |
|
|
|
|
if ( hash_equals($hmac, $calcmac) ){ // timing attack safe comparison |
|
|
|
|
return $clair; |
|
|
|
|
} |
|
|
|
|