2015-01-06 00:04:30 +00:00
|
|
|
package digest
|
2014-11-13 00:39:35 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"regexp"
|
|
|
|
)
|
|
|
|
|
2015-10-19 23:42:12 +00:00
|
|
|
// TarsumRegexp defines a regular expression to match tarsum identifiers.
|
2014-11-13 00:39:35 +00:00
|
|
|
var TarsumRegexp = regexp.MustCompile("tarsum(?:.[a-z0-9]+)?\\+[a-zA-Z0-9]+:[A-Fa-f0-9]+")
|
|
|
|
|
2015-04-17 12:39:52 +00:00
|
|
|
// TarsumRegexpCapturing defines a regular expression to match tarsum identifiers with
|
2014-11-13 00:39:35 +00:00
|
|
|
// capture groups corresponding to each component.
|
|
|
|
var TarsumRegexpCapturing = regexp.MustCompile("(tarsum)(.([a-z0-9]+))?\\+([a-zA-Z0-9]+):([A-Fa-f0-9]+)")
|
|
|
|
|
|
|
|
// TarSumInfo contains information about a parsed tarsum.
|
|
|
|
type TarSumInfo struct {
|
|
|
|
// Version contains the version of the tarsum.
|
|
|
|
Version string
|
|
|
|
|
|
|
|
// Algorithm contains the algorithm for the final digest
|
|
|
|
Algorithm string
|
|
|
|
|
|
|
|
// Digest contains the hex-encoded digest.
|
|
|
|
Digest string
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// InvalidTarSumError provides informations about a TarSum that cannot be parsed
|
|
|
|
// by ParseTarSum.
|
2015-03-10 21:40:58 +00:00
|
|
|
type InvalidTarSumError string
|
2014-11-13 00:39:35 +00:00
|
|
|
|
|
|
|
func (e InvalidTarSumError) Error() string {
|
2015-03-10 21:40:58 +00:00
|
|
|
return fmt.Sprintf("invalid tarsum: %q", string(e))
|
2014-11-13 00:39:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseTarSum parses a tarsum string into its components of interest. For
|
|
|
|
// example, this method may receive the tarsum in the following format:
|
|
|
|
//
|
|
|
|
// tarsum.v1+sha256:220a60ecd4a3c32c282622a625a54db9ba0ff55b5ba9c29c7064a2bc358b6a3e
|
|
|
|
//
|
|
|
|
// The function will return the following:
|
|
|
|
//
|
|
|
|
// TarSumInfo{
|
|
|
|
// Version: "v1",
|
|
|
|
// Algorithm: "sha256",
|
|
|
|
// Digest: "220a60ecd4a3c32c282622a625a54db9ba0ff55b5ba9c29c7064a2bc358b6a3e",
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
func ParseTarSum(tarSum string) (tsi TarSumInfo, err error) {
|
|
|
|
components := TarsumRegexpCapturing.FindStringSubmatch(tarSum)
|
|
|
|
|
|
|
|
if len(components) != 1+TarsumRegexpCapturing.NumSubexp() {
|
2015-03-10 21:40:58 +00:00
|
|
|
return TarSumInfo{}, InvalidTarSumError(tarSum)
|
2014-11-13 00:39:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TarSumInfo{
|
|
|
|
Version: components[3],
|
|
|
|
Algorithm: components[4],
|
|
|
|
Digest: components[5],
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the valid, string representation of the tarsum info.
|
|
|
|
func (tsi TarSumInfo) String() string {
|
|
|
|
if tsi.Version == "" {
|
|
|
|
return fmt.Sprintf("tarsum+%s:%s", tsi.Algorithm, tsi.Digest)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("tarsum.%s+%s:%s", tsi.Version, tsi.Algorithm, tsi.Digest)
|
|
|
|
}
|