zeg1jeux/lib/Keygen.class.php

102 lines
2.2 KiB
PHP
Raw Normal View History

<?php
class Keygen {
2022-11-15 20:49:57 +01:00
private $keygenPath = __DIR__ . '/../vendors/keygen/keygen';
2022-11-17 16:53:45 +01:00
// private $keygenPath = 'keygen'; // if you use Docker
2022-11-15 20:49:57 +01:00
private $pubsecDir = __DIR__ .'/../cache/pubsec/';
private $userPubsecPath;
2022-10-28 13:50:49 +02:00
public function __construct () {
2022-11-15 20:49:57 +01:00
}
public function getG1Pub ($salt, $pepper) {
$salt = str_replace('"', '\"', $salt);
$pepper = str_replace('"', '\"', $pepper);
$cmd = $this->keygenPath;
$cmd .= ' -f pubsec';
$cmd .= ' -t duniter';
$cmd .= ' "'. $salt .'"';
2022-11-17 16:53:45 +01:00
$cmd .= ' -p "'. $pepper .'"';
2022-11-15 20:49:57 +01:00
$output=null;
$result_code=null;
exec($cmd, $output, $result_code);
// die($cmd . '<br />'. print_r($output, true) . '<br />'. print_r($result_code, true));
if (empty($output) or empty($output[0])) {
throw new Exception('Keygen me calcule pas (la G1 pub)');
}
return $output[0];
}
2022-11-17 16:53:45 +01:00
public function getIPNSPub ($salt, $pepper) {
$salt = str_replace('"', '\"', $salt);
$pepper = str_replace('"', '\"', $pepper);
$cmd = $this->keygenPath;
$cmd .= ' -t b36mf';
$cmd .= ' "'. $salt .'"';
$cmd .= ' -p "'. $pepper .'"';
$output=null;
$result_code=null;
exec($cmd, $output, $result_code);
// die($cmd . '<br />'. print_r($output, true) . '<br />'. print_r($result_code, true));
if (empty($output) or empty($output[0])) {
throw new Exception('Keygen me calcule pas (la pub IPNS) :<br />' . $cmd . '<br />');
}
return $output[0];
}
2022-11-15 20:49:57 +01:00
public function generatePubsec ($salt, $pepper) {
$salt = str_replace('"', '\"', $salt);
$pepper = str_replace('"', '\"', $pepper);
$userPubkey = $this->getG1Pub($salt, $pepper);
2022-11-15 21:00:53 +01:00
if (!file_exists($this->pubsecDir)) {
mkdir($this->pubsecDir, 0777, true);
}
2022-11-15 20:49:57 +01:00
2022-11-17 16:53:45 +01:00
if (!file_exists($this->pubsecDir . $userPubkey . '.dunikey')) {
2022-11-15 20:49:57 +01:00
2022-11-17 16:53:45 +01:00
$cmd = $this->keygenPath;
$cmd .= ' -f pubsec';
$cmd .= ' -t duniter';
$cmd .= ' "'. $salt .'"';
$cmd .= ' -p "'. $pepper .'"';
$cmd .= ' -o '. $this->pubsecDir . $userPubkey . '.dunikey';
$output=null;
$result_code=null;
exec($cmd, $output, $result_code);
2022-11-15 20:49:57 +01:00
2022-11-17 16:53:45 +01:00
// die($cmd . '<br />'. print_r($result_code, true));
2022-11-15 20:49:57 +01:00
2022-11-17 16:53:45 +01:00
if ($result_code != 0) {
2022-11-17 16:53:45 +01:00
throw new Exception('Keygen me calcule pas (la dunikey)');
}
2022-11-15 20:49:57 +01:00
}
}
}