Hasher
Hash passwords for storage, verify submitted passwords during sign-in, and generate UUID v4 identifiers or cryptographically random verification tokens.
Hash a password before storing it
Resolve Hasher from the container and call hash() with the
user's password. The utility uses PHP's bcrypt password hashing with a cost of
10. Store the resulting hash in your user record, never the original
password.
use Zap\Core\Utils\Hasher;
$hasher = app(Hasher::class);
$passwordHash = $hasher->hash($request->input('password'));
// Persist $passwordHash on the user record, not the submitted password.
Verify a submitted password
Load the saved hash for the account and pass it as the first argument to
verify(hash, password). The method uses PHP's
password_verify() and returns a boolean. Re-hash only after checking a
valid password if your application later changes its hashing policy.
$hasher = app(Hasher::class);
$user = $this->findUserByEmail($request->input('email'));
if ($user === null || !$hasher->verify($user['password_hash'], $request->input('password'))) {
return $this->text('Invalid email or password.', 401);
}
// Continue with the application's authenticated session workflow.
Do not compare a submitted password directly with a stored hash. Password hashes
include salts and should be checked with verify().
Generate a UUID v4
Use generateUUIDv4() for a random UUID identifier. UUIDs are useful
for record identifiers; they are not a substitute for authorization checks.
$id = app(Hasher::class)->generateUUIDv4();
// Example shape: 7a74f5d2-8e44-4c33-9ef7-0c8b5281b879
Generate a verification token
generateVerificationToken() returns random bytes encoded as hexadecimal.
Its default input length is 32 bytes, producing a 64-character string. The optional
length is in bytes, so the returned string contains twice as many hexadecimal
characters.
$token = app(Hasher::class)->generateVerificationToken();
// Send $token to the intended user through a verified channel.
For password-reset or email-verification workflows, store a hash of the token and compare it safely when the user returns it. Also associate an expiration time and invalidate the token after successful use.
Use hashes and tokens safely
- Never log or persist plaintext passwords.
- Use
verify()for password checks and return generic sign-in errors. - Protect reset and verification tokens as secrets; do not put them in logs.
- Expire one-time tokens and invalidate them after use.
- Use session and authorization controls separately; a UUID is not proof of permission.
Continue with the Logger utility guide.