oid.go 702 B

123456789101112131415161718192021222324252627282930
  1. // Copyright 2020 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package lfsutil
  5. import (
  6. "strings"
  7. )
  8. // OID is an LFS object ID.
  9. type OID string
  10. // ValidOID returns true if given oid is valid according to spec:
  11. // https://github.com/git-lfs/git-lfs/blob/master/docs/spec.md
  12. func ValidOID(oid OID) bool {
  13. fields := strings.SplitN(string(oid), ":", 2)
  14. if len(fields) != 2 {
  15. return false
  16. }
  17. method := fields[0]
  18. hash := fields[1]
  19. switch method {
  20. case "sha256":
  21. // SHA256 produces 64-char lower case hexadecimal hash
  22. return len(hash) == 64 && strings.ToLower(hash) == hash
  23. }
  24. return false
  25. }