From be83492de15dc92a6fc4b0c0b7d9babfc57a0d5b Mon Sep 17 00:00:00 2001 From: Amal Francis Date: Sun, 12 Mar 2017 02:07:28 +0530 Subject: [PATCH] hello world --- Makefile | 9 +++++++++ etag.go | 25 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 Makefile create mode 100644 etag.go diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2e16225 --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +.PHONY: all + +fmt: + go fmt ./... + +vet: + go vet ./... + +build: fmt vet \ No newline at end of file diff --git a/etag.go b/etag.go new file mode 100644 index 0000000..8036294 --- /dev/null +++ b/etag.go @@ -0,0 +1,25 @@ +package etag + +import ( + "crypto/sha1" + "encoding/hex" + "fmt" +) + +func getHash(str string) string { + h := sha1.New() + h.Write([]byte(str)) + + return hex.EncodeToString(h.Sum(nil)) +} + +// Generate an Etag for given sring. Allows specifying whether to generate weak +// Etag or not as second parameter +func Generate(str string, weak bool) string { + tag := fmt.Sprintf("%d-%s", len(str), getHash(str)) + if weak { + tag = "W/" + tag + } + + return tag +}