v-generate-password-hash 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/usr/local/vesta/php/bin/php
  2. <?php
  3. //# info: generate password hash
  4. //# options: HASH-METHOD SALT PASSWORD
  5. //
  6. //# The function generates password hash
  7. // Checking arguments
  8. if ((empty($argv[1])) || (empty($argv[2]))) {
  9. echo "Error: not enought arguments\n";
  10. echo "Usage: " . $argv[0] ." HASH-METHOD SALT PASSWORD\n";
  11. exit(1);
  12. }
  13. $crypt = $argv[1];
  14. $salt = $argv[2];
  15. if (empty($argv[3])) {
  16. $password = file_get_contents("php://stdin");
  17. $password = str_replace("\n",'',$password);
  18. } else {
  19. $password = $argv[3];
  20. }
  21. // Generating MD5 hash
  22. if ($crypt == 'md5' ) {
  23. $hash = crypt($password, '$1$'.$salt.'$');
  24. }
  25. // Generating SHA-512 hash
  26. if ($crypt == 'sha-512' ) {
  27. $hash = crypt($password, '$6$rounds=5000$'.$salt.'$');
  28. $hash = str_replace('$rounds=5000','',$hash);
  29. }
  30. // Generating base64 hash
  31. if ($crypt == 'htpasswd' ) {
  32. $hash = crypt($password, base64_encode($password));
  33. }
  34. // Generating DES hash
  35. if ($crypt == 'des' ) {
  36. $hash = crypt($password, $salt);
  37. }
  38. // Printing result
  39. echo $hash . "\n";