What i am trying to get is
- encrypt a password + salt with libsodium
- store it to a database
- read it and decrypt it (getting the plain password back for authentication)
I got a list of salts that i want to use to encrypt / decrypt my password. When i encrypt the password i get a hash back so that one seems to work but at decrypting i always get false as return value.
Am i using the wrong methods for encrypt / decrypt with libsodium or am i completely driving in the wrong direction?
My source for encrypt / decrypt:
function encrypt_libsodium($to_encrypt, $salt_to_use){
if(!$data || !$salt_to_use){
return null;
}
//get stored salt
$this->key_ = substr(md5($this->pw_key[$salt_to_use].'_'), 0, $this->ks);
//some libsodium specific stuff
$out_len = \Sodium\CRYPTO_SIGN_SEEDBYTES;
$ops_limit = \Sodium\CRYPTO_PWHASH_SCRYPTSALSA208SHA256_OPSLIMIT_INTERACTIVE;
$mem_limit =\Sodium\CRYPTO_PWHASH_SCRYPTSALSA208SHA256_MEMLIMIT_INTERACTIVE;
//create hash using libsodium
$hash = \Sodium\crypto_pwhash($out_len, $to_encrypt, $this->key_,$ops_limit, $mem_limit);
return $hash;
}
function decrypt_libsodium($hash, $salt_to_use){
if(!$hash || !$what){
return null;
}
//get stored salt
$this->key_ = substr(md5($this->pw_key[$salt_to_use].'_'), 0, $this->ks);
//get verification hash
$decrypted = \Sodium\crypto_pwhash_scryptsalsa208sha256_str_verify($this->key_, $hash);
return $decrypted;
}
I appreciate any help!
regards Dom