add nonce to hash

Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
pull/304/head
Maxence Lange 2019-01-04 09:34:57 -01:00
rodzic dc3bbe9652
commit 721a060b48
1 zmienionych plików z 20 dodań i 5 usunięć

Wyświetl plik

@ -260,19 +260,23 @@ class LinkedDataSignature implements JsonSerializable {
$header = [
'@context' => 'https://w3id.org/identity/v1',
'nonce' => $this->getNonce(),
'creator' => $this->getCreator(),
'created' => $this->getCreated()
];
$hash = $this->hashedCanonicalize($header) . $this->hashedCanonicalize($this->getObject());
$signed = base64_decode($this->getSignatureValue());
$hashHeader = $this->hashedCanonicalize($header, true);
$hashObject = $this->hashedCanonicalize($this->getObject());
$algo = OPENSSL_ALGO_SHA256;
if ($this->getType() === 'RsaSignature2017') {
$algo = OPENSSL_ALGO_SHA256;
}
if (openssl_verify($hash, $signed, $this->getPublicKey(), $algo) === 1) {
if (openssl_verify(
$hashHeader . $hashObject, base64_decode($this->getSignatureValue()),
$this->getPublicKey(), $algo
) === 1) {
return true;
}
@ -282,10 +286,21 @@ class LinkedDataSignature implements JsonSerializable {
/**
* @param array $data
*
* @param bool $removeEmptyValue
*
* @return string
*/
private function hashedCanonicalize(array $data): string {
$object = json_decode(json_encode($data), false);
private function hashedCanonicalize(array $data, bool $removeEmptyValue = false): string {
if ($removeEmptyValue) {
$data = array_filter(
$data,
function($v) {
return ($v !== '');
}
);
}
$object = json_decode(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
$res = jsonld_normalize(
$object,
[