GenerateNewPubPrivKeys.php
1.35 KB
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
48
49
50
51
52
53
54
55
56
57
58
<?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);
?>