♻️ Replace HashType with Hasher

Compatible with crypto.Hash
This commit is contained in:
Dan Jones 2025-03-15 20:09:38 -05:00
commit 2440f55563
5 changed files with 47 additions and 95 deletions

View file

@ -26,7 +26,6 @@ tasks:
desc: Vet go code desc: Vet go code
sources: sources:
- '**/*.go' - '**/*.go'
deps: [gen]
cmds: cmds:
- go vet ./... - go vet ./...
@ -82,7 +81,7 @@ tasks:
test: test:
desc: Run unit tests desc: Run unit tests
deps: [fmt, vet, gen] deps: [fmt, vet]
sources: sources:
- '**/*.go' - '**/*.go'
generates: generates:

View file

@ -1,9 +1,7 @@
package nomino package nomino
import ( import (
"crypto/md5" "crypto"
"crypto/sha1"
"crypto/sha256"
"errors" "errors"
"fmt" "fmt"
"hash" "hash"
@ -42,39 +40,35 @@ func Slug(lang ...string) Generator {
// HashingFunc is a function that generates a hash.Hash // HashingFunc is a function that generates a hash.Hash
type HashingFunc func() hash.Hash type HashingFunc func() hash.Hash
//go:generate stringer -type=HashType -trimprefix=Hash // New allows HashingFunc to be used as a Hasher
func (hf HashingFunc) New() hash.Hash {
// HashType represents a particular hashing algorithm return hf()
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,
} }
// 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. // Hash generates a name from a hash of the filename.
// When this is used, the original filename will be removed from the final filename. // When this is used, the original filename will be removed from the final filename.
func Hash(t HashType) Generator { func Hash(h Hasher) Generator {
f, ok := hashMap[t] if h == nil {
h = crypto.MD5
}
return func(c *Config) (string, error) { return func(c *Config) (string, error) {
if !ok { if h == crypto.MD5SHA1 {
return "", fmt.Errorf("%w: %s", ErrInvalidHashType, t) return "", ErrInvalidHash
} }
name, err := getOriginal(c) name, err := getOriginal(c)
if err != nil { if err != nil {
return "", err return "", err
} }
hs := f() hs := h.New()
hs.Write([]byte(name)) hs.Write([]byte(name))
return fmt.Sprintf("%x", hs.Sum(nil)), nil return fmt.Sprintf("%x", hs.Sum(nil)), nil
} }

View file

@ -1,66 +1,56 @@
package nomino_test package nomino_test
import ( import (
"crypto"
"crypto/hmac"
"fmt" "fmt"
"hash"
"codeberg.org/danjones000/nomino" "codeberg.org/danjones000/nomino"
) )
func ExampleSlug() { func ExampleSlug() {
conf := nomino.NewConfig( str, _ := nomino.Slug().Make(nomino.WithOriginal("My name is Jimmy"))
nomino.WithOriginal("My name is Jimmy"),
nomino.WithGenerator(nomino.Slug()),
)
str, _ := nomino.Make(conf)
fmt.Println(str) fmt.Println(str)
// Output: my-name-is-jimmy.txt // Output: my-name-is-jimmy.txt
} }
func ExampleSlug_withLang() { func ExampleSlug_withLang() {
conf := nomino.NewConfig( str, _ := nomino.Slug("de").
nomino.WithOriginal("Diese & Dass"), Make(nomino.WithOriginal("Diese & Dass"))
nomino.WithGenerator(nomino.Slug("de")),
)
str, _ := nomino.Make(conf)
fmt.Println(str) fmt.Println(str)
// Output: diese-und-dass.txt // Output: diese-und-dass.txt
} }
func ExampleHash_mD5() { func ExampleHash_mD5() {
conf := nomino.NewConfig( str, _ := nomino.Hash(crypto.MD5).
nomino.WithOriginal("foobar"), Make(nomino.WithOriginal("foobar"))
nomino.WithGenerator(
nomino.Hash(nomino.HashMD5),
),
)
str, _ := nomino.Make(conf)
fmt.Println(str) fmt.Println(str)
// Output: 3858f62230ac3c915f300c664312c63f.txt // Output: 3858f62230ac3c915f300c664312c63f.txt
} }
func ExampleHash_sHA1() { func ExampleHash_sHA1() {
conf := nomino.NewConfig( str, _ := nomino.Hash(crypto.SHA1).
nomino.WithOriginal("foobar"), Make(nomino.WithOriginal("foobar"))
nomino.WithGenerator(
nomino.Hash(nomino.HashSHA1),
),
)
str, _ := nomino.Make(conf)
fmt.Println(str) fmt.Println(str)
// Output: 8843d7f92416211de9ebb963ff4ce28125932878.txt // Output: 8843d7f92416211de9ebb963ff4ce28125932878.txt
} }
func ExampleHash_sHA256() { func ExampleHash_sHA256() {
conf := nomino.NewConfig( str, _ := nomino.Hash(crypto.SHA256).
nomino.WithOriginal("foobar"), Make(nomino.WithOriginal("foobar"))
nomino.WithGenerator(
nomino.Hash(nomino.HashSHA256),
),
)
str, _ := nomino.Make(conf)
fmt.Println(str) fmt.Println(str)
// Output: c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2.txt // 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
}

View file

@ -1,6 +1,7 @@
package nomino package nomino
import ( import (
"crypto"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -22,21 +23,15 @@ func TestSlugRemovesOriginal(t *testing.T) {
} }
func TestHashBadHash(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) st, err := conf.generator(&conf)
assert.Equal(t, "", st) assert.Equal(t, "", st)
assert.ErrorIs(t, err, ErrInvalidHashType) assert.ErrorIs(t, err, ErrInvalidHash)
assert.ErrorContains(t, err, "invalid hash type: HashType(0)")
} }
func TestHashMissingOriginal(t *testing.T) { func TestHashMissingOriginal(t *testing.T) {
conf := NewConfig(WithGenerator(Hash(HashMD5))) conf := NewConfig(WithGenerator(Hash(nil)))
st, err := conf.generator(&conf) st, err := conf.generator(&conf)
assert.Equal(t, "", st) assert.Equal(t, "", st)
assert.ErrorIs(t, err, ErrMissingOriginal) assert.ErrorIs(t, err, ErrMissingOriginal)
} }
func TestHashTypeStringer(t *testing.T) {
s := HashMD5.String()
assert.Equal(t, "MD5", s)
}

View file

@ -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]]
}