Skip to content

Instantly share code, notes, and snippets.

@andyg2
Created April 11, 2026 14:42
Show Gist options
  • Select an option

  • Save andyg2/0d426cd98d888e250c0affabbee0eac5 to your computer and use it in GitHub Desktop.

Select an option

Save andyg2/0d426cd98d888e250c0affabbee0eac5 to your computer and use it in GitHub Desktop.
Generate a mosquitto passwd file with iter=101, 12-byte salt (matching mosquitto_passwd 2.0.x)
<?php
// Generate a passwd file with iter=101, 12-byte salt (matching mosquitto_passwd 2.0.x)
$user = getenv('MQTT_USER');
$pass = getenv('MQTT_PASSWORD');
$iter = 101;
$salt = random_bytes(12);
$hash = hash_pbkdf2('sha512', $pass, $salt, $iter, 64, true);
$line = $user . ':$7$' . $iter . '$' . base64_encode($salt) . '$' . base64_encode($hash);
echo $line . PHP_EOL;
file_put_contents('/mosquitto-data/passwd', $line . PHP_EOL);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment