@@ -16,16 +16,83 @@ var (
16
16
ErrRDRequiredField = errors .New ("at least one of name, URI, or digest are required" )
17
17
)
18
18
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
+
19
88
// Indicates if a given fixed-size hash algorithm is supported by default and returns the algorithm's
20
89
// digest size in bytes, if supported. We assume gitCommit and dirHash are aliases for sha1 and sha256, respectively.
21
90
//
22
91
// SHA digest sizes from https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf
23
92
// 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 ()
29
96
}
30
97
31
98
func (d * ResourceDescriptor ) Validate () error {
0 commit comments