53 lines
1.4 KiB
PHP
53 lines
1.4 KiB
PHP
<?php
|
|
class SignedCommunicationProvider
|
|
{
|
|
|
|
protected $GPG;
|
|
|
|
public function __construct($keyFile, $fingerprint = null, $passphrase = null, $tmpPath = null)
|
|
{
|
|
if(empty($tmpPath)) {
|
|
$tmpPath = "/tmp/tononixOS.gpg";
|
|
}
|
|
if (file_exists($tmpPath)) {
|
|
unlink($tmpPath);
|
|
}
|
|
$this->GPG = new GnuPG(['home-dir' => $tmpPath]);
|
|
$keyData = file_get_contents($keyFile);
|
|
$this->GPG->import($keyData);
|
|
if (!empty($fingerprint)) {
|
|
$result = $this->GPG->addsignkey($fingerprint, $passphrase);
|
|
if ($result == false) {
|
|
return false;
|
|
} else {
|
|
$this->GPG->setsignmode(GNUPG_SIG_MODE_NORMAL);
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function sign($data)
|
|
{
|
|
if ($this->GPG == null) {
|
|
return false;
|
|
}
|
|
$data = $this->GPG->sign($data);
|
|
return $data;
|
|
}
|
|
|
|
public function verify($data)
|
|
{
|
|
$verified = "";
|
|
$info = $this->GPG->verify($data, false, $verified);
|
|
$result = array("data" => $verified,
|
|
"message" => $info);
|
|
if ($info == false) {
|
|
$result['message'] = "Signature Verification Failed.";
|
|
$result['data'] = false;
|
|
return $result;
|
|
} else {
|
|
return $result;
|
|
}
|
|
}
|
|
}
|