mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-09-25 00:15:15 +00:00
Use only dotnet
This commit is contained in:
@@ -1,115 +0,0 @@
|
||||
#if DNXCORE50
|
||||
?using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Org.BouncyCastle.Crypto.Digests;
|
||||
using Org.BouncyCastle.Crypto.Engines;
|
||||
using Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
namespace SharpCompress.Common.Rar
|
||||
{
|
||||
internal class RarRijndael : IDisposable
|
||||
{
|
||||
internal const int CRYPTO_BLOCK_SIZE = 16;
|
||||
|
||||
private readonly string password;
|
||||
private readonly byte[] salt;
|
||||
private byte[] aesInitializationVector;
|
||||
private RijndaelEngine rijndael;
|
||||
|
||||
private RarRijndael(string password, byte[] salt)
|
||||
{
|
||||
this.password = password;
|
||||
this.salt = salt;
|
||||
}
|
||||
|
||||
private byte[] ComputeHash(byte[] input)
|
||||
{
|
||||
var sha = new Sha1Digest();
|
||||
sha.BlockUpdate(input, 0, input.Length);
|
||||
byte[] result = new byte[sha.GetDigestSize()];
|
||||
sha.DoFinal(result, 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
private void Initialize()
|
||||
{
|
||||
|
||||
rijndael = new RijndaelEngine();
|
||||
aesInitializationVector = new byte[CRYPTO_BLOCK_SIZE];
|
||||
int rawLength = 2 * password.Length;
|
||||
byte[] rawPassword = new byte[rawLength + 8];
|
||||
byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
|
||||
for (int i = 0; i<password.Length; i++)
|
||||
{
|
||||
rawPassword[i * 2] = passwordBytes[i];
|
||||
rawPassword[i * 2 + 1] = 0;
|
||||
}
|
||||
for (int i = 0; i<salt.Length; i++)
|
||||
{
|
||||
rawPassword[i + rawLength] = salt[i];
|
||||
}
|
||||
|
||||
|
||||
const int noOfRounds = (1 << 18);
|
||||
IList<byte> bytes = new List<byte>();
|
||||
byte[] digest;
|
||||
|
||||
//TODO slow code below, find ways to optimize
|
||||
for (int i = 0; i<noOfRounds; i++)
|
||||
{
|
||||
bytes.AddRange(rawPassword);
|
||||
|
||||
bytes.AddRange(new[] { (byte)i, (byte)(i >> 8), (byte)(i >> CRYPTO_BLOCK_SIZE) });
|
||||
if (i % (noOfRounds / CRYPTO_BLOCK_SIZE) == 0)
|
||||
{
|
||||
digest = ComputeHash(bytes.ToArray());
|
||||
aesInitializationVector[i / (noOfRounds / CRYPTO_BLOCK_SIZE)] = digest[19];
|
||||
}
|
||||
}
|
||||
|
||||
digest = ComputeHash(bytes.ToArray());
|
||||
//slow code ends
|
||||
|
||||
byte[] aesKey = new byte[CRYPTO_BLOCK_SIZE];
|
||||
for (int i = 0; i< 4; i++)
|
||||
for (int j = 0; j< 4; j++)
|
||||
aesKey[i * 4 + j] = (byte)
|
||||
(((digest[i * 4] * 0x1000000) & 0xff000000 |
|
||||
(uint)((digest[i * 4 + 1] * 0x10000) & 0xff0000) |
|
||||
(uint)((digest[i * 4 + 2] * 0x100) & 0xff00) |
|
||||
(uint)(digest[i * 4 + 3] & 0xff)) >> (j* 8));
|
||||
|
||||
|
||||
rijndael.Init(false, new KeyParameter(aesKey));
|
||||
|
||||
}
|
||||
|
||||
public static RarRijndael InitializeFrom(string password, byte[] salt)
|
||||
{
|
||||
var rijndael = new RarRijndael(password, salt);
|
||||
rijndael.Initialize();
|
||||
return rijndael;
|
||||
}
|
||||
|
||||
public byte[] ProcessBlock(byte[] cipherText)
|
||||
{
|
||||
var plainText = new byte[CRYPTO_BLOCK_SIZE];
|
||||
var decryptedBytes = new List<byte>();
|
||||
rijndael.ProcessBlock(cipherText, 0, plainText, 0);
|
||||
|
||||
for (int j = 0; j<plainText.Length; j++)
|
||||
decryptedBytes.Add((byte)(plainText[j] ^ aesInitializationVector[j % 16])); //32:114, 33:101
|
||||
|
||||
for (int j = 0; j<aesInitializationVector.Length; j++)
|
||||
aesInitializationVector[j] = cipherText[j];
|
||||
return decryptedBytes.ToArray();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,20 +1,21 @@
|
||||
#if !DNXCORE50
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Org.BouncyCastle.Crypto.Digests;
|
||||
using Org.BouncyCastle.Crypto.Engines;
|
||||
using Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
namespace SharpCompress.Common.Rar
|
||||
{
|
||||
internal class RarRijndael : IDisposable
|
||||
{
|
||||
internal const int CRYPTO_BLOCK_SIZE = 16;
|
||||
|
||||
private readonly string password;
|
||||
private readonly byte[] salt;
|
||||
private byte[] aesInitializationVector;
|
||||
private Rijndael rijndael;
|
||||
private RijndaelEngine rijndael;
|
||||
|
||||
private RarRijndael(string password, byte[] salt)
|
||||
{
|
||||
@@ -22,24 +23,33 @@ namespace SharpCompress.Common.Rar
|
||||
this.salt = salt;
|
||||
}
|
||||
|
||||
private byte[] ComputeHash(byte[] input)
|
||||
{
|
||||
var sha = new Sha1Digest();
|
||||
sha.BlockUpdate(input, 0, input.Length);
|
||||
byte[] result = new byte[sha.GetDigestSize()];
|
||||
sha.DoFinal(result, 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
private void Initialize()
|
||||
{
|
||||
|
||||
rijndael = new RijndaelManaged() { Padding = PaddingMode.None };
|
||||
rijndael = new RijndaelEngine();
|
||||
aesInitializationVector = new byte[CRYPTO_BLOCK_SIZE];
|
||||
int rawLength = 2 * password.Length;
|
||||
int rawLength = 2*password.Length;
|
||||
byte[] rawPassword = new byte[rawLength + 8];
|
||||
byte[] passwordBytes = Encoding.Unicode.GetBytes(password);
|
||||
for (int i = 0; i < rawLength; i++)
|
||||
byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
|
||||
for (int i = 0; i < password.Length; i++)
|
||||
{
|
||||
rawPassword[i] = passwordBytes[i];
|
||||
rawPassword[i*2] = passwordBytes[i];
|
||||
rawPassword[i*2 + 1] = 0;
|
||||
}
|
||||
for (int i = 0; i < salt.Length; i++)
|
||||
{
|
||||
rawPassword[i + rawLength] = salt[i];
|
||||
}
|
||||
|
||||
SHA1 sha = new SHA1CryptoServiceProvider();
|
||||
|
||||
const int noOfRounds = (1 << 18);
|
||||
IList<byte> bytes = new List<byte>();
|
||||
@@ -50,29 +60,31 @@ namespace SharpCompress.Common.Rar
|
||||
{
|
||||
bytes.AddRange(rawPassword);
|
||||
|
||||
bytes.AddRange(new[] { (byte)i, (byte)(i >> 8), (byte)(i >> CRYPTO_BLOCK_SIZE) });
|
||||
if (i % (noOfRounds / CRYPTO_BLOCK_SIZE) == 0)
|
||||
bytes.AddRange(new[]
|
||||
{
|
||||
digest = sha.ComputeHash(bytes.ToArray());
|
||||
aesInitializationVector[i / (noOfRounds / CRYPTO_BLOCK_SIZE)] = digest[19];
|
||||
(byte) i, (byte) (i >> 8), (byte) (i >> CRYPTO_BLOCK_SIZE)
|
||||
});
|
||||
if (i%(noOfRounds/CRYPTO_BLOCK_SIZE) == 0)
|
||||
{
|
||||
digest = ComputeHash(bytes.ToArray());
|
||||
aesInitializationVector[i/(noOfRounds/CRYPTO_BLOCK_SIZE)] = digest[19];
|
||||
}
|
||||
}
|
||||
|
||||
digest = sha.ComputeHash(bytes.ToArray());
|
||||
digest = ComputeHash(bytes.ToArray());
|
||||
//slow code ends
|
||||
|
||||
byte[] aesKey = new byte[CRYPTO_BLOCK_SIZE];
|
||||
for (int i = 0; i < 4; i++)
|
||||
for (int j = 0; j < 4; j++)
|
||||
aesKey[i * 4 + j] = (byte)
|
||||
(((digest[i * 4] * 0x1000000) & 0xff000000 |
|
||||
(uint)((digest[i * 4 + 1] * 0x10000) & 0xff0000) |
|
||||
(uint)((digest[i * 4 + 2] * 0x100) & 0xff00) |
|
||||
(uint)(digest[i * 4 + 3] & 0xff)) >> (j * 8));
|
||||
aesKey[i*4 + j] = (byte)
|
||||
(((digest[i*4]*0x1000000) & 0xff000000 |
|
||||
(uint) ((digest[i*4 + 1]*0x10000) & 0xff0000) |
|
||||
(uint) ((digest[i*4 + 2]*0x100) & 0xff00) |
|
||||
(uint) (digest[i*4 + 3] & 0xff)) >> (j*8));
|
||||
|
||||
rijndael.IV = new byte[CRYPTO_BLOCK_SIZE];
|
||||
rijndael.Key = aesKey;
|
||||
rijndael.BlockSize = CRYPTO_BLOCK_SIZE * 8;
|
||||
|
||||
rijndael.Init(false, new KeyParameter(aesKey));
|
||||
|
||||
}
|
||||
|
||||
@@ -87,17 +99,10 @@ namespace SharpCompress.Common.Rar
|
||||
{
|
||||
var plainText = new byte[CRYPTO_BLOCK_SIZE];
|
||||
var decryptedBytes = new List<byte>();
|
||||
var decryptor = rijndael.CreateDecryptor();
|
||||
using (var msDecrypt = new MemoryStream(cipherText))
|
||||
{
|
||||
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
|
||||
{
|
||||
csDecrypt.ReadFully(plainText);
|
||||
}
|
||||
}
|
||||
rijndael.ProcessBlock(cipherText, 0, plainText, 0);
|
||||
|
||||
for (int j = 0; j < plainText.Length; j++)
|
||||
decryptedBytes.Add((byte)(plainText[j] ^ aesInitializationVector[j % 16])); //32:114, 33:101
|
||||
decryptedBytes.Add((byte) (plainText[j] ^ aesInitializationVector[j%16])); //32:114, 33:101
|
||||
|
||||
for (int j = 0; j < aesInitializationVector.Length; j++)
|
||||
aesInitializationVector[j] = cipherText[j];
|
||||
@@ -106,8 +111,6 @@ namespace SharpCompress.Common.Rar
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
((IDisposable)rijndael).Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
18
SharpCompress/Common/Tar/Headers/EntryType.cs
Normal file
18
SharpCompress/Common/Tar/Headers/EntryType.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
namespace SharpCompress.Common.Tar.Headers
|
||||
{
|
||||
internal enum EntryType : byte
|
||||
{
|
||||
File = 0,
|
||||
OldFile = (byte) '0',
|
||||
HardLink = (byte) '1',
|
||||
SymLink = (byte) '2',
|
||||
CharDevice = (byte) '3',
|
||||
BlockDevice = (byte) '4',
|
||||
Directory = (byte) '5',
|
||||
Fifo = (byte) '6',
|
||||
LongLink = (byte) 'K',
|
||||
LongName = (byte) 'L',
|
||||
SparseFile = (byte) 'S',
|
||||
VolumeHeader = (byte) 'V',
|
||||
}
|
||||
}
|
||||
@@ -1,28 +1,9 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
#if !PORTABLE
|
||||
using System.Net;
|
||||
#endif
|
||||
using System.Text;
|
||||
|
||||
namespace SharpCompress.Common.Tar.Headers
|
||||
{
|
||||
internal enum EntryType : byte
|
||||
{
|
||||
File = 0,
|
||||
OldFile = (byte) '0',
|
||||
HardLink = (byte) '1',
|
||||
SymLink = (byte) '2',
|
||||
CharDevice = (byte) '3',
|
||||
BlockDevice = (byte) '4',
|
||||
Directory = (byte) '5',
|
||||
Fifo = (byte) '6',
|
||||
LongLink = (byte) 'K',
|
||||
LongName = (byte) 'L',
|
||||
SparseFile = (byte) 'S',
|
||||
VolumeHeader = (byte) 'V',
|
||||
}
|
||||
|
||||
internal class TarHeader
|
||||
{
|
||||
internal static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
||||
@@ -64,11 +45,7 @@ namespace SharpCompress.Common.Tar.Headers
|
||||
|
||||
if (Size >= 0x1FFFFFFFF)
|
||||
{
|
||||
#if PORTABLE || NETFX_CORE
|
||||
byte[] bytes = BitConverter.GetBytes(Utility.HostToNetworkOrder(Size));
|
||||
#else
|
||||
byte[] bytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(Size));
|
||||
#endif
|
||||
var bytes12 = new byte[12];
|
||||
bytes.CopyTo(bytes12, 12 - bytes.Length);
|
||||
bytes12[0] |= 0x80;
|
||||
@@ -120,11 +97,7 @@ namespace SharpCompress.Common.Tar.Headers
|
||||
if ((buffer[124] & 0x80) == 0x80) // if size in binary
|
||||
{
|
||||
long sizeBigEndian = BitConverter.ToInt64(buffer, 0x80);
|
||||
#if PORTABLE || NETFX_CORE
|
||||
Size = Utility.NetworkToHostOrder(sizeBigEndian);
|
||||
#else
|
||||
Size = IPAddress.NetworkToHostOrder(sizeBigEndian);
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -1,190 +0,0 @@
|
||||
#if DNXCORE50
|
||||
using System;
|
||||
using System.IO;
|
||||
using Org.BouncyCastle.Crypto;
|
||||
using Org.BouncyCastle.Crypto.Engines;
|
||||
using Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
namespace SharpCompress.Common.Zip
|
||||
{
|
||||
internal class WinzipAesCryptoStream : Stream
|
||||
{
|
||||
private const int BLOCK_SIZE_IN_BYTES = 16;
|
||||
private readonly IBufferedCipher rijndael;
|
||||
private readonly byte[] counter = new byte[BLOCK_SIZE_IN_BYTES];
|
||||
private readonly Stream stream;
|
||||
private int nonce = 1;
|
||||
private byte[] counterOut = new byte[BLOCK_SIZE_IN_BYTES];
|
||||
private bool isFinalBlock;
|
||||
private long totalBytesLeftToRead;
|
||||
private bool isDisposed;
|
||||
|
||||
internal WinzipAesCryptoStream(Stream stream, WinzipAesEncryptionData winzipAesEncryptionData, long length)
|
||||
{
|
||||
this.stream = stream;
|
||||
totalBytesLeftToRead = length;
|
||||
|
||||
rijndael = CreateRijndael(winzipAesEncryptionData);
|
||||
}
|
||||
|
||||
private IBufferedCipher CreateRijndael(WinzipAesEncryptionData winzipAesEncryptionData)
|
||||
{
|
||||
var blockCipher = new BufferedBlockCipher(new RijndaelEngine());
|
||||
var param = new KeyParameter(winzipAesEncryptionData.KeyBytes);
|
||||
blockCipher.Init(true, param);
|
||||
return blockCipher;
|
||||
}
|
||||
|
||||
public override bool CanRead
|
||||
{
|
||||
get
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool CanSeek
|
||||
{
|
||||
get
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool CanWrite
|
||||
{
|
||||
get
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override long Length
|
||||
{
|
||||
get
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public override long Position
|
||||
{
|
||||
get
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
set
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (isDisposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
isDisposed = true;
|
||||
if (disposing)
|
||||
{
|
||||
//read out last 10 auth bytes
|
||||
var ten = new byte[10];
|
||||
stream.Read(ten, 0, 10);
|
||||
stream.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
if (totalBytesLeftToRead == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
int bytesToRead = count;
|
||||
if (count > totalBytesLeftToRead)
|
||||
{
|
||||
bytesToRead = (int)totalBytesLeftToRead;
|
||||
}
|
||||
int read = stream.Read(buffer, offset, bytesToRead);
|
||||
totalBytesLeftToRead = read;
|
||||
|
||||
ReadTransformBlocks(buffer, offset, read);
|
||||
|
||||
return read;
|
||||
}
|
||||
|
||||
private int ReadTransformOneBlock(byte[] buffer, int offset, int last)
|
||||
{
|
||||
if (isFinalBlock)
|
||||
{
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
int bytesRemaining = last - offset;
|
||||
int bytesToRead = (bytesRemaining > BLOCK_SIZE_IN_BYTES)
|
||||
? BLOCK_SIZE_IN_BYTES
|
||||
: bytesRemaining;
|
||||
|
||||
// update the counter
|
||||
Array.Copy(BitConverter.GetBytes(nonce++), 0, counter, 0, 4);
|
||||
|
||||
// Determine if this is the final block
|
||||
if ((bytesToRead == bytesRemaining) && (totalBytesLeftToRead == 0))
|
||||
{
|
||||
counterOut = rijndael.DoFinal(counter, 0, BLOCK_SIZE_IN_BYTES);
|
||||
|
||||
isFinalBlock = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
rijndael.ProcessBytes(counter, 0, BLOCK_SIZE_IN_BYTES, counterOut, 0);
|
||||
}
|
||||
XorInPlace(buffer, offset, bytesToRead);
|
||||
return bytesToRead;
|
||||
}
|
||||
|
||||
|
||||
private void XorInPlace(byte[] buffer, int offset, int count)
|
||||
{
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
buffer[offset + i] = (byte)(counterOut[i] ^ buffer[offset + i]);
|
||||
}
|
||||
}
|
||||
|
||||
private void ReadTransformBlocks(byte[] buffer, int offset, int count)
|
||||
{
|
||||
int posn = offset;
|
||||
int last = count + offset;
|
||||
|
||||
while (posn < buffer.Length && posn < last)
|
||||
{
|
||||
int n = ReadTransformOneBlock(buffer, posn, last);
|
||||
posn += n;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,17 +1,17 @@
|
||||
#if !DNXCORE50
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Security.Cryptography;
|
||||
using Org.BouncyCastle.Crypto;
|
||||
using Org.BouncyCastle.Crypto.Engines;
|
||||
using Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
namespace SharpCompress.Common.Zip
|
||||
{
|
||||
internal class WinzipAesCryptoStream : Stream
|
||||
{
|
||||
private const int BLOCK_SIZE_IN_BYTES = 16;
|
||||
private readonly SymmetricAlgorithm cipher;
|
||||
private readonly IBufferedCipher rijndael;
|
||||
private readonly byte[] counter = new byte[BLOCK_SIZE_IN_BYTES];
|
||||
private readonly Stream stream;
|
||||
private readonly ICryptoTransform transform;
|
||||
private int nonce = 1;
|
||||
private byte[] counterOut = new byte[BLOCK_SIZE_IN_BYTES];
|
||||
private bool isFinalBlock;
|
||||
@@ -23,46 +23,59 @@ namespace SharpCompress.Common.Zip
|
||||
this.stream = stream;
|
||||
totalBytesLeftToRead = length;
|
||||
|
||||
cipher = CreateCipher(winzipAesEncryptionData);
|
||||
|
||||
var iv = new byte[BLOCK_SIZE_IN_BYTES];
|
||||
transform = cipher.CreateEncryptor(winzipAesEncryptionData.KeyBytes, iv);
|
||||
rijndael = CreateRijndael(winzipAesEncryptionData);
|
||||
}
|
||||
|
||||
private SymmetricAlgorithm CreateCipher(WinzipAesEncryptionData winzipAesEncryptionData)
|
||||
private IBufferedCipher CreateRijndael(WinzipAesEncryptionData winzipAesEncryptionData)
|
||||
{
|
||||
RijndaelManaged cipher = new RijndaelManaged();
|
||||
cipher.BlockSize = BLOCK_SIZE_IN_BYTES * 8;
|
||||
cipher.KeySize = winzipAesEncryptionData.KeyBytes.Length * 8;
|
||||
cipher.Mode = CipherMode.ECB;
|
||||
cipher.Padding = PaddingMode.None;
|
||||
return cipher;
|
||||
var blockCipher = new BufferedBlockCipher(new RijndaelEngine());
|
||||
var param = new KeyParameter(winzipAesEncryptionData.KeyBytes);
|
||||
blockCipher.Init(true, param);
|
||||
return blockCipher;
|
||||
}
|
||||
|
||||
public override bool CanRead
|
||||
{
|
||||
get { return true; }
|
||||
get
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool CanSeek
|
||||
{
|
||||
get { return false; }
|
||||
get
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool CanWrite
|
||||
{
|
||||
get { return false; }
|
||||
get
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override long Length
|
||||
{
|
||||
get { throw new NotSupportedException(); }
|
||||
get
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public override long Position
|
||||
{
|
||||
get { throw new NotSupportedException(); }
|
||||
set { throw new NotSupportedException(); }
|
||||
get
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
set
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
@@ -83,7 +96,7 @@ namespace SharpCompress.Common.Zip
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
@@ -98,7 +111,7 @@ namespace SharpCompress.Common.Zip
|
||||
bytesToRead = (int)totalBytesLeftToRead;
|
||||
}
|
||||
int read = stream.Read(buffer, offset, bytesToRead);
|
||||
totalBytesLeftToRead -= read;
|
||||
totalBytesLeftToRead = read;
|
||||
|
||||
ReadTransformBlocks(buffer, offset, read);
|
||||
|
||||
@@ -123,20 +136,14 @@ namespace SharpCompress.Common.Zip
|
||||
// Determine if this is the final block
|
||||
if ((bytesToRead == bytesRemaining) && (totalBytesLeftToRead == 0))
|
||||
{
|
||||
counterOut = transform.TransformFinalBlock(counter,
|
||||
0,
|
||||
BLOCK_SIZE_IN_BYTES);
|
||||
counterOut = rijndael.DoFinal(counter, 0, BLOCK_SIZE_IN_BYTES);
|
||||
|
||||
isFinalBlock = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
transform.TransformBlock(counter,
|
||||
0, // offset
|
||||
BLOCK_SIZE_IN_BYTES,
|
||||
counterOut,
|
||||
0); // offset
|
||||
rijndael.ProcessBytes(counter, 0, BLOCK_SIZE_IN_BYTES, counterOut, 0);
|
||||
}
|
||||
|
||||
XorInPlace(buffer, offset, bytesToRead);
|
||||
return bytesToRead;
|
||||
}
|
||||
@@ -165,18 +172,17 @@ namespace SharpCompress.Common.Zip
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
#if DNXCORE50
|
||||
?using System;
|
||||
using System.Text;
|
||||
using SharpCompress.Crypto;
|
||||
|
||||
namespace SharpCompress.Common.Zip
|
||||
{
|
||||
internal class WinzipAesEncryptionData
|
||||
{
|
||||
private const int RFC2898_ITERATIONS = 1000;
|
||||
|
||||
private byte[] salt;
|
||||
private WinzipAesKeySize keySize;
|
||||
private byte[] passwordVerifyValue;
|
||||
private string password;
|
||||
|
||||
private byte[] generatedVerifyValue;
|
||||
|
||||
internal WinzipAesEncryptionData(WinzipAesKeySize keySize, byte[] salt, byte[] passwordVerifyValue,
|
||||
string password)
|
||||
{
|
||||
this.keySize = keySize;
|
||||
this.salt = salt;
|
||||
this.passwordVerifyValue = passwordVerifyValue;
|
||||
this.password = password;
|
||||
Initialize();
|
||||
}
|
||||
|
||||
internal byte[] IvBytes
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
internal byte[] KeyBytes
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
private int KeySizeInBytes
|
||||
{
|
||||
get { return KeyLengthInBytes(keySize);
|
||||
}
|
||||
}
|
||||
|
||||
internal static int KeyLengthInBytes(WinzipAesKeySize keySize)
|
||||
{
|
||||
switch (keySize)
|
||||
{
|
||||
case WinzipAesKeySize.KeySize128:
|
||||
return 16;
|
||||
case WinzipAesKeySize.KeySize192:
|
||||
return 24;
|
||||
case WinzipAesKeySize.KeySize256:
|
||||
return 32;
|
||||
}
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
private void Initialize()
|
||||
{
|
||||
var utf8 = new UTF8Encoding(false);
|
||||
var paramz = new PBKDF2(utf8.GetBytes(password), salt, RFC2898_ITERATIONS);
|
||||
KeyBytes = paramz.GetBytes(KeySizeInBytes);
|
||||
IvBytes = paramz.GetBytes(KeySizeInBytes);
|
||||
generatedVerifyValue = paramz.GetBytes(2);
|
||||
|
||||
|
||||
short verify = BitConverter.ToInt16(passwordVerifyValue, 0);
|
||||
if (password != null)
|
||||
{
|
||||
short generated = BitConverter.ToInt16(generatedVerifyValue, 0);
|
||||
if (verify != generated)
|
||||
throw new InvalidFormatException("bad password");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,5 +1,6 @@
|
||||
#if !DNXCORE50
|
||||
using System;
|
||||
using System.Text;
|
||||
using SharpCompress.Crypto;
|
||||
|
||||
namespace SharpCompress.Common.Zip
|
||||
{
|
||||
@@ -24,12 +25,19 @@ namespace SharpCompress.Common.Zip
|
||||
Initialize();
|
||||
}
|
||||
|
||||
internal byte[] IvBytes { get; set; }
|
||||
internal byte[] KeyBytes { get; set; }
|
||||
internal byte[] IvBytes
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
internal byte[] KeyBytes
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
private int KeySizeInBytes
|
||||
{
|
||||
get { return KeyLengthInBytes(keySize); }
|
||||
get { return KeyLengthInBytes(keySize);
|
||||
}
|
||||
}
|
||||
|
||||
internal static int KeyLengthInBytes(WinzipAesKeySize keySize)
|
||||
@@ -48,12 +56,12 @@ namespace SharpCompress.Common.Zip
|
||||
|
||||
private void Initialize()
|
||||
{
|
||||
System.Security.Cryptography.Rfc2898DeriveBytes rfc2898 =
|
||||
new System.Security.Cryptography.Rfc2898DeriveBytes(password, salt, RFC2898_ITERATIONS);
|
||||
var utf8 = new UTF8Encoding(false);
|
||||
var paramz = new PBKDF2(utf8.GetBytes(password), salt, RFC2898_ITERATIONS);
|
||||
KeyBytes = paramz.GetBytes(KeySizeInBytes);
|
||||
IvBytes = paramz.GetBytes(KeySizeInBytes);
|
||||
generatedVerifyValue = paramz.GetBytes(2);
|
||||
|
||||
KeyBytes = rfc2898.GetBytes(KeySizeInBytes); // 16 or 24 or 32 ???
|
||||
IvBytes = rfc2898.GetBytes(KeySizeInBytes);
|
||||
generatedVerifyValue = rfc2898.GetBytes(2);
|
||||
|
||||
short verify = BitConverter.ToInt16(passwordVerifyValue, 0);
|
||||
if (password != null)
|
||||
@@ -65,4 +73,3 @@ namespace SharpCompress.Common.Zip
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -2,9 +2,7 @@
|
||||
using System.IO;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
#if DNXCORE50
|
||||
using Org.BouncyCastle.Crypto.Digests;
|
||||
#endif
|
||||
using SharpCompress.Compressor.LZMA.Utilites;
|
||||
|
||||
namespace SharpCompress.Compressor.LZMA
|
||||
@@ -188,7 +186,6 @@ namespace SharpCompress.Compressor.LZMA
|
||||
}
|
||||
else
|
||||
{
|
||||
#if DNXCORE50
|
||||
var sha = new Sha256Digest();
|
||||
byte[] counter = new byte[8];
|
||||
long numRounds = 1L << mNumCyclesPower;
|
||||
@@ -207,28 +204,6 @@ namespace SharpCompress.Compressor.LZMA
|
||||
|
||||
sha.DoFinal(counter, 0);
|
||||
return counter;
|
||||
#else
|
||||
using (var sha = new SHA256CryptoServiceProvider())
|
||||
{
|
||||
byte[] counter = new byte[8];
|
||||
long numRounds = 1L << mNumCyclesPower;
|
||||
for (long round = 0; round < numRounds; round++)
|
||||
{
|
||||
sha.TransformBlock(salt, 0, salt.Length, null, 0);
|
||||
sha.TransformBlock(pass, 0, pass.Length, null, 0);
|
||||
sha.TransformBlock(counter, 0, 8, null, 0);
|
||||
|
||||
// This mirrors the counter so we don't have to convert long to byte[] each round.
|
||||
// (It also ensures the counter is little endian, which BitConverter does not.)
|
||||
for (int i = 0; i < 8; i++)
|
||||
if (++counter[i] != 0)
|
||||
break;
|
||||
}
|
||||
|
||||
sha.TransformFinalBlock(counter, 0, 0);
|
||||
return sha.Hash;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -436,8 +436,7 @@ namespace SharpCompress
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#if PORTABLE || NETFX_CORE
|
||||
|
||||
public static void CopyTo(this byte[] array, byte[] destination, int index)
|
||||
{
|
||||
Array.Copy(array, 0, destination, index, array.Length);
|
||||
@@ -469,6 +468,5 @@ namespace SharpCompress
|
||||
{
|
||||
return HostToNetworkOrder(network);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -7,33 +7,17 @@
|
||||
"licenseUrl": "",
|
||||
|
||||
"frameworks": {
|
||||
"net35": { },
|
||||
"net40": { },
|
||||
"net45": { },
|
||||
"netcore45": {
|
||||
"frameworkAssemblies": {
|
||||
"mscorlib": "",
|
||||
"System": "",
|
||||
"System.Collections": "",
|
||||
"System.IO": "",
|
||||
"System.Linq": "",
|
||||
"System.Reflection": ""
|
||||
},
|
||||
"compilationOptions": {
|
||||
"define": [ "NETFX_CORE" ]
|
||||
}
|
||||
},
|
||||
"dnxcore50": {
|
||||
"dotnet": {
|
||||
"dependencies": {
|
||||
"Microsoft.CSharp": "4.0.0",
|
||||
"System.Collections": "4.0.10",
|
||||
"System.IO": "4.0.10",
|
||||
"System.IO.FileSystem": "4.0.0-beta-23109",
|
||||
"System.Net.Primitives": "4.0.10-beta-23109",
|
||||
"System.Linq": "4.0.0-beta-23109",
|
||||
"System.Linq": "4.0.0",
|
||||
"System.Security.Cryptography.DeriveBytes": "4.0.0-beta-23109",
|
||||
"System.Security.Cryptography.Encryption.Aes": "4.0.0-beta-23109",
|
||||
"System.Security.Cryptography.Hashing.Algorithms": "4.0.0-beta-23109"
|
||||
},
|
||||
"compilationOptions": {
|
||||
"define": [ "DNXCORE50" ]
|
||||
"System.Security.Cryptography.Hashing.Algorithms": "4.0.0-beta-23109",
|
||||
"System.Text.Encoding.Extensions": "4.0.10-beta-23019"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user