mirror of
https://github.com/packwiz/packwiz.git
synced 2025-11-19 01:24:32 +01:00
Implement Modrinth pack exporting (fixes #34)
This commit is contained in:
37
core/hash.go
37
core/hash.go
@@ -5,23 +5,44 @@ import (
|
||||
"crypto/sha1"
|
||||
"crypto/sha256"
|
||||
"crypto/sha512"
|
||||
"errors"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"github.com/packwiz/packwiz/curseforge/murmur2"
|
||||
"hash"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// GetHashImpl gets an implementation of hash.Hash for the given hash type string
|
||||
func GetHashImpl(hashType string) (hash.Hash, error) {
|
||||
func GetHashImpl(hashType string) (hash.Hash, HashStringer, error) {
|
||||
switch strings.ToLower(hashType) {
|
||||
case "sha1":
|
||||
return sha1.New(), nil
|
||||
return sha1.New(), hexStringer{}, nil
|
||||
case "sha256":
|
||||
return sha256.New(), nil
|
||||
return sha256.New(), hexStringer{}, nil
|
||||
case "sha512":
|
||||
return sha512.New(), nil
|
||||
return sha512.New(), hexStringer{}, nil
|
||||
case "md5":
|
||||
return md5.New(), nil
|
||||
return md5.New(), hexStringer{}, nil
|
||||
case "murmur2":
|
||||
return murmur2.New(), numberStringer{}, nil
|
||||
}
|
||||
// TODO: implement murmur2
|
||||
return nil, errors.New("hash implementation not found")
|
||||
return nil, nil, fmt.Errorf("hash implementation %s not found", hashType)
|
||||
}
|
||||
|
||||
type HashStringer interface {
|
||||
HashToString([]byte) string
|
||||
}
|
||||
|
||||
type hexStringer struct{}
|
||||
|
||||
func (hexStringer) HashToString(data []byte) string {
|
||||
return hex.EncodeToString(data)
|
||||
}
|
||||
|
||||
type numberStringer struct{}
|
||||
|
||||
func (numberStringer) HashToString(data []byte) string {
|
||||
return strconv.FormatUint(uint64(binary.BigEndian.Uint32(data)), 10)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user