forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDigestUtils.cs
40 lines (32 loc) · 1.25 KB
/
DigestUtils.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Security.Cryptography;
namespace Microsoft.NET.Build.Containers;
internal sealed class DigestUtils
{
/// <summary>
/// Gets digest for string <paramref name="str"/>.
/// </summary>
internal static string GetDigest(string str) => GetDigestFromSha(GetSha(str));
/// <summary>
/// Formats digest based on ready SHA <paramref name="sha"/>.
/// </summary>
internal static string GetDigestFromSha(string sha) => $"sha256:{sha}";
internal static string GetShaFromDigest(string digest)
{
if (!digest.StartsWith("sha256:", StringComparison.OrdinalIgnoreCase))
{
throw new ArgumentException($"Invalid digest '{digest}'. Digest must start with 'sha256:'.");
}
return digest.Substring("sha256:".Length);
}
/// <summary>
/// Gets the SHA of <paramref name="str"/>.
/// </summary>
internal static string GetSha(string str)
{
Span<byte> hash = stackalloc byte[SHA256.HashSizeInBytes];
SHA256.HashData(Encoding.UTF8.GetBytes(str), hash);
return Convert.ToHexString(hash).ToLowerInvariant();
}
}