github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/packages/container/oci/digest.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package oci
     7  
     8  import (
     9  	"regexp"
    10  	"strings"
    11  )
    12  
    13  var digestPattern = regexp.MustCompile(`\Asha256:[a-f0-9]{64}\z`)
    14  
    15  type Digest string
    16  
    17  // Validate checks if the digest has a valid SHA256 signature
    18  func (d Digest) Validate() bool {
    19  	return digestPattern.MatchString(string(d))
    20  }
    21  
    22  func (d Digest) Hash() string {
    23  	p := strings.SplitN(string(d), ":", 2)
    24  	if len(p) != 2 {
    25  		return ""
    26  	}
    27  	return p[1]
    28  }