Skip to content

Commit a50a5a1

Browse files
committedJan 16, 2025··
Expose known algorithms
This commit exposes the known algorithm list as its own type to let programs using the library to use the standarized algorithm names. Signed-off-by: Adolfo García Veytia (Puerco) <adolfo.garcia@uservers.net>
1 parent d0a03f1 commit a50a5a1

File tree

1 file changed

+72
-5
lines changed

1 file changed

+72
-5
lines changed
 

‎go/v1/resource_descriptor.go

+72-5
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,83 @@ var (
1616
ErrRDRequiredField = errors.New("at least one of name, URI, or digest are required")
1717
)
1818

19+
type HashAlgorithm string
20+
21+
const (
22+
AlgorithmMD5 HashAlgorithm = "md5"
23+
AlgorithmSHA1 HashAlgorithm = "sha1"
24+
AlgorithmSHA224 HashAlgorithm = "sha224"
25+
AlgorithmSHA512_224 HashAlgorithm = "sha512_224"
26+
AlgorithmSHA256 HashAlgorithm = "sha256"
27+
AlgorithmSHA512_256 HashAlgorithm = "sha512_256"
28+
AlgorithmSHA384 HashAlgorithm = "sha384"
29+
AlgorithmSHA512 HashAlgorithm = "sha512"
30+
AlgorithmSHA3_224 HashAlgorithm = "sha3_224"
31+
AlgorithmSHA3_256 HashAlgorithm = "sha3_256"
32+
AlgorithmSHA3_384 HashAlgorithm = "sha3_384"
33+
AlgorithmSHA3_512 HashAlgorithm = "sha3_512"
34+
AlgorithmGitBlob HashAlgorithm = "gitBlob"
35+
AlgorithmGitCommit HashAlgorithm = "gitCommit"
36+
AlgorithmGitTag HashAlgorithm = "gitTag"
37+
AlgorithmGitTree HashAlgorithm = "gitTree"
38+
AlgorithmDirHash HashAlgorithm = "dirHash"
39+
)
40+
41+
// HashAlgorithms indexes the known algorithms in a dictionary
42+
// by their string value
43+
var HashAlgorithms = map[string]HashAlgorithm{
44+
"md5": AlgorithmMD5,
45+
"sha1": AlgorithmSHA1,
46+
"sha224": AlgorithmSHA224,
47+
"sha512_224": AlgorithmSHA512_224,
48+
"sha256": AlgorithmSHA256,
49+
"sha512_256": AlgorithmSHA512_256,
50+
"sha384": AlgorithmSHA384,
51+
"sha512": AlgorithmSHA512,
52+
"sha3_224": AlgorithmSHA3_224,
53+
"sha3_256": AlgorithmSHA3_256,
54+
"sha3_384": AlgorithmSHA3_384,
55+
"sha3_512": AlgorithmSHA3_512,
56+
"gitBlob": AlgorithmGitBlob,
57+
"gitCommit": AlgorithmGitCommit,
58+
"gitTag": AlgorithmGitTag,
59+
"gitTree": AlgorithmGitTree,
60+
"dirHash": AlgorithmDirHash,
61+
}
62+
63+
// HexLength returns the expected length of an algorithm's hash when hexencoded
64+
func (algo HashAlgorithm) HexLength() int {
65+
switch algo {
66+
case AlgorithmMD5:
67+
return 16
68+
case AlgorithmSHA1, AlgorithmGitBlob, AlgorithmGitCommit, AlgorithmGitTag, AlgorithmGitTree:
69+
return 20
70+
case AlgorithmSHA224, AlgorithmSHA512_224, AlgorithmSHA3_224:
71+
return 28
72+
case AlgorithmSHA256, AlgorithmSHA512_256, AlgorithmSHA3_256, AlgorithmDirHash:
73+
return 32
74+
case AlgorithmSHA384, AlgorithmSHA3_384:
75+
return 48
76+
case AlgorithmSHA512, AlgorithmSHA3_512:
77+
return 64
78+
default:
79+
return 0
80+
}
81+
}
82+
83+
// String returns the hash algorithm name as a string
84+
func (algo HashAlgorithm) String() string {
85+
return string(algo)
86+
}
87+
1988
// Indicates if a given fixed-size hash algorithm is supported by default and returns the algorithm's
2089
// digest size in bytes, if supported. We assume gitCommit and dirHash are aliases for sha1 and sha256, respectively.
2190
//
2291
// SHA digest sizes from https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf
2392
// MD5 digest size from https://www.rfc-editor.org/rfc/rfc1321.html#section-1
24-
func isSupportedFixedSizeAlgorithm(alg string) (bool, int) {
25-
algos := map[string]int{"md5": 16, "sha1": 20, "sha224": 28, "sha512_224": 28, "sha256": 32, "sha512_256": 32, "sha384": 48, "sha512": 64, "sha3_224": 28, "sha3_256": 32, "sha3_384": 48, "sha3_512": 64, "gitCommit": 20, "dirHash": 32}
26-
27-
size, ok := algos[alg]
28-
return ok, size
93+
func isSupportedFixedSizeAlgorithm(algString string) (bool, int) {
94+
algo := HashAlgorithm(algString)
95+
return algo.HexLength() > 0, algo.HexLength()
2996
}
3097

3198
func (d *ResourceDescriptor) Validate() error {

0 commit comments

Comments
 (0)
Please sign in to comment.