Documentation
¶
Overview ¶
Package marshaller serializes github.com/go-passwd/hasher hashers to and from a single string in a Django-style format.
The output adapts to whether the hasher carries iteration count and salt: the marshaller emits "code$iter$salt$digest" when both are set, and drops empty segments otherwise. Round-tripping a marshalled string through HexMarshaller.Unmarshal (or any other Marshaller implementation) reconstructs a hasher whose Check method recognises the original password.
DjangoMarshaller is the conventional default: hex digest, "$" separator. Base64Marshaller uses standard base64 instead, matching how Django stores PBKDF2 hashes.
Custom hashers participate by implementing Marshalable.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DjangoMarshaller = HexMarshaller{Separator: "$"}
DjangoMarshaller stores passwords in Django-like format using "$" as the separator and hex-encoded hashes.
Functions ¶
This section is empty.
Types ¶
type Base64Marshaller ¶
type Base64Marshaller struct {
Separator string
}
Base64Marshaller stores password using standard base64 encoding (with "=" padding). Matches Django's PBKDF2 hash storage format.
type HexMarshaller ¶
type HexMarshaller struct {
Separator string
}
HexMarshaller stores password in HEX
func (*HexMarshaller) Marshal ¶
func (m *HexMarshaller) Marshal(h hasher.Hasher) (string, error)
Marshal hasher.Hasher to string
Example ¶
package main
import (
"fmt"
"github.com/go-passwd/hasher"
"github.com/go-passwd/marshaller"
)
func main() {
// Use a fixed salt + iter so the output is deterministic for the
// example. SetPassword on a fresh hasher would otherwise generate
// a random salt.
iter := 10
salt := "salt"
h := hasher.MD5Hasher{Iter: &iter, Salt: &salt}
h.SetPassword("password")
s, err := marshaller.DjangoMarshaller.Marshal(&h)
if err != nil {
panic(err)
}
fmt.Println(s)
}
Output: md5$10$salt$1446572e0a1e0d275b6d1ec51d61d5b1
func (*HexMarshaller) Unmarshal ¶
func (m *HexMarshaller) Unmarshal(s string) (hasher.Hasher, error)
Unmarshal string to Hasher
Example ¶
package main
import (
"fmt"
"github.com/go-passwd/marshaller"
)
func main() {
encoded := "md5$10$salt$1446572e0a1e0d275b6d1ec51d61d5b1"
h, err := marshaller.DjangoMarshaller.Unmarshal(encoded)
if err != nil {
panic(err)
}
fmt.Println(h.Check("password"))
fmt.Println(h.Check("wrong"))
}
Output: true false
type Marshalable ¶
type Marshalable interface {
// Code returns the hasher's algorithm code (e.g. "sha256").
Code() string
// HashIter returns the iteration count used. 0 means "no
// iteration count was set".
HashIter() int
// HashSalt returns the salt used. "" means "no salt was set".
HashSalt() string
// HashedPassword returns the stored digest, or nil when unset.
HashedPassword() []byte
}
Marshalable defines interface for hasher who can be marshalable