asciigoat's .htaccess and .htpasswd parser
https://asciigoat.org/httools
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.1 KiB
52 lines
1.1 KiB
package htpasswd |
|
|
|
import ( |
|
"bytes" |
|
"crypto/sha512" |
|
"encoding/base64" |
|
"fmt" |
|
) |
|
|
|
// Sha512 facilitates sha512 style hashing |
|
type Sha512 struct{} |
|
|
|
// Hash returns the hashed variant of the password or an error |
|
func (ss *Sha512) Hash(passwd string) (string, error) { |
|
s := sha512.New() |
|
_, err := s.Write([]byte(passwd)) |
|
if err != nil { |
|
return "", err |
|
} |
|
passwordSum := []byte(s.Sum(nil)) |
|
return ss.Prefix() + base64.StdEncoding.EncodeToString(passwordSum), nil |
|
} |
|
|
|
// Match verifier the hashed password using the original |
|
func (*Sha512) Match(password, hashedPassword string) error { |
|
eppS := hashedPassword[3:] |
|
hash, err := base64.StdEncoding.DecodeString(eppS) |
|
|
|
if err != nil { |
|
return fmt.Errorf("cannot base64 decode") |
|
} |
|
|
|
sha := sha512.New() |
|
_, err = sha.Write([]byte(password)) |
|
|
|
if err != nil { |
|
return err |
|
} |
|
|
|
sum := sha.Sum(nil) |
|
|
|
if !bytes.Equal(sum, hash) { |
|
return fmt.Errorf("wrong password") |
|
} |
|
return nil |
|
} |
|
|
|
// Name returns the name of the hasher |
|
func (*Sha512) Name() string { return "sha512" } |
|
|
|
// Prefix returns the hasher's prefix |
|
func (*Sha512) Prefix() string { return "$6$" }
|
|
|