48 lines
1.3 KiB
PHP
48 lines
1.3 KiB
PHP
<?php
|
|
/*
|
|
Author: Riccardo Di Dato
|
|
Creation Date: 25/giu/2015
|
|
*/
|
|
|
|
class RAPI_CryptUtils{
|
|
|
|
/**
|
|
* Genera un digest
|
|
* @param string $user Identificativo dell'utente
|
|
* @param int $timestamp Timestamp in microsecondi
|
|
* @param string $details Corpo della richiesta in JSON
|
|
* @param string $passphrase Password per la cifratura
|
|
* @return string
|
|
*/
|
|
private static function getDigest($user, $timestamp, $details, $passphrase) {
|
|
return hash_hmac("sha256",$user.$timestamp.substr($details,0,1000),$passphrase);
|
|
}
|
|
|
|
/**
|
|
* Genera un digest a partire da un messaggio
|
|
* @param string $user Identificativo dell'utente
|
|
* @param int $timestamp Timestamp in microsecondi
|
|
* @param string $details Corpo della richiesta in JSON
|
|
* @param string $passphrase Password per la cifratura
|
|
* @return string
|
|
*/
|
|
public static function getDigestForMessage(RAPI_Message $msg, $passphrase){
|
|
$user = null;
|
|
if ($msg instanceof RAPI_Request){
|
|
$user = $msg->getUser();
|
|
}
|
|
else if ($msg instanceof RAPI_Response){
|
|
$user = $msg->getRelatedRequest()->getUser();
|
|
}
|
|
else {
|
|
throw new RAPI_Exception("Invalid usage of CryptUtils::getDigestForMessage(); Unimplemented message type ('".get_class($msg)."')");
|
|
}
|
|
$timestamp = $msg->getTime();
|
|
$details = $msg->getDetails(true);
|
|
|
|
return self::getDigest($user, $timestamp, $details, $passphrase);
|
|
}
|
|
|
|
}
|
|
|
|
?>
|