Sunday, March 19, 2023

golang: compute PBKDF2 of a password


 package main

import (
  "bytes"
  "crypto/rand"
  "crypto/sha256"
  "encoding/hex"
  "fmt"
  "golang.org/x/crypto/pbkdf2"
  "golang.org/x/term"
  "log"
  "strings"
  "syscall"
)

func main() {
  fmt.Print("Input password: ")

  password, err := term.ReadPassword(int(syscall.Stdin))

  if err != nil {
    log.Fatal(err)
  }

  fmt.Print("\nConfirm password: ")

  confirmPw, err := term.ReadPassword(int(syscall.Stdin))
 
  if err != nil {
    log.Fatal(err)
  }
 
  if !bytes.Equal(password, ConfirmPw) {
    log.Fatal("Error: inputs mismatch")
  }

  salt := make([]byte, 32)

  _, err = rand.Read(salt)

  if err != nil {
    log.Fatal(err)
  }

  pwPbkdf2 := pbkdf2.Key(password, salt, 10240, 32, sha256.New)

  fmt.Println("\nSalt: ", strings.ToUpper(hex.EncodeToString(salt))
  fmt.Println("PBKDF2: ", strings.ToUpper(hex.EncodeToString(pwPbkdf2))
}




No comments:

 
Get This <