diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..662ae1b --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/go-core-stack/auth + +go 1.24 diff --git a/hash/generator.go b/hash/generator.go new file mode 100644 index 0000000..96246ce --- /dev/null +++ b/hash/generator.go @@ -0,0 +1,67 @@ +// Copyright © 2025 Prabhjot Singh Sethi, All Rights reserved +// Author: Prabhjot Singh Sethi + +package hash + +import ( + "crypto/hmac" + "crypto/sha256" + "encoding/hex" + "strings" +) + +/* +Package hash provides utilities for generating cryptographic hashes. + +This file contains a function to generate a SHA-256 HMAC (Hash-based Message Authentication Code) +from a secret and one or more input strings. + +# Usage + + import ( + "fmt" + "yourmodule/hash" + ) + + func main() { + secret := "mysecretkey" + message := "data to protect" + signature := hash.GenerateSHA256HMAC(secret, message) + fmt.Println("HMAC:", signature) + } + +# Function Details + +- GenerateSHA256HMAC(secret string, v ...string) string + + - secret: The secret key used for HMAC generation. + - v: Variadic string arguments to be concatenated and signed. + + Returns a hex-encoded string representing the HMAC-SHA256 signature. +*/ + +// GenerateSHA256HMAC generates a SHA-256 HMAC signature for the given input strings using the provided secret key. +// The input strings are concatenated in the order provided, and the resulting string is signed. +// Returns the signature as a hex-encoded string. +// +// Parameters: +// - secret: The secret key used for HMAC generation. +// - v: Variadic string arguments to be concatenated and signed. +// +// Example: +// +// sig := GenerateSHA256HMAC("mysecret", "foo", "bar") +// // sig now contains the hex-encoded HMAC of "foobar" using "mysecret" as the key. +func GenerateSHA256HMAC(secret string, v ...string) string { + // Concatenate all input strings into a single string + raw := strings.Join(v, "") + + // Create a new HMAC hasher using SHA-256 and the provided secret key + h := hmac.New(sha256.New, []byte(secret)) + + // Write the concatenated string to the hasher + h.Write([]byte(raw)) + + // Compute the HMAC and return it as a hex-encoded string + return hex.EncodeToString(h.Sum(nil)) +} diff --git a/hash/generator_test.go b/hash/generator_test.go new file mode 100644 index 0000000..faeaf85 --- /dev/null +++ b/hash/generator_test.go @@ -0,0 +1,21 @@ +// Copyright © 2025 Prabhjot Singh Sethi, All Rights reserved +// Author: Prabhjot Singh Sethi + +package hash + +import ( + "testing" +) + +func Test_HashGeneration(t *testing.T) { + secret := "mysupersecretcode" + method := "POST" + path := "/api/service1/v1/scope/abc/test/test1" + timestamp := "1748410688" + + sig := GenerateSHA256HMAC(secret, method, path, timestamp) + + if sig != "2c269d572fbd6b324b5f6eb1cf06bed60811b43a82642d5f7f438b65160caa08" { + t.Errorf("generated HMAC signature doesn't match as expected") + } +}