From 0aed2fb5d82beb22ba04bc590c6fcbbc8f9deb3e Mon Sep 17 00:00:00 2001 From: Aneurin Barker Snook Date: Fri, 21 Jun 2024 21:54:44 +0100 Subject: [PATCH] add readme for each package --- README.md | 34 ++++++++++++++++++++++++++++++++++ hash.go | 12 ++---------- 2 files changed, 36 insertions(+), 10 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..3193108 --- /dev/null +++ b/README.md @@ -0,0 +1,34 @@ +# Go Hash + +Simple wrapper functions which take arbitrary string input and return string hashes in hex encoding. + +The author is aware that this package is somewhat perfunctory, but likes not having to think about the `crypto` packages. + +## Example + +```go +package main + +import ( + "errors" + "fmt" + "os" + + "github.com/annybs/go/hash" +) + +func main() { + if len(os.Args) < 2 { + fmt.Println(errors.New("input required")) + os.Exit(1) + } + + fmt.Println(hash.SHA256(os.Args[1])) +} +``` + +Execute `go run main.go "any test string you like"` to create SHA256 output. + +## License + +See [LICENSE.md](../LICENSE.md) diff --git a/hash.go b/hash.go index c92f69b..1b938aa 100644 --- a/hash.go +++ b/hash.go @@ -9,19 +9,11 @@ import ( // SHA256 creates a hex-encoded SHA256 checksum for a string input. func SHA256(value string) string { sum := sha256.Sum256([]byte(value)) - hash := []byte{} - for _, b := range sum { - hash = append(hash, b) - } - return hex.EncodeToString(hash) + return hex.EncodeToString(sum[0:]) } // SHA512 creates a hex-encoded SHA512 checksum for a string input. func SHA512(value string) string { sum := sha512.Sum512([]byte(value)) - hash := []byte{} - for _, b := range sum { - hash = append(hash, b) - } - return hex.EncodeToString(hash) + return hex.EncodeToString(sum[0:]) }