Skip to content

Hashing

Security

Hashing

Introduction

The hash package hashes your users' passwords with bcrypt. Bcrypt is a great choice for passwords because you can tune how long a hash takes, and raise it as hardware gets faster. Slow is good when hashing passwords: the longer each guess takes, the longer an attacker needs to crack a stolen hash.

In most applications, you won't need to call the hash package often. Authentication checks password hashes when users log in, upgrades them automatically, and hashes new passwords when users change them. Call hash.Make yourself when you create a user, such as in a sign-up handler or a seeder.

Configuration

By default, Laravel hashes passwords with 12 rounds of bcrypt, which is fine for most applications. You may change the work factor with BCRYPT_ROUNDS. Each extra round doubles the time a hash takes:

1BCRYPT_ROUNDS=12
1BCRYPT_ROUNDS=12

Tests that use laraveltest.New, as the tests in new applications do, automatically hash with 4 rounds to stay fast.

Basic usage

Hashing passwords

You may hash a password by calling hash.Make with the context and the password. It hashes with the rounds that BCRYPT_ROUNDS sets, and returns the hash to store in your users table. This sign-up handler hashes the password from its form and creates the user:

1package registrations
2 
3import (
4 "go.laravel.com/hash"
5 "go.laravel.com/model"
6 "go.laravel.com/validation"
7 "go.laravel.com/validation/rule"
8 "go.laravel.com/web"
9 
10 "app/models"
11)
12 
13type SignUp struct {
14 Name string `json:"name"`
15 Email string `json:"email"`
16 Password string `json:"password"`
17}
18 
19func (f *SignUp) Rules() validation.Rules {
20 return validation.For(f,
21 rule.Field(&f.Name, rule.Required, rule.Max(255)),
22 rule.Field(&f.Email, rule.Required, rule.Email),
23 rule.Field(&f.Password, rule.Required, rule.Min(8), hash.Password, rule.Confirmed),
24 )
25}
26 
27func Store(r *web.Request) *web.Response {
28 form, err := r.Validate[SignUp]()
29 if err != nil {
30 return web.Fail(err)
31 }
32 
33 hashed, err := hash.Make(r, form.Password)
34 if err != nil {
35 return web.Fail(err)
36 }
37 
38 user := models.User{Name: form.Name, Email: form.Email, Password: hashed}
39 if err := model.Create(r, &user); err != nil {
40 return web.Fail(err)
41 }
42 
43 return web.Redirect("/login")
44}
1package registrations
2 
3import (
4 "go.laravel.com/hash"
5 "go.laravel.com/model"
6 "go.laravel.com/validation"
7 "go.laravel.com/validation/rule"
8 "go.laravel.com/web"
9 
10 "app/models"
11)
12 
13type SignUp struct {
14 Name string `json:"name"`
15 Email string `json:"email"`
16 Password string `json:"password"`
17}
18 
19func (f *SignUp) Rules() validation.Rules {
20 return validation.For(f,
21 rule.Field(&f.Name, rule.Required, rule.Max(255)),
22 rule.Field(&f.Email, rule.Required, rule.Email),
23 rule.Field(&f.Password, rule.Required, rule.Min(8), hash.Password, rule.Confirmed),
24 )
25}
26 
27func Store(r *web.Request) *web.Response {
28 form, err := r.Validate[SignUp]()
29 if err != nil {
30 return web.Fail(err)
31 }
32 
33 hashed, err := hash.Make(r, form.Password)
34 if err != nil {
35 return web.Fail(err)
36 }
37 
38 user := models.User{Name: form.Name, Email: form.Email, Password: hashed}
39 if err := model.Create(r, &user); err != nil {
40 return web.Fail(err)
41 }
42 
43 return web.Redirect("/login")
44}

A handler passes r. A seeder, a command, or a queued job passes the ctx it received: hash.Make(ctx, "password").

Password length

Bcrypt uses only the first 72 bytes of a password, so Make refuses a longer one with hash.ErrTooLong, which responds with a 422 error page rather than sending the user back to the form. Give your password fields the hash.Password rule, as the sign-up form above does. A long password then fails validation instead, and the user sees "The password field must not be greater than 72 bytes." on the password field.

rule.Max(72) isn't enough, because it counts characters, and a character outside ASCII takes more than one byte. For example, a passphrase of 25 Japanese characters passes rule.Max(72), but it takes 75 bytes.

Storing hashes

Store a hash in a string field of your user model, and tag the field json:"-". The User model in new applications, in models/user.go, already does. Here it is with its other fields left out:

1package models
2 
3type User struct {
4 ID int64 `json:"id"`
5 Name string `json:"name"`
6 Email string `json:"email"`
7 Password string `json:"-"`
8}
1package models
2 
3type User struct {
4 ID int64 `json:"id"`
5 Name string `json:"name"`
6 Email string `json:"email"`
7 Password string `json:"-"`
8}

The json:"-" tag keeps the hash out of every Inertia page and JSON response that includes the user. Without it, those responses would send the user's password hash to the browser, so keep the tag on any field that holds a hash.

Verifying that a password matches a hash

hash.Check verifies that a plain-text password, such as the one from your form, matches the user's stored hash. It returns false for a wrong password, and for a hash it can't read:

1import "go.laravel.com/hash"
2 
3if hash.Check(form.Password, user.Password) {
4 // The passwords match...
5}
1import "go.laravel.com/hash"
2 
3if hash.Check(form.Password, user.Password) {
4 // The passwords match...
5}

To log a user in, use auth.Attempt instead. It checks the password for you, and takes as long to fail whether or not the account exists.

Determining if a password needs to be rehashed

When you raise BCRYPT_ROUNDS, the hashes already in your database keep their old rounds. hash.NeedsRehash returns true for a hash made with rounds other than your current setting. Hash the password again right after Check accepts it, while you still have the plain text:

1import (
2 "go.laravel.com/db"
3 "go.laravel.com/hash"
4 "go.laravel.com/model"
5 "go.laravel.com/web"
6)
7 
8if hash.Check(form.Password, user.Password) {
9 if hash.NeedsRehash(r, user.Password) {
10 hashed, err := hash.Make(r, form.Password)
11 if err != nil {
12 return web.Fail(err)
13 }
14 
15 if err := model.Update(r, &user, db.Values{"password": hashed}); err != nil {
16 return web.Fail(err)
17 }
18 }
19 
20 // Log the user in...
21}
1import (
2 "go.laravel.com/db"
3 "go.laravel.com/hash"
4 "go.laravel.com/model"
5 "go.laravel.com/web"
6)
7 
8if hash.Check(form.Password, user.Password) {
9 if hash.NeedsRehash(r, user.Password) {
10 hashed, err := hash.Make(r, form.Password)
11 if err != nil {
12 return web.Fail(err)
13 }
14 
15 if err := model.Update(r, &user, db.Values{"password": hashed}); err != nil {
16 return web.Fail(err)
17 }
18 }
19 
20 // Log the user in...
21}

auth.Attempt does this for you, so after you raise BCRYPT_ROUNDS, each user's hash upgrades the next time they log in. A new hash also logs the user out of their other devices, so raising the rounds logs each user out of their other devices once.