summaryrefslogtreecommitdiffstats
path: root/inc/simplesamlphp/bin/pwgen.php
blob: 83b4dfeafea66023b251f82812ac3ff1a408237a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env php
<?php
/*
 * Interactive script to generate password hashes.
 *
 */


/* This is the base directory of the simpleSAMLphp installation. */
$baseDir = dirname(dirname(__FILE__));

/* Add library autoloader. */
require_once($baseDir . '/lib/_autoload.php');


echo "Enter password: ";
$password = trim(fgets(STDIN));

if(empty($password)) {
	echo "Need at least one character for a password\n";
	exit(1);
}

$table = '';
foreach (array_chunk(hash_algos(), 6) as $chunk) {
	foreach($chunk as $algo) {
		$table .= sprintf('%-13s', $algo);
	}
	$table .= "\n";
}

echo "The following hashing algorithms are available:\n" . $table . "\n";
echo "Which one do you want? [sha256] ";
$algo = trim(fgets(STDIN));
if(empty($algo)) {
	$algo = 'sha256';
}

if(!in_array(strtolower($algo), hash_algos())) {
	echo "Hashing algorithm '$algo' is not supported\n";
	exit(1);
}

echo "Do you want to use a salt? (yes/no) [yes] ";
$s = (trim(fgets(STDIN)) == 'no') ? '' : 'S';

echo "\n  " . SimpleSAML_Utils_Crypto::pwHash($password, strtoupper( $s . $algo ) ). "\n\n";