digest.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package model
  2. import (
  3. "database/sql"
  4. "database/sql/driver"
  5. "errors"
  6. "fmt"
  7. "log/slog"
  8. "strings"
  9. "unicode"
  10. )
  11. // Digest represents a digest of a model Manifest. It is a comparable value
  12. // type and is immutable.
  13. //
  14. // The zero Digest is not a valid digest.
  15. type Digest struct {
  16. s string
  17. }
  18. // Type returns the digest type of the digest.
  19. //
  20. // Example:
  21. //
  22. // ParseDigest("sha256-1234").Type() // returns "sha256"
  23. func (d Digest) Type() string {
  24. typ, _, _ := strings.Cut(d.s, "-")
  25. return typ
  26. }
  27. // String returns the digest in the form of "<digest-type>-<digest>", or the
  28. // empty string if the digest is invalid.
  29. func (d Digest) String() string { return d.s }
  30. // IsValid returns true if the digest is valid (not zero).
  31. //
  32. // A valid digest may be created only by ParseDigest, or
  33. // ParseName(name).Digest().
  34. func (d Digest) IsValid() bool { return d.s != "" }
  35. // LogValue implements slog.Value.
  36. func (d Digest) LogValue() slog.Value {
  37. return slog.StringValue(d.String())
  38. }
  39. var (
  40. _ slog.LogValuer = Digest{}
  41. )
  42. // ParseDigest parses a string in the form of "<digest-type>-<digest>" into a
  43. // Digest.
  44. func ParseDigest(s string) Digest {
  45. typ, digest, ok := strings.Cut(s, "-")
  46. if ok && isValidDigestType(typ) && isValidHex(digest) {
  47. return Digest{s: s}
  48. }
  49. return Digest{}
  50. }
  51. func isValidDigestType(s string) bool {
  52. if len(s) == 0 {
  53. return false
  54. }
  55. for _, r := range s {
  56. if !unicode.IsLower(r) && !unicode.IsDigit(r) {
  57. return false
  58. }
  59. }
  60. return true
  61. }
  62. func isValidHex(s string) bool {
  63. if len(s) == 0 {
  64. return false
  65. }
  66. for i := range s {
  67. c := s[i]
  68. if c < '0' || c > '9' && c < 'a' || c > 'f' {
  69. return false
  70. }
  71. }
  72. return true
  73. }