big clean up

This commit is contained in:
Adam Hathcock
2022-12-20 15:06:44 +00:00
parent 970e31a1b1
commit 959bbdcf1b
378 changed files with 59744 additions and 60562 deletions

View File

@@ -1,145 +1,144 @@
#nullable disable
#nullable disable
using System;
using System.IO;
namespace SharpCompress.Crypto
namespace SharpCompress.Crypto;
[CLSCompliant(false)]
public sealed class Crc32Stream : Stream
{
[CLSCompliant(false)]
public sealed class Crc32Stream : Stream
public const uint DefaultPolynomial = 0xedb88320u;
public const uint DefaultSeed = 0xffffffffu;
private static uint[] defaultTable;
private readonly uint[] table;
private uint hash;
private readonly Stream stream;
public Crc32Stream(Stream stream) : this(stream, DefaultPolynomial, DefaultSeed) { }
public Crc32Stream(Stream stream, uint polynomial, uint seed)
{
public const uint DefaultPolynomial = 0xedb88320u;
public const uint DefaultSeed = 0xffffffffu;
this.stream = stream;
table = InitializeTable(polynomial);
hash = seed;
}
private static uint[] defaultTable;
public Stream WrappedStream => stream;
private readonly uint[] table;
private uint hash;
public override void Flush()
{
stream.Flush();
}
private readonly Stream stream;
public override int Read(byte[] buffer, int offset, int count) =>
throw new NotSupportedException();
public Crc32Stream(Stream stream) : this(stream, DefaultPolynomial, DefaultSeed) { }
public override long Seek(long offset, SeekOrigin origin) =>
throw new NotSupportedException();
public Crc32Stream(Stream stream, uint polynomial, uint seed)
{
this.stream = stream;
table = InitializeTable(polynomial);
hash = seed;
}
public Stream WrappedStream => stream;
public override void Flush()
{
stream.Flush();
}
public override int Read(byte[] buffer, int offset, int count) =>
throw new NotSupportedException();
public override long Seek(long offset, SeekOrigin origin) =>
throw new NotSupportedException();
public override void SetLength(long value) => throw new NotSupportedException();
public override void SetLength(long value) => throw new NotSupportedException();
#if !NETFRAMEWORK && !NETSTANDARD2_0
public override void Write(ReadOnlySpan<byte> buffer)
{
stream.Write(buffer);
public override void Write(ReadOnlySpan<byte> buffer)
{
stream.Write(buffer);
hash = CalculateCrc(table, hash, buffer);
}
hash = CalculateCrc(table, hash, buffer);
}
#endif
public override void Write(byte[] buffer, int offset, int count)
public override void Write(byte[] buffer, int offset, int count)
{
stream.Write(buffer, offset, count);
hash = CalculateCrc(table, hash, buffer.AsSpan(offset, count));
}
public override void WriteByte(byte value)
{
stream.WriteByte(value);
hash = CalculateCrc(table, hash, value);
}
public override bool CanRead => stream.CanRead;
public override bool CanSeek => false;
public override bool CanWrite => stream.CanWrite;
public override long Length => throw new NotSupportedException();
public override long Position
{
get => throw new NotSupportedException();
set => throw new NotSupportedException();
}
public uint Crc => ~hash;
public static uint Compute(byte[] buffer)
{
return Compute(DefaultSeed, buffer);
}
public static uint Compute(uint seed, byte[] buffer)
{
return Compute(DefaultPolynomial, seed, buffer);
}
public static uint Compute(uint polynomial, uint seed, ReadOnlySpan<byte> buffer)
{
return ~CalculateCrc(InitializeTable(polynomial), seed, buffer);
}
private static uint[] InitializeTable(uint polynomial)
{
if (polynomial == DefaultPolynomial && defaultTable != null)
{
stream.Write(buffer, offset, count);
hash = CalculateCrc(table, hash, buffer.AsSpan(offset, count));
return defaultTable;
}
public override void WriteByte(byte value)
var createTable = new uint[256];
for (var i = 0; i < 256; i++)
{
stream.WriteByte(value);
hash = CalculateCrc(table, hash, value);
}
public override bool CanRead => stream.CanRead;
public override bool CanSeek => false;
public override bool CanWrite => stream.CanWrite;
public override long Length => throw new NotSupportedException();
public override long Position
{
get => throw new NotSupportedException();
set => throw new NotSupportedException();
}
public uint Crc => ~hash;
public static uint Compute(byte[] buffer)
{
return Compute(DefaultSeed, buffer);
}
public static uint Compute(uint seed, byte[] buffer)
{
return Compute(DefaultPolynomial, seed, buffer);
}
public static uint Compute(uint polynomial, uint seed, ReadOnlySpan<byte> buffer)
{
return ~CalculateCrc(InitializeTable(polynomial), seed, buffer);
}
private static uint[] InitializeTable(uint polynomial)
{
if (polynomial == DefaultPolynomial && defaultTable != null)
var entry = (uint)i;
for (var j = 0; j < 8; j++)
{
return defaultTable;
}
var createTable = new uint[256];
for (var i = 0; i < 256; i++)
{
var entry = (uint)i;
for (var j = 0; j < 8; j++)
if ((entry & 1) == 1)
{
if ((entry & 1) == 1)
{
entry = (entry >> 1) ^ polynomial;
}
else
{
entry = entry >> 1;
}
entry = (entry >> 1) ^ polynomial;
}
createTable[i] = entry;
}
if (polynomial == DefaultPolynomial)
{
defaultTable = createTable;
}
return createTable;
}
private static uint CalculateCrc(uint[] table, uint crc, ReadOnlySpan<byte> buffer)
{
unchecked
{
for (int i = 0; i < buffer.Length; i++)
else
{
crc = CalculateCrc(table, crc, buffer[i]);
entry >>= 1;
}
}
return crc;
createTable[i] = entry;
}
private static uint CalculateCrc(uint[] table, uint crc, byte b)
if (polynomial == DefaultPolynomial)
{
return (crc >> 8) ^ table[(crc ^ b) & 0xFF];
defaultTable = createTable;
}
return createTable;
}
private static uint CalculateCrc(uint[] table, uint crc, ReadOnlySpan<byte> buffer)
{
unchecked
{
for (var i = 0; i < buffer.Length; i++)
{
crc = CalculateCrc(table, crc, buffer[i]);
}
}
return crc;
}
private static uint CalculateCrc(uint[] table, uint crc, byte b)
{
return (crc >> 8) ^ table[(crc ^ b) & 0xFF];
}
}

View File

@@ -1,13 +1,12 @@
using System;
using System;
namespace SharpCompress.Crypto
namespace SharpCompress.Crypto;
public class CryptoException : Exception
{
public class CryptoException : Exception
{
public CryptoException() { }
public CryptoException() { }
public CryptoException(string message) : base(message) { }
public CryptoException(string message) : base(message) { }
public CryptoException(string message, Exception exception) : base(message, exception) { }
}
public CryptoException(string message, Exception exception) : base(message, exception) { }
}

View File

@@ -1,24 +1,23 @@
using System;
using System;
namespace SharpCompress.Crypto
namespace SharpCompress.Crypto;
public class DataLengthException : CryptoException
{
public class DataLengthException : CryptoException
{
/**
* base constructor.
*/
/**
* base constructor.
*/
public DataLengthException() { }
public DataLengthException() { }
/**
* create a DataLengthException with the given message.
*
* @param message the message to be carried with the exception.
*/
/**
* create a DataLengthException with the given message.
*
* @param message the message to be carried with the exception.
*/
public DataLengthException(string message) : base(message) { }
public DataLengthException(string message) : base(message) { }
public DataLengthException(string message, Exception exception) : base(message, exception)
{ }
}
public DataLengthException(string message, Exception exception) : base(message, exception)
{ }
}

View File

