working commit

This commit is contained in:
2026-02-26 18:15:10 +02:00
commit 19b173357a
9 changed files with 328 additions and 0 deletions
+50
View File
@@ -0,0 +1,50 @@
package client
import (
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"strings"
)
func SHA256Digest(src []byte) string {
hasher := sha256.New()
hasher.Write(src)
sum := hasher.Sum(nil)
return "sha256:" + hex.EncodeToString(sum)
}
func SHA512Digest(src []byte) string {
hasher := sha512.New()
hasher.Write(src)
sum := hasher.Sum(nil)
return "sha512:" + hex.EncodeToString(sum)
}
const (
Undefined int = iota
SHA256
SHA512
)
func DigestType(digest string) int {
var err error
var typ int
digest = strings.ToLower(digest)
digest = strings.TrimPrefix(digest, "sha256:")
digest = strings.TrimPrefix(digest, "sha512:")
decoded, err := hex.DecodeString(digest)
if err != nil {
return Undefined
}
switch (len(decoded)) {
case 64:
typ = SHA256
case 128:
typ = SHA512
default:
typ = Undefined
}
return typ
}