-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.PHONY: all | ||
|
||
fmt: | ||
go fmt ./... | ||
|
||
vet: | ||
go vet ./... | ||
|
||
build: fmt vet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |