package carlosop import ( "crypto/sha256" "encoding/hex" "regexp" ) // EnvDigest is carlos/platform 522's `vd1:` digest (spec ยง4.3) of a plain // var holding value: SHA-256 over the four stored fields of a var, kind // "env", the value, and an empty envelope and sealed-to fingerprint, // NUL-separated under the scheme's domain string. // // It is applied only to the two agent keys' values, which the runner itself // sends. Never to a secret: a secret's digest is over its envelope, and the // runner never holds a secret's plaintext. Like the value, the result is not // to be printed or logged. func EnvDigest(value string) string { h := sha256.New() for i, f := range []string{"carlos.config.var.v1", "env", value, "", ""} { if i > 0 { h.Write([]byte{0}) } h.Write([]byte(f)) } return "vd1:" + hex.EncodeToString(h.Sum(nil)) } var ( versionShape = regexp.MustCompile(`^cv1:[0-9a-f]{64}$`) digestShape = regexp.MustCompile(`^vd1:[0-9a-f]{64}$`) ) // validVersion admits a 522 layer version: "none" for an absent layer, or // "cv1:" and 64 lowercase hex. func validVersion(v string) bool { return v == "none" || versionShape.MatchString(v) } // validDigest admits a 522 var digest: "vd1:" and 64 lowercase hex. func validDigest(d string) bool { return digestShape.MatchString(d) }