Safely store passwords with BCrypt


To save passwords, BCrypt is better than SHA hash

Pros

  • Protect against rainbow table attacks (Generate different hashes with the same password)
  • Resistant to brute-force attacks

Setup

Updating your dependencies

// build.gradle
dependencies {
  compile "org.springframework.security:spring-security-core"
}

Usage

...
user.passwordHash = BCryptPasswordEncoder().encode(password)
user.save()
fun login(userId:String, password: String): Boolean {
  ...
  if(!BCryptPasswordEncoder().matches(password, user.passwordHash)){
    return false
  }
  return true
}

See also