Skip to content

Commit adf3104

Browse files
authored
Merge pull request #151 from eerhardt/SHAOneShot
Use SHA256 OneShot APIs
2 parents f2a478e + 02f5c8b commit adf3104

File tree

3 files changed

+6
-9
lines changed

3 files changed

+6
-9
lines changed

Microsoft.NET.Build.Containers/Image.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ public string GetDigest(JsonNode json)
152152

153153
public static string GetSha(JsonNode json)
154154
{
155-
using SHA256 mySHA256 = SHA256.Create();
156-
byte[] hash = mySHA256.ComputeHash(Encoding.UTF8.GetBytes(json.ToJsonString()));
155+
Span<byte> hash = stackalloc byte[SHA256.HashSizeInBytes];
156+
SHA256.HashData(Encoding.UTF8.GetBytes(json.ToJsonString()), hash);
157157

158158
return Convert.ToHexString(hash).ToLowerInvariant();
159159
}

Microsoft.NET.Build.Containers/Layer.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static Layer FromDirectory(string directory, string containerPath)
2525
public static Layer FromFiles(IEnumerable<(string path, string containerPath)> fileList)
2626
{
2727
long fileSize;
28-
byte[] hash;
28+
Span<byte> hash = stackalloc byte[SHA256.HashSizeInBytes];
2929

3030
string tempTarballPath = ContentStore.GetTempFile();
3131
using (FileStream fs = File.Create(tempTarballPath))
@@ -47,8 +47,7 @@ public static Layer FromFiles(IEnumerable<(string path, string containerPath)> f
4747

4848
fs.Position = 0;
4949

50-
using SHA256 mySHA256 = SHA256.Create();
51-
hash = mySHA256.ComputeHash(fs);
50+
SHA256.HashData(fs, hash);
5251
}
5352

5453
string contentHash = Convert.ToHexString(hash).ToLowerInvariant();

Test.Microsoft.NET.Build.Containers.Filesystem/LayerEndToEnd.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ public void SingleFileInFolder()
2828

2929
byte[] hashBytes;
3030

31-
using (SHA256 hasher = SHA256.Create())
3231
using (FileStream fs = File.OpenRead(l.BackingFile))
3332
{
34-
hashBytes = hasher.ComputeHash(fs);
33+
hashBytes = SHA256.HashData(fs);
3534
}
3635

3736
Assert.AreEqual(Convert.ToHexString(hashBytes), l.Descriptor.Digest.Substring("sha256:".Length), ignoreCase: true);
@@ -67,10 +66,9 @@ public void TwoFilesInTwoFolders()
6766

6867
byte[] hashBytes;
6968

70-
using (SHA256 hasher = SHA256.Create())
7169
using (FileStream fs = File.OpenRead(l.BackingFile))
7270
{
73-
hashBytes = hasher.ComputeHash(fs);
71+
hashBytes = SHA256.HashData(fs);
7472
}
7573

7674
Assert.AreEqual(Convert.ToHexString(hashBytes), l.Descriptor.Digest.Substring("sha256:".Length), ignoreCase: true);

0 commit comments

Comments
 (0)