Verified Commit aa950991 authored by Cyril Vazquez's avatar Cyril Vazquez
Browse files

Fix hash calculation when not using the configured algo

parent 394458e1
......@@ -97,11 +97,17 @@ class digitalSafe
} catch (\Exception $e) {
throw $this->getThrowable($e->getMessage(), 400, $replyMessage);
}
} elseif (!isset($resource->hash) && !isset($resource->hashAlgorithm)) {
$this->getHash($resource);
continue;
} else {
throw $this->getThrowable("Hash or hash algorithm missing", 401, $replyMessage);
}
if (!isset($resource->hash) && !isset($resource->hashAlgorithm)) {
$this->getHash($resource);
continue;
}
throw $this->getThrowable("Hash or hash algorithm missing", 401, $replyMessage);
}
try {
......@@ -673,6 +679,32 @@ class digitalSafe
}
}
/**
* Calculate hash if necessary
*
* @param object $resource
*/
protected function getHash($resource)
{
$hashAlgorithm = \laabs::configuration('recordsManagement')['hashAlgorithm'];
if ($resource->hashAlgorithm == $hashAlgorithm) {
return;
}
$resource->hashAlgorithm = $hashAlgorithm;
if (is_string($resource->handler)) {
$resource->hash = strtolower(hash($hashAlgorithm, base64_decode($resource->handler)));
} else {
$tmpfile = \laabs::getTmpDir().DIRECTORY_SEPARATOR.rand();
file_put_contents($tmpfile, base64_decode(stream_get_contents($resource->handler)));
rewind($resource->handler);
$resource->hash = strtolower(hash_file($hashAlgorithm, $tmpfile));
unlink($tmpfile);
}
}
/**
* log event in lifecycle journal
*
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment