Skip to content

Commit 6f7f1e8

Browse files
committed
fix async read of aes encrypted content for .net6
1 parent b8ecc63 commit 6f7f1e8

File tree

2 files changed

+22
-78
lines changed

2 files changed

+22
-78
lines changed

src/ICSharpCode.SharpZipLib/Encryption/ZipAESStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public ZipAESStream(Stream stream, ZipAESTransform transform, CryptoStreamMode m
4040
}
4141

4242
// The final n bytes of the AES stream contain the Auth Code.
43-
private const int AUTH_CODE_LENGTH = 10;
43+
public const int AUTH_CODE_LENGTH = 10;
4444

4545
// Blocksize is always 16 here, even for AES-256 which has transform.InputBlockSize of 32.
4646
private const int CRYPTO_BLOCK_SIZE = 16;

src/ICSharpCode.SharpZipLib/Encryption/ZipAESTransform.cs

Lines changed: 21 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Security.Cryptography;
3-
using ICSharpCode.SharpZipLib.Core;
43

54
namespace ICSharpCode.SharpZipLib.Encryption
65
{
@@ -9,31 +8,6 @@ namespace ICSharpCode.SharpZipLib.Encryption
98
/// </summary>
109
internal class ZipAESTransform : ICryptoTransform
1110
{
12-
#if NET45
13-
class IncrementalHash : HMACSHA1
14-
{
15-
bool _finalised;
16-
public IncrementalHash(byte[] key) : base(key) { }
17-
public static IncrementalHash CreateHMAC(string n, byte[] key) => new IncrementalHash(key);
18-
public void AppendData(byte[] buffer, int offset, int count) => TransformBlock(buffer, offset, count, buffer, offset);
19-
public byte[] GetHashAndReset()
20-
{
21-
if (!_finalised)
22-
{
23-
byte[] dummy = new byte[0];
24-
TransformFinalBlock(dummy, 0, 0);
25-
_finalised = true;
26-
}
27-
return Hash;
28-
}
29-
}
30-
31-
static class HashAlgorithmName
32-
{
33-
public static string SHA1 = null;
34-
}
35-
#endif
36-
3711
private const int PWD_VER_LENGTH = 2;
3812

3913
// WinZip use iteration count of 1000 for PBKDF2 key generation
@@ -133,91 +107,61 @@ public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, b
133107
/// <summary>
134108
/// Returns the 2 byte password verifier
135109
/// </summary>
136-
public byte[] PwdVerifier
137-
{
138-
get
139-
{
140-
return _pwdVerifier;
141-
}
142-
}
110+
public byte[] PwdVerifier => _pwdVerifier;
143111

144112
/// <summary>
145113
/// Returns the 10 byte AUTH CODE to be checked or appended immediately following the AES data stream.
146114
/// </summary>
147-
public byte[] GetAuthCode()
148-
{
149-
if (_authCode == null)
150-
{
151-
_authCode = _hmacsha1.GetHashAndReset();
152-
}
153-
return _authCode;
154-
}
115+
public byte[] GetAuthCode() => _authCode ?? (_authCode = _hmacsha1.GetHashAndReset());
155116

156117
#region ICryptoTransform Members
157118

158119
/// <summary>
159-
/// Not implemented.
120+
/// Transform final block and read auth code
160121
/// </summary>
161122
public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
162123
{
163-
if(inputCount > 0)
124+
var buffer = Array.Empty<byte>();
125+
if (inputCount > ZipAESStream.AUTH_CODE_LENGTH)
164126
{
165-
throw new NotImplementedException("TransformFinalBlock is not implemented and inputCount is greater than 0");
127+
// At least one byte of data is preceeding the auth code
128+
int finalBlock = inputCount - ZipAESStream.AUTH_CODE_LENGTH;
129+
buffer = new byte[finalBlock];
130+
TransformBlock(inputBuffer, inputOffset, finalBlock, buffer, 0);
166131
}
167-
return Empty.Array<byte>();
132+
else if (inputCount < ZipAESStream.AUTH_CODE_LENGTH)
133+
throw new Zip.ZipException("Auth code missing from input stream");
134+
135+
// Read the authcode from the last 10 bytes
136+
_authCode = _hmacsha1.GetHashAndReset();
137+
138+
return buffer;
168139
}
169140

170141
/// <summary>
171142
/// Gets the size of the input data blocks in bytes.
172143
/// </summary>
173-
public int InputBlockSize
174-
{
175-
get
176-
{
177-
return _blockSize;
178-
}
179-
}
144+
public int InputBlockSize => _blockSize;
180145

181146
/// <summary>
182147
/// Gets the size of the output data blocks in bytes.
183148
/// </summary>
184-
public int OutputBlockSize
185-
{
186-
get
187-
{
188-
return _blockSize;
189-
}
190-
}
149+
public int OutputBlockSize => _blockSize;
191150

192151
/// <summary>
193152
/// Gets a value indicating whether multiple blocks can be transformed.
194153
/// </summary>
195-
public bool CanTransformMultipleBlocks
196-
{
197-
get
198-
{
199-
return true;
200-
}
201-
}
154+
public bool CanTransformMultipleBlocks => true;
202155

203156
/// <summary>
204157
/// Gets a value indicating whether the current transform can be reused.
205158
/// </summary>
206-
public bool CanReuseTransform
207-
{
208-
get
209-
{
210-
return true;
211-
}
212-
}
159+
public bool CanReuseTransform => true;
213160

214161
/// <summary>
215162
/// Cleanup internal state.
216163
/// </summary>
217-
public void Dispose()
218-
{
219-
_encryptor.Dispose();
220-
}
164+
public void Dispose() => _encryptor.Dispose();
221165

222166
#endregion ICryptoTransform Members
223167
}

0 commit comments

Comments
 (0)