Signer
Class Signer
Tags
Table of Contents
Methods
- __construct() : mixed
- Creates new Signer object. If you want to use your own signing algorithm - you can do this.
- getSeparator() : string
- Returns separator, used for packing/unpacking.
- getSignature() : string
- Returns message signature.
- pack() : string
- Packs array values to single string: pack(['test', 'all', 'values']) -> 'test.all.values'
- setKey() : $this
- Sets key for signing.
- setSeparator() : $this
- Sets separator, used for packing/unpacking.
- sign() : string
- Signs message, returns string in format "{message}{separator}{signature}".
- unpack() : array<string|int, mixed>
- Unpacks values from string (something like rsplit).
- unsign() : string
- Checks message signature and return original message.
- validate() : bool
- Simply validation of message signature.
Methods
__construct()
Creates new Signer object. If you want to use your own signing algorithm - you can do this.
public
__construct([SigningAlgorithm|null $algorithm = null ]) : mixed
Parameters
- $algorithm : SigningAlgorithm|null = null
-
Custom signing algorithm.
getSeparator()
Returns separator, used for packing/unpacking.
public
getSeparator() : string
Return values
stringgetSignature()
Returns message signature.
public
getSignature(string $value[, string|null $salt = null ]) : string
Parameters
- $value : string
-
Message.
- $salt : string|null = null
-
Salt.
Tags
Return values
stringpack()
Packs 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()
Sets key for signing.
public
setKey(string $value) : $this
Parameters
- $value : string
-
Key.
Tags
Return values
$thissetSeparator()
Sets separator, used for packing/unpacking.
public
setSeparator(string $value) : $this
Parameters
- $value : string
-
Separator.
Tags
Return values
$thissign()
Signs message, returns string in format "{message}{separator}{signature}".
public
sign(string $value[, string|null $salt = null ]) : string
Simple example:
// If salt needed
$foo = (new Signer)->sign('test', 'my_salt');
// Otherwise $bar = (new Signer)->sign('test');
Parameters
- $value : string
-
Message for signing.
- $salt : string|null = null
-
Salt, if needed.
Tags
Return values
stringunpack()
Unpacks 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()
Checks message signature and return original message.
public
unsign(string $signedValue[, string|null $salt = null ]) : string
Simple example:
$signer = new Signer;
// Sign message $signedValue = $signer->sign('test');
// 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 //throw BadSignatureException with message 'Signature does not match' echo $signer->unsign($signedValue, 'invalid_salt');
Parameters
- $signedValue : string
-
Signed value, must be in format "{message}{separator}{signature}".
- $salt : string|null = null
-
Salt, if used while signing.
Tags
Return values
stringvalidate()
Simply validation of message signature.
public
validate(string $value, string $signature[, string|null $salt = null ]) : bool
Parameters
- $value : string
-
Message.
- $signature : string
-
Signature.
- $salt : string|null = null
-
Salt, if used while signing.
Return values
bool —True if OK, otherwise - false.