Description
MD5 is fast enough to test many guesses against leaked password hashes. Without a salt, the same calculations also work across accounts. An MD5 collision alone does not reveal a particular password. Guessing it depends on its strength and the resources available.
Potential impact
An exposed password can give someone access to personal data, payments or administrative functions, depending on the account. Reused passwords can expose other accounts. Weak storage may also violate applicable privacy or security requirements.
Remediation
- Use a trusted bcrypt, scrypt or Argon2id implementation. With Go’s
golang.org/x/crypto/bcrypt, benchmark a cost of 10 or higher in your deployment and tune it for the service. - Use the storage format for your chosen algorithm.
bcrypt.GenerateFromPasswordincludes a new random salt in the result. Withargon2.IDKey, generate and supply the salt yourself. - After a successful login against an existing MD5 hash, hash the password the user entered with the new algorithm and replace the stored hash.
Examples
Before
go
package main
import (
"crypto/md5"
"encoding/hex"
)
type User struct {
PasswordHash string
}
// Before: hash passwords with MD5
func (u *User) SetPassword(plain string) {
h := md5.New()
h.Write([]byte(plain))
sum := h.Sum(nil)
// Storing passwords as MD5 hashes is insecure
u.PasswordHash = hex.EncodeToString(sum)
}
func (u *User) CheckPassword(plain string) bool {
h := md5.New()
h.Write([]byte(plain))
sum := h.Sum(nil)
return u.PasswordHash == hex.EncodeToString(sum)
}
After
go
package main
import (
"fmt"
"golang.org/x/crypto/bcrypt"
)
type User struct {
PasswordHash string
}
// After: hash passwords with bcrypt
func (u *User) SetPassword(plain string) error {
// Set cost to at least 10 and tune it with performance testing
hash, err := bcrypt.GenerateFromPassword([]byte(plain), bcrypt.DefaultCost)
if err != nil {
return err
}
u.PasswordHash = string(hash)
return nil
}
func (u *User) CheckPassword(plain string) bool {
err := bcrypt.CompareHashAndPassword([]byte(u.PasswordHash), []byte(plain))
return err == nil
}
func main() {
u := &User{}
if err := u.SetPassword("MySecurePassword!123"); err != nil {
panic(err)
}
fmt.Println("login ok?", u.CheckPassword("MySecurePassword!123")) // true
fmt.Println("login ok?", u.CheckPassword("wrong")) // false
}
SetPassword stores the bcrypt result only after generation succeeds. CheckPassword checks the supplied password with bcrypt.CompareHashAndPassword.