Is it possible to set the Argon2 Hash length in PHP ?
updated question + answer on github: https://github.com/charlesportwoodii/php-argon2-ext/issues/9
Problem: 2 Apps communicating and doing encryption. Now I want both apps to be able to create the same hash lengths.
The java app is using a hash_len of 24 while the php app has the default of 32.
The Java app is out of my control so how can I generate the same hash length in php ?
Those are the official php options:
memory cost
time cost
threads
the standard php lib doesn't seem to support anything else, as far as I can see, any workarounds ?
in Java we can do something along those lines:
argon2.hash_password(
"Password",
memory_cost=512,
time_cost=2,
parallelism=2,
hash_len=24
)
In PHP
$options = [
"memory_cost" => 1024,
"time_cost" => 2,
"threads" => 2
];
password_hash('test', PASSWORD_ARGON2I, $options);
All I could think of as a workaround atm is maybe a python package which generates the hash with the length.
Python/PHP Example:
PHP
$command = "python /path/to/argon2i_script.py 2>&1";
$pid = popen( $command,"r");
while( !feof( $pid ) )
{
echo fread($pid, 256);
flush();
ob_flush();
usleep(100000);
}
pclose($pid);
Python
import argon2, binascii
hash = argon2.hash_password_raw(
time_cost=16, memory_cost=2**15, parallelism=2, hash_len=32,
password=b'password', salt=b'some salt', type=argon2.low_level.Type.ID)
print("Argon2 raw hash:", binascii.hexlify(hash))
argon2Hasher = argon2.PasswordHasher(
time_cost=16, memory_cost=2**15, parallelism=2, hash_len=32, salt_len=16)
hash = argon2Hasher.hash("password")
references: https://framework.zend.com/blog/2017-08-17-php72-argon2-hash-password.html#:~:text=The%20hash%20size%20is%2032%20bytes.&text=The%20default%20parameters%20for%20the,2%20value%20for%20the%20memory_cost%20. https://symfony.com/blog/new-in-symfony-4-3-sodium-password-encoder