9951 explained code solutions for 126 technologies


golangHow to validate hashed password


Use bcrypt package (to install run go get golang.org/x/crypto/bcrypt) for hashin passwords:

package main

import (
  "fmt"
  "golang.org/x/crypto/bcrypt"
)

func main() {
  pwd := "qwerty123"
  hash, _ := bcrypt.GenerateFromPassword([]byte(pwd), 10)
  
  if ( bcrypt.CompareHashAndPassword(hash, []byte("qwerty123")) == nil ) {
    fmt.Println("password is correct")
  }
}ctrl + c
package main

default package declaration

qwerty123

sample password string to hash

bcrypt.GenerateFromPassword

returns hash from given password

CompareHashAndPassword

compares given hash (generated early) and password value (e.g., posted from client), returns error if they don't match


Usage example

package main

import (
  "fmt"
  "golang.org/x/crypto/bcrypt"
)

func main() {
  pwd := "qwerty123"
  hash, _ := bcrypt.GenerateFromPassword([]byte(pwd), 10)

  if ( bcrypt.CompareHashAndPassword(hash, []byte("qwerty123")) == nil ) {
    fmt.Println("password is correct")
  }
}
output
password is correct