mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-01-06 05:01:10 -05:00
Bumps [github.com/open-policy-agent/opa](https://github.com/open-policy-agent/opa) from 1.8.0 to 1.9.0. - [Release notes](https://github.com/open-policy-agent/opa/releases) - [Changelog](https://github.com/open-policy-agent/opa/blob/main/CHANGELOG.md) - [Commits](https://github.com/open-policy-agent/opa/compare/v1.8.0...v1.9.0) --- updated-dependencies: - dependency-name: github.com/open-policy-agent/opa dependency-version: 1.9.0 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com>
46 lines
1.3 KiB
Go
46 lines
1.3 KiB
Go
package dsig
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"fmt"
|
|
"hash"
|
|
)
|
|
|
|
func toHMACKey(dst *[]byte, key any) error {
|
|
keyBytes, ok := key.([]byte)
|
|
if !ok {
|
|
return fmt.Errorf(`dsig.toHMACKey: invalid key type %T. []byte is required`, key)
|
|
}
|
|
|
|
if len(keyBytes) == 0 {
|
|
return fmt.Errorf(`dsig.toHMACKey: missing key while signing payload`)
|
|
}
|
|
|
|
*dst = keyBytes
|
|
return nil
|
|
}
|
|
|
|
// SignHMAC generates an HMAC signature for the given payload using the specified hash function and key.
|
|
// The raw parameter should be the pre-computed signing input (typically header.payload).
|
|
func SignHMAC(key, payload []byte, hfunc func() hash.Hash) ([]byte, error) {
|
|
h := hmac.New(hfunc, key)
|
|
if _, err := h.Write(payload); err != nil {
|
|
return nil, fmt.Errorf(`failed to write payload using hmac: %w`, err)
|
|
}
|
|
return h.Sum(nil), nil
|
|
}
|
|
|
|
// VerifyHMAC verifies an HMAC signature for the given payload.
|
|
// This function verifies the signature using the specified key and hash function.
|
|
// The payload parameter should be the pre-computed signing input (typically header.payload).
|
|
func VerifyHMAC(key, payload, signature []byte, hfunc func() hash.Hash) error {
|
|
expected, err := SignHMAC(key, payload, hfunc)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to sign payload for verification: %w", err)
|
|
}
|
|
if !hmac.Equal(signature, expected) {
|
|
return NewVerificationError("invalid HMAC signature")
|
|
}
|
|
return nil
|
|
}
|