GenerateNewPubPrivKeys.php 1.35 KB
<?php

$shortopts  = "d:";
$longopts  = array("description:");
$options = getopt($shortopts, $longopts);
$client_description = "";
if (array_key_exists("description",$options)) {
	$client_description = $options["description"];
}
else if (array_key_exists("d",$options)) {
	$client_description = $options["d"];
}

if (empty($client_description)) {
	echo "[ERROR] Missing client description".PHP_EOL;
	exit(1);
}

$keys_file = getenv("DDBASEROOT")."/ddservice_clients_keys.json";

$existing_keys = array();
if (file_exists($keys_file)) {
	$keys_content = file_get_contents($keys_file);
	if (empty($keys_content)) {
		echo "[ERROR] Cannot load DDService clients keys file: $keys_file".PHP_EOL;
		exit(1);
	}
	$existing_keys = json_decode($keys_content, TRUE);
	if ($existing_keys == NULL) {
		echo "[ERROR] DDService clients keys file seems to be corrupted: $keys_file".PHP_EOL;
		exit(1);
	}
}

$public_key=md5(microtime());

sleep(1);

$private_key=md5(microtime());

$existing_keys[] = array(
	"public"  => $public_key,
	"private" => $private_key,
	"description" => $client_description,
);

if (!file_put_contents($keys_file, json_encode($existing_keys))) {
	echo "[ERROR] Cannot write clients keys file: $keys_file".PHP_EOL;
	exit(1);
}

echo "DESCRIPTION: $client_description".PHP_EOL;
echo "PUBLIC_KEY: $public_key".PHP_EOL;
echo "PRIVATE_KEY: $private_key".PHP_EOL;

exit(0);

?>