@@ -1,34 +1,33 @@
using System;
using System;
namespace SharpCompress.Crypto
namespace SharpCompress.Crypto;
/// <remarks>Base interface for a symmetric key block cipher.</remarks>
public interface IBlockCipher
{
/// <remarks>Base interface for a symmetric key block cipher.</remarks>
public interface IBlockCipher
{
/// <summary>The name of the algorithm this cipher implements.</summary>
string AlgorithmName { get; }
/// <summary>The name of the algorithm this cipher implements.</summary>
string AlgorithmName { get; }
/// <summary>Initialise the cipher.</summary>
/// <param name="forEncryption">Initialise for encryption if true, for decryption if false.</param>
/// <param name="parameters">The key or other data required by the cipher.</param>
void Init(bool forEncryption, ICipherParameters parameters);
/// <summary>Initialise the cipher.</summary>
/// <param name="forEncryption">Initialise for encryption if true, for decryption if false.</param>
/// <param name="parameters">The key or other data required by the cipher.</param>
void Init(bool forEncryption, ICipherParameters parameters);
/// <returns>The block size for this cipher, in bytes.</returns>
int GetBlockSize();
/// <returns>The block size for this cipher, in bytes.</returns>
int GetBlockSize();
/// <summary>Indicates whether this cipher can handle partial blocks.</summary>
bool IsPartialBlockOkay { get; }
/// <summary>Indicates whether this cipher can handle partial blocks.</summary>
bool IsPartialBlockOkay { get; }
/// <summary>Process a block.</summary>
/// <param name="inBuf">The input buffer.</param>
/// <param name="outBuf">The output buffer.</param>
/// <exception cref="DataLengthException">If input block is wrong size, or outBuf too small.</exception>
/// <returns>The number of bytes processed and produced.</returns>
int ProcessBlock(ReadOnlySpan<byte> inBuf, Span<byte> outBuf);
/// <summary>Process a block.</summary>
/// <param name="inBuf">The input buffer.</param>
/// <param name="outBuf">The output buffer.</param>
/// <exception cref="DataLengthException">If input block is wrong size, or outBuf too small.</exception>
/// <returns>The number of bytes processed and produced.</returns>
int ProcessBlock(ReadOnlySpan<byte> inBuf, Span<byte> outBuf);
/// <summary>
/// Reset the cipher to the same state as it was after the last init (if there was one).
/// </summary>
void Reset();
}
/// <summary>
/// Reset the cipher to the same state as it was after the last init (if there was one).
/// </summary>
void Reset();
}

View File

@@ -1,4 +1,3 @@
namespace SharpCompress.Crypto
{
public interface ICipherParameters { }
}
namespace SharpCompress.Crypto;
public interface ICipherParameters { }

View File

@@ -1,43 +1,42 @@
using System;
using System;
namespace SharpCompress.Crypto
namespace SharpCompress.Crypto;
public class KeyParameter : ICipherParameters
{
public class KeyParameter : ICipherParameters
private readonly byte[] key;
public KeyParameter(byte[] key)
{
private readonly byte[] key;
public KeyParameter(byte[] key)
if (key is null)
{
if (key is null)
{
throw new ArgumentNullException(nameof(key));
}
this.key = (byte[])key.Clone();
throw new ArgumentNullException(nameof(key));
}
public KeyParameter(byte[] key, int keyOff, int keyLen)
{
if (key is null)
{
throw new ArgumentNullException(nameof(key));
}
if (keyOff < 0 || keyOff > key.Length)
{
throw new ArgumentOutOfRangeException(nameof(keyOff));
}
if (keyLen < 0 || (keyOff + keyLen) > key.Length)
{
throw new ArgumentOutOfRangeException(nameof(keyLen));
}
this.key = (byte[])key.Clone();
}
this.key = new byte[keyLen];
Array.Copy(key, keyOff, this.key, 0, keyLen);
public KeyParameter(byte[] key, int keyOff, int keyLen)
{
if (key is null)
{
throw new ArgumentNullException(nameof(key));
}
if (keyOff < 0 || keyOff > key.Length)
{
throw new ArgumentOutOfRangeException(nameof(keyOff));
}
if (keyLen < 0 || (keyOff + keyLen) > key.Length)
{
throw new ArgumentOutOfRangeException(nameof(keyLen));
}
public byte[] GetKey()
{
return (byte[])key.Clone();
}
this.key = new byte[keyLen];
Array.Copy(key, keyOff, this.key, 0, keyLen);
}
public byte[] GetKey()
{
return (byte[])key.Clone();
}
}

File diff suppressed because it is too large Load Diff