♻️ Replace HashType with Hasher
Compatible with crypto.Hash
This commit is contained in:
parent
1008a064d0
commit
2440f55563
5 changed files with 47 additions and 95 deletions
|
|
@ -26,7 +26,6 @@ tasks:
|
|||
desc: Vet go code
|
||||
sources:
|
||||
- '**/*.go'
|
||||
deps: [gen]
|
||||
cmds:
|
||||
- go vet ./...
|
||||
|
||||
|
|
@ -82,7 +81,7 @@ tasks:
|
|||
|
||||
test:
|
||||
desc: Run unit tests
|
||||
deps: [fmt, vet, gen]
|
||||
deps: [fmt, vet]
|
||||
sources:
|
||||
- '**/*.go'
|
||||
generates:
|
||||
|
|
|
|||
46
gen_file.go
46
gen_file.go
|
|
@ -1,9 +1,7 @@
|
|||
package nomino
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"crypto/sha1"
|
||||
"crypto/sha256"
|
||||
"crypto"
|
||||
"errors"
|
||||
"fmt"
|
||||
"hash"
|
||||
|
|
@ -42,39 +40,35 @@ func Slug(lang ...string) Generator {
|
|||
// HashingFunc is a function that generates a hash.Hash
|
||||
type HashingFunc func() hash.Hash
|
||||
|
||||
//go:generate stringer -type=HashType -trimprefix=Hash
|
||||
|
||||
// HashType represents a particular hashing algorithm
|
||||
type HashType uint8
|
||||
|
||||
const (
|
||||
HashMD5 HashType = iota + 1
|
||||
HashSHA1
|
||||
HashSHA256
|
||||
)
|
||||
|
||||
// ErrInvalidHashType is returned by the Hash generator when an invalid HashType is passed
|
||||
var ErrInvalidHashType = errors.New("invalid hash type")
|
||||
|
||||
var hashMap = map[HashType]HashingFunc{
|
||||
HashMD5: md5.New,
|
||||
HashSHA1: sha1.New,
|
||||
HashSHA256: sha256.New,
|
||||
// New allows HashingFunc to be used as a Hasher
|
||||
func (hf HashingFunc) New() hash.Hash {
|
||||
return hf()
|
||||
}
|
||||
|
||||
// Hasher is a type returns a hash.Hash.
|
||||
// All crypto.Hash may be used.
|
||||
type Hasher interface {
|
||||
New() hash.Hash
|
||||
}
|
||||
|
||||
// ErrInvalidHash is returned by the Hash generator when an invalid HashType is passed
|
||||
var ErrInvalidHash = errors.New("invalid hash type")
|
||||
|
||||
// Hash generates a name from a hash of the filename.
|
||||
// When this is used, the original filename will be removed from the final filename.
|
||||
func Hash(t HashType) Generator {
|
||||
f, ok := hashMap[t]
|
||||
func Hash(h Hasher) Generator {
|
||||
if h == nil {
|
||||
h = crypto.MD5
|
||||
}
|
||||
return func(c *Config) (string, error) {
|
||||
if !ok {
|
||||
return "", fmt.Errorf("%w: %s", ErrInvalidHashType, t)
|
||||
if h == crypto.MD5SHA1 {
|
||||
return "", ErrInvalidHash
|
||||
}
|
||||
name, err := getOriginal(c)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
hs := f()
|
||||
hs := h.New()
|
||||
hs.Write([]byte(name))
|
||||
return fmt.Sprintf("%x", hs.Sum(nil)), nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,66 +1,56 @@
|
|||
package nomino_test
|
||||
|
||||
import (
|
||||
"crypto"
|
||||
"crypto/hmac"
|
||||
"fmt"
|
||||
"hash"
|
||||
|
||||
"codeberg.org/danjones000/nomino"
|
||||
)
|
||||
|
||||
func ExampleSlug() {
|
||||
conf := nomino.NewConfig(
|
||||
nomino.WithOriginal("My name is Jimmy"),
|
||||
nomino.WithGenerator(nomino.Slug()),
|
||||
)
|
||||
str, _ := nomino.Make(conf)
|
||||
str, _ := nomino.Slug().Make(nomino.WithOriginal("My name is Jimmy"))
|
||||
fmt.Println(str)
|
||||
|
||||
// Output: my-name-is-jimmy.txt
|
||||
}
|
||||
|
||||
func ExampleSlug_withLang() {
|
||||
conf := nomino.NewConfig(
|
||||
nomino.WithOriginal("Diese & Dass"),
|
||||
nomino.WithGenerator(nomino.Slug("de")),
|
||||
)
|
||||
|
||||
str, _ := nomino.Make(conf)
|
||||
str, _ := nomino.Slug("de").
|
||||
Make(nomino.WithOriginal("Diese & Dass"))
|
||||
fmt.Println(str)
|
||||
|
||||
// Output: diese-und-dass.txt
|
||||
}
|
||||
|
||||
func ExampleHash_mD5() {
|
||||
conf := nomino.NewConfig(
|
||||
nomino.WithOriginal("foobar"),
|
||||
nomino.WithGenerator(
|
||||
nomino.Hash(nomino.HashMD5),
|
||||
),
|
||||
)
|
||||
str, _ := nomino.Make(conf)
|
||||
str, _ := nomino.Hash(crypto.MD5).
|
||||
Make(nomino.WithOriginal("foobar"))
|
||||
fmt.Println(str)
|
||||
// Output: 3858f62230ac3c915f300c664312c63f.txt
|
||||
}
|
||||
|
||||
func ExampleHash_sHA1() {
|
||||
conf := nomino.NewConfig(
|
||||
nomino.WithOriginal("foobar"),
|
||||
nomino.WithGenerator(
|
||||
nomino.Hash(nomino.HashSHA1),
|
||||
),
|
||||
)
|
||||
str, _ := nomino.Make(conf)
|
||||
str, _ := nomino.Hash(crypto.SHA1).
|
||||
Make(nomino.WithOriginal("foobar"))
|
||||
fmt.Println(str)
|
||||
// Output: 8843d7f92416211de9ebb963ff4ce28125932878.txt
|
||||
}
|
||||
|
||||
func ExampleHash_sHA256() {
|
||||
conf := nomino.NewConfig(
|
||||
nomino.WithOriginal("foobar"),
|
||||
nomino.WithGenerator(
|
||||
nomino.Hash(nomino.HashSHA256),
|
||||
),
|
||||
)
|
||||
str, _ := nomino.Make(conf)
|
||||
str, _ := nomino.Hash(crypto.SHA256).
|
||||
Make(nomino.WithOriginal("foobar"))
|
||||
fmt.Println(str)
|
||||
// Output: c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2.txt
|
||||
}
|
||||
|
||||
func ExampleHashingFunc_hMAC() {
|
||||
var hasher nomino.HashingFunc = func() hash.Hash {
|
||||
return hmac.New(crypto.SHA1.New, []byte("hello"))
|
||||
}
|
||||
g := nomino.Hash(hasher)
|
||||
str, _ := g.Make(nomino.WithOriginal("foobar"))
|
||||
fmt.Println(str)
|
||||
// Output: 85f767c284c80a3a59a9635194321d20dd90f31b.txt
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package nomino
|
||||
|
||||
import (
|
||||
"crypto"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
|
@ -22,21 +23,15 @@ func TestSlugRemovesOriginal(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestHashBadHash(t *testing.T) {
|
||||
conf := NewConfig(WithOriginal("foobar"), WithGenerator(Hash(0)))
|
||||
conf := NewConfig(WithOriginal("foobar"), WithGenerator(Hash(crypto.MD5SHA1)))
|
||||
st, err := conf.generator(&conf)
|
||||
assert.Equal(t, "", st)
|
||||
assert.ErrorIs(t, err, ErrInvalidHashType)
|
||||
assert.ErrorContains(t, err, "invalid hash type: HashType(0)")
|
||||
assert.ErrorIs(t, err, ErrInvalidHash)
|
||||
}
|
||||
|
||||
func TestHashMissingOriginal(t *testing.T) {
|
||||
conf := NewConfig(WithGenerator(Hash(HashMD5)))
|
||||
conf := NewConfig(WithGenerator(Hash(nil)))
|
||||
st, err := conf.generator(&conf)
|
||||
assert.Equal(t, "", st)
|
||||
assert.ErrorIs(t, err, ErrMissingOriginal)
|
||||
}
|
||||
|
||||
func TestHashTypeStringer(t *testing.T) {
|
||||
s := HashMD5.String()
|
||||
assert.Equal(t, "MD5", s)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,26 +0,0 @@
|
|||
// Code generated by "stringer -type=HashType -trimprefix=Hash"; DO NOT EDIT.
|
||||
|
||||
package nomino
|
||||
|
||||
import "strconv"
|
||||
|
||||
func _() {
|
||||
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||
// Re-run the stringer command to generate them again.
|
||||
var x [1]struct{}
|
||||
_ = x[HashMD5-1]
|
||||
_ = x[HashSHA1-2]
|
||||
_ = x[HashSHA256-3]
|
||||
}
|
||||
|
||||
const _HashType_name = "MD5SHA1SHA256"
|
||||
|
||||
var _HashType_index = [...]uint8{0, 3, 7, 13}
|
||||
|
||||
func (i HashType) String() string {
|
||||
i -= 1
|
||||
if i >= HashType(len(_HashType_index)-1) {
|
||||
return "HashType(" + strconv.FormatInt(int64(i+1), 10) + ")"
|
||||
}
|
||||
return _HashType_name[_HashType_index[i]:_HashType_index[i+1]]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue