RecoveryCodeService
in package
Generates and verifies one-time recovery codes.
Tags
Table of Contents
Constants
- ENTROPY_BYTES = 10
- Random bytes of entropy per code (10 bytes = 20 hex chars = 80 bits).
- GROUP_SIZE = 5
- Characters per dash-separated group in the displayed code.
Properties
Methods
- __construct() : mixed
- generate() : array{code: string, hash: string}
- Generate a new recovery code.
- verify() : bool
- Verify a user-entered code against a stored hash.
- canonicalize() : string
- Reduce user input to the canonical form the hash is computed over.
- format() : string
- Group a raw code into dash-separated chunks for display.
Constants
ENTROPY_BYTES
Random bytes of entropy per code (10 bytes = 20 hex chars = 80 bits).
private
mixed
ENTROPY_BYTES
= 10
GROUP_SIZE
Characters per dash-separated group in the displayed code.
private
mixed
GROUP_SIZE
= 5
Properties
$tokenHasher
private
TokenHasher
$tokenHasher
Methods
__construct()
public
__construct([TokenHasher|null $tokenHasher = null ]) : mixed
Parameters
- $tokenHasher : TokenHasher|null = null
generate()
Generate a new recovery code.
public
generate() : array{code: string, hash: string}
Return values
array{code: string, hash: string} —The human-readable code to show the user once, and the hash to store.
verify()
Verify a user-entered code against a stored hash.
public
verify(string $input, string $hash) : bool
Parameters
- $input : string
-
The code as typed by the user (any case/spacing).
- $hash : string
-
The stored hash to check against.
Return values
boolcanonicalize()
Reduce user input to the canonical form the hash is computed over.
private
canonicalize(string $input) : string
Parameters
- $input : string
Return values
stringformat()
Group a raw code into dash-separated chunks for display.
private
format(string $raw) : string
Parameters
- $raw : string