Unsafe password storage or use of fast hashes

Unsafe password storage or use of fast hashes

Description

Plaintext passwords are immediately readable in a database or log leak. Fast general-purpose hashes such as MD5 or SHA let someone quickly check many guesses against leaked hashes.

Potential impact

A recovered password can give someone else access to the user’s account.

Remediation

Choose an adjustable-cost password hash such as bcrypt, scrypt, Argon2id or PBKDF2. After a successful login against an old fast hash, replace the old hash with a hash of the entered password using the new algorithm.

Examples

Benchmark Go’s bcrypt cost in your deployment. Handle inputs longer than 72 bytes as errors.

Before

go
user.SetPasswordHash(sha256.Sum256([]byte(password)))

After

go
package example

import "golang.org/x/crypto/bcrypt"

type passwordHashStore interface {
    SetPasswordHash([]byte)
}

func storePassword(user passwordHashStore, password string) error {
    hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
    if err != nil {
        return err
    }
    user.SetPasswordHash(hash)
    return nil
}

storePassword returns immediately if GenerateFromPassword fails. It writes the hash only after a successful call.

References