36 lines
835 B
Go
36 lines
835 B
Go
package auth
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
)
|
|
|
|
// Authenticator handles authentication
|
|
type Authenticator struct {
|
|
user string
|
|
password string
|
|
hash string
|
|
}
|
|
|
|
// New creates a new authenticator with the given credentials
|
|
func New(username, password string) *Authenticator {
|
|
authString := fmt.Sprintf("%s:%s", username, password)
|
|
hash := sha256.Sum256([]byte(authString))
|
|
hashStr := hex.EncodeToString(hash[:])
|
|
|
|
return &Authenticator{
|
|
user: username,
|
|
password: password,
|
|
hash: hashStr,
|
|
}
|
|
}
|
|
|
|
// Verify checks if the provided username and password are valid
|
|
func (a *Authenticator) Verify(username, password string) bool {
|
|
authString := fmt.Sprintf("%s:%s", username, password)
|
|
hash := sha256.Sum256([]byte(authString))
|
|
hashStr := hex.EncodeToString(hash[:])
|
|
return hashStr == a.hash
|
|
}
|