Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateSHA256HMAC ¶
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.
Types ¶
type Generator ¶
type Generator interface {
// AddAuthHeaders adds authentication headers to the provided HTTP request and returns it.
AddAuthHeaders(r *http.Request) *http.Request
}
Generator defines an interface for adding authentication headers to HTTP requests. Implementations should add at least a signature, API key ID, and timestamp.
func NewGenerator ¶
NewGenerator creates a new Generator instance for signing HTTP requests.
Parameters:
- id: API key identifier
- secret: Secret key for HMAC signing
Returns:
- Generator: An instance that can add authentication headers to HTTP requests.
Example:
gen := hash.NewGenerator("api-key-id", "supersecret")
req, _ := http.NewRequest("GET", "https://api.example.com/resource", nil)
signedReq := gen.AddAuthHeaders(req)
type Validator ¶
type Validator interface {
// Validate checks the HMAC signature, timestamp, and expiration of the request.
// Returns true if valid, false and an error otherwise.
Validate(r *http.Request, secret string) (bool, error)
// Check if Api Key is in use
GetKeyId(r *http.Request) string
}
Validator defines an interface for validating authentication headers on HTTP requests.
func NewValidator ¶
NewValidator creates a new Validator instance for validating HTTP requests.
Parameters:
- validity: Allowed time window (in seconds) for the request to be valid.
Returns:
- Validator: An instance that can validate authentication headers on HTTP requests.
Example:
validator := hash.NewValidator(60) // 60 seconds validity ok, err := validator.Validate(req, "supersecret")