github.com/MetalBlockchain/metalgo@v1.11.9/utils/password/hash.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package password
     5  
     6  import (
     7  	"bytes"
     8  	"crypto/rand"
     9  
    10  	"golang.org/x/crypto/argon2"
    11  )
    12  
    13  // Hash of a password
    14  type Hash struct {
    15  	Password [32]byte `serialize:"true"` // The salted, hashed password
    16  	Salt     [16]byte `serialize:"true"` // The salt
    17  }
    18  
    19  // Set updates the password hash to be of the provided password
    20  func (h *Hash) Set(password string) error {
    21  	if _, err := rand.Read(h.Salt[:]); err != nil {
    22  		return err
    23  	}
    24  	// pw is the salted, hashed password
    25  	pw := argon2.IDKey([]byte(password), h.Salt[:], 1, 64*1024, 4, 32)
    26  	copy(h.Password[:], pw[:32])
    27  	return nil
    28  }
    29  
    30  // Check returns true iff the provided password was the same as the last
    31  // password set.
    32  func (h *Hash) Check(password string) bool {
    33  	pw := argon2.IDKey([]byte(password), h.Salt[:], 1, 64*1024, 4, 32)
    34  	return bytes.Equal(pw, h.Password[:])
    35  }