Created
August 3, 2016 13:51
-
-
Save vaibhavpandeyvpz/741bfd343ff98c68dedeed00df56a42e to your computer and use it in GitHub Desktop.
Function in PHP for hashing passwords to be compatible with Mosquitto MQTT broker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @param string $plain | |
* @param string $algo | |
* @param int $iterations | |
* @param int $saltlen | |
* @param int $keylen | |
* @return string | |
*/ | |
function mosquitto_pw($plain, $algo = 'sha256', $iterations = 901, $saltlen = 12, $keylen = 24) | |
{ | |
$salt = base64_encode(openssl_random_pseudo_bytes($saltlen)); | |
$hash = hash_pbkdf2($algo, $plain, $salt, $iterations, $keylen, true); | |
return sprintf('PBKDF2$%s$%d$%s$%s', $algo, $iterations, $salt, base64_encode($hash)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment