Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/go-core-stack/auth

go 1.24
67 changes: 67 additions & 0 deletions hash/generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright © 2025 Prabhjot Singh Sethi, All Rights reserved
// Author: Prabhjot Singh Sethi <prabhjot.sethi@gmail.com>

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))
}
21 changes: 21 additions & 0 deletions hash/generator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright © 2025 Prabhjot Singh Sethi, All Rights reserved
// Author: Prabhjot Singh Sethi <prabhjot.sethi@gmail.com>

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")
}
}