1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36:
<?php
namespace Apptus\ESales\Connector;
/**
* A class that is used for signing messages.
*/
class MessageAuthentication {
private $privateKey;
/**
* Creates a new instance with a specific private key, that can then be used to sign messages.
* @param string $privateKey A private key that will be used as a secret in the signature. Must not be null.
* @throws \InvalidArgumentException If privateKey is null or empty.
*/
public function __construct($privateKey)
{
if (!isset($privateKey) || strlen($privateKey) == 0) {
throw new \InvalidArgumentException('Argument $privateKey must not be null or empty.');
}
$this->privateKey = $privateKey;
}
/**
* Signs a message by combining it with a private key, used for prevention of unauthorized changes of the message.
* @param string $message The message that should be signed. Must not be null or empty.
* @return string A string token in hex format.
* @throws \InvalidArgumentException If message is null or empty.
*/
public function sign($message) {
if (!isset($message) || strlen($message) == 0) {
throw new \InvalidArgumentException('Argument $message must not be null or empty.');
}
return hash_hmac('sha256', $message, $this->privateKey);
}
}