#!/usr/local/vesta/php/bin/php
<?php
//# info: generate password  hash
//# options: HASH-METHOD SALT PASSWORD
//
//# The function generates password hash

// Checking arguments
if ((empty($argv[1])) || (empty($argv[2]))) {
    echo "Error: not enought arguments\n";
    echo "Usage: " . $argv[0] ." HASH-METHOD SALT PASSWORD\n";
    exit(1);
}

$crypt = $argv[1];
$salt = $argv[2];
if (empty($argv[3])) {
    $password = file_get_contents("php://stdin");
    $password = str_replace("\n",'',$password);
} else {
    $password = $argv[3];
}

// Generating MD5 hash
if ($crypt == 'md5' ) {
    $hash = crypt($password,  '$1$'.$salt.'$');
}

// Generating SHA-512 hash
if ($crypt == 'sha-512' ) {
    $hash = crypt($password,  '$6$rounds=5000$'.$salt.'$');
    $hash = str_replace('$rounds=5000','',$hash);
}

// Generating base64 hash
if ($crypt == 'htpasswd' ) {
    $hash = crypt($password, base64_encode($password));
}

// Generating DES hash
if ($crypt == 'des' ) {
    $hash = crypt($password, $salt);
}

// Printing result
echo $hash . "\n";
