1
- using System . Formats . Tar ;
1
+ using System . Diagnostics ;
2
+ using System . Formats . Tar ;
2
3
using System . IO . Compression ;
3
4
using System . Security . Cryptography ;
4
5
@@ -36,7 +37,7 @@ public static Layer FromFiles(IEnumerable<(string path, string containerPath)> f
36
37
{
37
38
long fileSize ;
38
39
Span < byte > hash = stackalloc byte [ SHA256 . HashSizeInBytes ] ;
39
- byte [ ] uncompressedHash ;
40
+ Span < byte > uncompressedHash = stackalloc byte [ SHA256 . HashSizeInBytes ] ;
40
41
41
42
string tempTarballPath = ContentStore . GetTempFile ( ) ;
42
43
using ( FileStream fs = File . Create ( tempTarballPath ) )
@@ -55,7 +56,8 @@ public static Layer FromFiles(IEnumerable<(string path, string containerPath)> f
55
56
}
56
57
} // Dispose of the TarWriter before getting the hash so the final data get written to the tar stream
57
58
58
- uncompressedHash = gz . GetHash ( ) ;
59
+ int bytesWritten = gz . GetCurrentUncompressedHash ( uncompressedHash ) ;
60
+ Debug . Assert ( bytesWritten == uncompressedHash . Length ) ;
59
61
}
60
62
61
63
fileSize = fs . Length ;
@@ -98,51 +100,41 @@ public static Layer FromFiles(IEnumerable<(string path, string containerPath)> f
98
100
/// </summary>
99
101
private sealed class HashDigestGZipStream : Stream
100
102
{
101
- private readonly SHA256 hashAlgorithm ;
102
- private readonly CryptoStream sha256Stream ;
103
+ private readonly IncrementalHash sha256Hash ;
103
104
private readonly Stream compressionStream ;
104
105
105
106
public HashDigestGZipStream ( Stream writeStream , bool leaveOpen )
106
107
{
107
- hashAlgorithm = SHA256 . Create ( ) ;
108
- sha256Stream = new CryptoStream ( Stream . Null , hashAlgorithm , CryptoStreamMode . Write ) ;
108
+ sha256Hash = IncrementalHash . CreateHash ( HashAlgorithmName . SHA256 ) ;
109
109
compressionStream = new GZipStream ( writeStream , CompressionMode . Compress , leaveOpen ) ;
110
110
}
111
111
112
112
public override bool CanWrite => true ;
113
113
114
114
public override void Write ( byte [ ] buffer , int offset , int count )
115
115
{
116
- sha256Stream . Write ( buffer , offset , count ) ;
116
+ sha256Hash . AppendData ( buffer , offset , count ) ;
117
117
compressionStream . Write ( buffer , offset , count ) ;
118
118
}
119
119
120
120
public override void Write ( ReadOnlySpan < byte > buffer )
121
121
{
122
- sha256Stream . Write ( buffer ) ;
122
+ sha256Hash . AppendData ( buffer ) ;
123
123
compressionStream . Write ( buffer ) ;
124
124
}
125
125
126
126
public override void Flush ( )
127
127
{
128
- sha256Stream . Flush ( ) ;
129
128
compressionStream . Flush ( ) ;
130
129
}
131
130
132
- internal byte [ ] GetHash ( )
133
- {
134
- sha256Stream . FlushFinalBlock ( ) ;
135
- return hashAlgorithm . Hash ! ;
136
- }
131
+ internal int GetCurrentUncompressedHash ( Span < byte > buffer ) => sha256Hash . GetCurrentHash ( buffer ) ;
137
132
138
133
protected override void Dispose ( bool disposing )
139
134
{
140
135
try
141
136
{
142
- // dispose hashAlgorithm after sha256Stream since sha256Stream references/uses it
143
- sha256Stream . Dispose ( ) ;
144
- hashAlgorithm . Dispose ( ) ;
145
-
137
+ sha256Hash . Dispose ( ) ;
146
138
compressionStream . Dispose ( ) ;
147
139
}
148
140
finally
0 commit comments