Skip to content

Argon2id Go package to facilitate the use of "crypto/argon2" package.

License

Notifications You must be signed in to change notification settings

KEINOS/go-argonize

Repository files navigation

go-argonize

GitHub go.mod Go versionGo Reference

Go package to facilitate the use of the Argon2id password hashing algorithm from the "crypto/argon2" package.

go get "github.com/KEINOS/go-argonize"
func Example_basic() {
    // Your strong and unpredictable password
    password := []byte("my password")

    // Password hash your password
    hashedObj, err := argonize.Hash(password)
    if err != nil {
        log.Fatal(err)
    }

    // View the hashed password
    fmt.Println("Passwd to save:", hashedObj.String())

    // Verify password (golden case)
    if hashedObj.IsValidPassword([]byte("my password")) {
        fmt.Println("the password is valid")
    } else {
        fmt.Println("the password is invalid")
    }

    // Verify password (wrong case)
    if hashedObj.IsValidPassword([]byte("wrong password")) {
        fmt.Println("the password is valid")
    } else {
        fmt.Println("the password is invalid")
    }

    // Output:
    // Passwd to save: $argon2id$v=19$m=65536,t=1,p=2$ek6ZYdlRm2D5AsGV98TWKA$QAIDZEdIgwohrNX678mHc448LOmD7jGR4BGw/9YMMVU
    // the password is valid
    // the password is invalid
}
func Example_from_saved_password() {
  // Load the hashed password from a file, DB or etc.
  savedPasswd := "$argon2id$v=19$m=65536,t=1,p=2$iuIIXq4foOhcGUH1BjE08w$kA+XOAMls8hzWg3J1sYxkeuK/lkU4HDRBf0zchdyllY"

  // Decode the saved password to a Hashed object.
  // Note that once hashed, passwords cannot be recovered and can only be
  // verified.
  hashObj, err := argonize.DecodeHashStr(savedPasswd)
  if err != nil {
    log.Fatal(err)
  }

  // Validate the password against the hashed password.
  if hashObj.IsValidPassword([]byte("my password")) {
    fmt.Println("the password is valid")
  } else {
    fmt.Println("the password is invalid")
  }

  if hashObj.IsValidPassword([]byte("wrong password")) {
    fmt.Println("the password is valid")
  } else {
    fmt.Println("the password is invalid")
  }
  // Output:
  // the password is valid
  // the password is invalid
}

Contributing

GitHub go.mod Go version Go Reference Opened Issues PR

Any Pull-Request for improvement is welcome!

Statuses

UnitTests golangci-lint CodeQL-Analysis PlatformTests

codecov Go Report Card

License, copyright and credits