TimeSigner
Class TimeSigner
Tags
Table of Contents
Methods
- __construct() : mixed
- Creates new TimeSigner object. If you want use your own signing algorithm - you can this
- getSeparator() : string
- Return separator, used for packing/unpacking
- getSignature() : string
- Return message signature
- pack() : string
- Pack array values to single string: pack(['test', 'all', 'values']) -> 'test.all.values'
- setKey() : $this
- Set key for signing
- setSeparator() : $this
- Set separator, used for packing/unpacking
- sign() : string
- Sign message with expired time, return string in format: {message}{separator}{expired timestamp}{separator}{signature}
- unpack() : array<string|int, mixed>
- Unpack values from string (something like rsplit).
- unsign() : string
- Check message signature and it lifetime. If everything is OK - return original message.
- validate() : bool
- Simply validation of message signature
Methods
__construct()
Creates new TimeSigner object. If you want use your own signing algorithm - you can this
public
__construct([SigningAlgorithm $algorithm = null ]) : mixed
Parameters
- $algorithm : SigningAlgorithm = null
-
Custom signing algorithm.
getSeparator()
Return separator, used for packing/unpacking
public
getSeparator() : string
Return values
stringgetSignature()
Return message signature
public
getSignature(string $value, int $timestamp[, null $salt = null ]) : string
Parameters
- $value : string
-
Message.
- $timestamp : int
-
Expire timestamp.
- $salt : null = null
-
Salt (if needed).
Tags
Return values
stringpack()
Pack array values to single string: pack(['test', 'all', 'values']) -> 'test.all.values'
public
pack(array<string|int, mixed> $values) : string
Parameters
- $values : array<string|int, mixed>
-
Values for packing.
Return values
stringsetKey()
Set key for signing
public
setKey(string $value) : $this
Parameters
- $value : string
-
Key.
Tags
Return values
$thissetSeparator()
Set separator, used for packing/unpacking
public
setSeparator(string $value) : $this
Parameters
- $value : string
-
Separator.
Tags
Return values
$thissign()
Sign message with expired time, return string in format: {message}{separator}{expired timestamp}{separator}{signature}
public
sign(string $value, string $time[, string|null $salt = null ]) : string
Simple example:
// If salt needed
$foo = (new TimeSigner)->sign('test', '+1 hour', 'my_salt');
// Otherwise $bar = (new TimeSigner)->sign('test', '+1 day');
Parameters
- $value : string
-
Message for signing.
- $time : string
-
Timestamp or datetime description (presented in format accepted by strtotime).
- $salt : string|null = null
-
Salt, if needed.
Return values
stringunpack()
Unpack values from string (something like rsplit).
public
unpack(string $value[, int $limit = 2 ]) : array<string|int, mixed>
Simple example for separator ".":
// Unpack all values:
unpack('test.all.values', 0) -> ['test', 'all', 'values']
// Unpack 2 values (by default). First element containing the rest of string. unpack('test.all.values') -> ['test.all', 'values']
// Exception if separator is missing unpack('test.all values', 3) -> throws BadSignatureException
Parameters
- $value : string
-
String for unpacking.
- $limit : int = 2
-
If $limit === 0 - unpack all values, default - 2.
Tags
Return values
array<string|int, mixed>unsign()
Check message signature and it lifetime. If everything is OK - return original message.
public
unsign(string $signedValue[, string|null $salt = null ]) : string
Simple example:
$signer = new TimeSigner;
// Sing message for 1 second $signedValue = $signer->sign('test', '+1 second');
// Or sign with expiring on some magic timestamp (e.g. 01.01.2030) $signedValue = $signer->sign('test', 1893445200);
// Get original message with checking echo $signer->unsign($signedValue); // Output: 'test'
// Try to unsigning not signed value echo $signer->unsign('test'); //throw BadSignatureException with message 'Separator not found in value'
// Or with invalid sign echo $signer->unsign('test.invalid_sign');
// Or invalid salt echo $signer->unsign($signedValue, 'invalid_salt'); //throw BadSignatureException with message 'Signature does not match'
// Or expired lifetime echo $signer->unsign($signedValue); //throw BadSignatureException with message 'Signature timestamp expired (1403039921 < 1403040024)'
Parameters
- $signedValue : string
-
Signed value, must be in format: {message}{separator}{expired timestamp}{separator}{signature}.
- $salt : string|null = null
-
Salt, if used while signing.
Tags
Return values
stringvalidate()
Simply validation of message signature
public
validate(string $value, int $timestamp, string $signature[, string|null $salt = null ]) : bool
Parameters
- $value : string
-
Message.
- $timestamp : int
-
Expire timestamp.
- $signature : string
-
Signature.
- $salt : string|null = null
-
Salt, if used while signing.
Return values
bool —True if OK, otherwise - false.