depot/go/nix/nixhash/nixhash.go

33 lines
784 B
Go
Raw Normal View History

2023-08-23 23:00:44 +00:00
package nixhash
import (
"crypto/sha256"
"encoding/hex"
"sort"
"strings"
"github.com/numtide/go-nix/nixbase32"
)
func StorePathForText(name, s, storePath string, references []string) string {
strHash := sha256.Sum256([]byte(s))
sort.Strings(references)
var typeStrBuilder strings.Builder
typeStrBuilder.WriteString("text")
for _, ref := range references {
typeStrBuilder.WriteString(":")
typeStrBuilder.WriteString(ref)
}
typeStr := typeStrBuilder.String()
sStr := typeStr + ":sha256:" + hex.EncodeToString(strHash[:]) + ":" + storePath + ":" + name
sHash := sha256.Sum256([]byte(sStr))
sCompressedHash := make([]byte, 20)
for i := range sHash {
sCompressedHash[i%len(sCompressedHash)] ^= sHash[i]
}
return nixbase32.EncodeToString(sCompressedHash[:])
}