-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathDigestUtility.cs
More file actions
53 lines (43 loc) · 1.27 KB
/
DigestUtility.cs
File metadata and controls
53 lines (43 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
namespace Microsoft.ComponentDetection.Common;
using System.Collections.Generic;
public static class DigestUtility
{
private static readonly Dictionary<string, int> AlgorithmsSizes = new Dictionary<string, int>()
{
{ "sha256", 32 },
{ "sha384", 48 },
{ "sha512", 64 },
};
public static bool CheckDigest(string digest, bool throwError = true)
{
var indexOfColon = digest.IndexOf(':');
if (indexOfColon < 0 ||
indexOfColon + 1 == digest.Length ||
!ContainerImageRegex.AnchoredDigestRegexp.IsMatch(digest))
{
if (throwError)
{
throw new InvalidDigestFormatError(digest);
}
return false;
}
var algorithm = digest[..indexOfColon];
if (!AlgorithmsSizes.ContainsKey(algorithm))
{
if (throwError)
{
throw new UnsupportedAlgorithmError(digest);
}
return false;
}
if (AlgorithmsSizes[algorithm] * 2 != (digest.Length - indexOfColon - 1))
{
if (throwError)
{
throw new InvalidDigestLengthError(digest);
}
return false;
}
return true;
}
}