caphash.go 682 B

1234567891011121314151617181920212223242526
  1. package caphash
  2. import (
  3. "crypto/sha256"
  4. "encoding/hex"
  5. "google.golang.org/protobuf/proto"
  6. hthv1 "hth/internal/gen/hth/api/v1"
  7. )
  8. // HashCanonicalCapabilities is the unique Capability Hash algorithm.
  9. // Callers: GetCapabilities, hthctl doctor, hth-capture, Replay, acceptance, manifests.
  10. // It is NOT profile.ProfileIdentityDigest (a profile-file digest).
  11. func HashCanonicalCapabilities(c *hthv1.Capabilities) string {
  12. if c == nil {
  13. return ""
  14. }
  15. cp := proto.Clone(c).(*hthv1.Capabilities)
  16. cp.CapabilityHash = ""
  17. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(cp)
  18. if err != nil {
  19. return ""
  20. }
  21. sum := sha256.Sum256(b)
  22. return hex.EncodeToString(sum[:])
  23. }