Downgrade to net standard 1.2

This commit is contained in:
Geoffroy BONNEVILLE
2020-03-23 18:35:00 +01:00
parent b8240d482f
commit 5067f81189
60 changed files with 1250 additions and 285 deletions

View File

@@ -23,9 +23,11 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
#if !KeePassUAP
#if ModernKeePassLib
#elif !KeePassUAP
using System.Security.Cryptography;
#endif
@@ -33,8 +35,14 @@ using ModernKeePassLib.Utility;
namespace ModernKeePassLib.Cryptography.Hash
{
public sealed class Blake2b : HashAlgorithm
public sealed class Blake2b : IDisposable
{
protected int HashSizeValue;
protected internal byte[] HashValue;
protected int State = 0;
private bool m_bDisposed = false;
private const int NbRounds = 12;
private const int NbBlockBytes = 128;
private const int NbMaxOutBytes = 64;
@@ -72,6 +80,23 @@ namespace ModernKeePassLib.Cryptography.Hash
private ulong[] m_m = new ulong[16];
private ulong[] m_v = new ulong[16];
public int HashSize
{
get { return HashSizeValue; }
}
public byte[] Hash
{
get
{
if (m_bDisposed)
throw new ObjectDisposedException(null);
if (State != 0)
throw new InvalidOperationException("Blak2B Cryptography Hash Not Yet Finalized");
return (byte[])HashValue.Clone();
}
}
public Blake2b()
{
m_cbHashLength = NbMaxOutBytes;
@@ -91,7 +116,7 @@ namespace ModernKeePassLib.Cryptography.Hash
Initialize();
}
public override void Initialize()
public void Initialize()
{
Debug.Assert(m_h.Length == g_vIV.Length);
Array.Copy(g_vIV, m_h, m_h.Length);
@@ -166,7 +191,7 @@ namespace ModernKeePassLib.Cryptography.Hash
if(m_t[0] < cb) ++m_t[1];
}
protected override void HashCore(byte[] array, int ibStart, int cbSize)
private void HashCore(byte[] array, int ibStart, int cbSize)
{
Debug.Assert(m_f[0] == 0);
@@ -201,7 +226,7 @@ namespace ModernKeePassLib.Cryptography.Hash
}
}
protected override byte[] HashFinal()
private byte[] HashFinal()
{
if(m_f[0] != 0) { Debug.Assert(false); throw new InvalidOperationException(); }
Debug.Assert(((m_t[1] == 0) && (m_t[0] == 0)) ||
@@ -228,5 +253,142 @@ namespace ModernKeePassLib.Cryptography.Hash
MemUtil.ZeroByteArray(pbHash);
return pbShort;
}
public byte[] ComputeHash(Stream inputStream)
{
if (m_bDisposed)
throw new ObjectDisposedException(null);
// Default the buffer size to 4K.
byte[] buffer = new byte[4096];
int bytesRead;
do
{
bytesRead = inputStream.Read(buffer, 0, 4096);
if (bytesRead > 0)
{
HashCore(buffer, 0, bytesRead);
}
} while (bytesRead > 0);
HashValue = HashFinal();
byte[] Tmp = (byte[])HashValue.Clone();
Initialize();
return (Tmp);
}
public byte[] ComputeHash(byte[] buffer)
{
if (m_bDisposed)
throw new ObjectDisposedException(null);
// Do some validation
if (buffer == null) throw new ArgumentNullException("buffer");
HashCore(buffer, 0, buffer.Length);
HashValue = HashFinal();
byte[] Tmp = (byte[])HashValue.Clone();
Initialize();
return (Tmp);
}
public byte[] ComputeHash(byte[] buffer, int offset, int count)
{
// Do some validation
if (buffer == null)
throw new ArgumentNullException("buffer");
if (offset < 0)
throw new ArgumentOutOfRangeException("offset", "ArgumentOutOfRange_NeedNonNegNum");
if (count < 0 || (count > buffer.Length))
throw new ArgumentException("Argument_InvalidValue");
if ((buffer.Length - count) < offset)
throw new ArgumentException("Argument_InvalidOffLen");
if (m_bDisposed)
throw new ObjectDisposedException(null);
HashCore(buffer, offset, count);
HashValue = HashFinal();
byte[] Tmp = (byte[])HashValue.Clone();
Initialize();
return (Tmp);
}
public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
{
// Do some validation, we let BlockCopy do the destination array validation
if (inputBuffer == null)
throw new ArgumentNullException("inputBuffer");
if (inputOffset < 0)
throw new ArgumentOutOfRangeException("inputOffset", "ArgumentOutOfRange_NeedNonNegNum");
if (inputCount < 0 || (inputCount > inputBuffer.Length))
throw new ArgumentException("Argument_InvalidValue");
if ((inputBuffer.Length - inputCount) < inputOffset)
throw new ArgumentException("Argument_InvalidOffLen");
if (m_bDisposed)
throw new ObjectDisposedException(null);
// Change the State value
State = 1;
HashCore(inputBuffer, inputOffset, inputCount);
if ((outputBuffer != null) && ((inputBuffer != outputBuffer) || (inputOffset != outputOffset)))
Buffer.BlockCopy(inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount);
return inputCount;
}
public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
{
// Do some validation
if (inputBuffer == null)
throw new ArgumentNullException("inputBuffer");
if (inputOffset < 0)
throw new ArgumentOutOfRangeException("inputOffset", "ArgumentOutOfRange_NeedNonNegNum");
if (inputCount < 0 || (inputCount > inputBuffer.Length))
throw new ArgumentException("Argument_InvalidValue");
if ((inputBuffer.Length - inputCount) < inputOffset)
throw new ArgumentException("Argument_InvalidOffLen");
if (m_bDisposed)
throw new ObjectDisposedException(null);
HashCore(inputBuffer, inputOffset, inputCount);
HashValue = HashFinal();
byte[] outputBytes;
if (inputCount != 0)
{
outputBytes = new byte[inputCount];
Array.Copy(inputBuffer, inputOffset, outputBytes, 0, inputCount);
}
else
{
outputBytes = MemUtil.EmptyByteArray;
}
// reset the State value
State = 0;
return outputBytes;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void Clear()
{
(this as IDisposable).Dispose();
}
private void Dispose(bool disposing)
{
if (disposing)
{
if (HashValue != null)
Array.Clear(HashValue, 0, HashValue.Length);
HashValue = null;
m_bDisposed = true;
}
}
}
}

View File

@@ -0,0 +1,62 @@
using System;
using Org.BouncyCastle.Crypto.Macs;
namespace ModernKeePassLib.Cryptography.Hash
{
public class HMAC : IDisposable
{
protected HMac Hmac;
public byte[] Hash
{
get
{
var result = new byte[Hmac.GetMacSize()];
Hmac.DoFinal(result, 0);
return result;
}
}
public byte[] ComputeHash(byte[] value)
{
return ComputeHash(value, 0, value.Length);
}
public byte[] ComputeHash(byte[] value, int offset, int length)
{
if (value == null) throw new ArgumentNullException(nameof(value));
byte[] resBuf = new byte[Hmac.GetMacSize()];
Hmac.BlockUpdate(value, offset, length);
Hmac.DoFinal(resBuf, offset);
return resBuf;
}
public void TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
{
Hmac.BlockUpdate(inputBuffer, inputOffset, inputCount);
if ((outputBuffer != null) && ((inputBuffer != outputBuffer) || (inputOffset != outputOffset)))
Buffer.BlockCopy(inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount);
}
public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
{
Hmac.BlockUpdate(inputBuffer, inputOffset, inputCount);
byte[] outputBytes = new byte[inputCount];
if (inputCount != 0)
Buffer.BlockCopy(inputBuffer, inputOffset, outputBytes, 0, inputCount);
return outputBytes;
}
public void Initialize()
{
Hmac.Reset();
}
public void Dispose()
{
Hmac.Reset();
}
}
}

View File

@@ -0,0 +1,10 @@
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Security.Cryptography.Core;
namespace ModernKeePassLib.Cryptography.Hash
{
public class HMACSHA1: HashAlgorithm
{
public HMACSHA1(byte[] key) : base (MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha1).CreateHash(key.AsBuffer())) {}
}
}

View File

@@ -0,0 +1,10 @@
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Security.Cryptography.Core;
namespace ModernKeePassLib.Cryptography.Hash
{
public class HMACSHA256: HashAlgorithm
{
public HMACSHA256(byte[] key) : base (MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha256).CreateHash(key.AsBuffer())) {}
}
}

View File

@@ -0,0 +1,105 @@
using System;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Security.Cryptography.Core;
using Validation;
namespace ModernKeePassLib.Cryptography.Hash
{
public abstract class HashAlgorithm: IDisposable
{
/// <summary>
/// The platform-specific hash object.
/// </summary>
private readonly CryptographicHash _hash;
/// <summary>
/// Initializes a new instance of the <see cref="HashAlgorithm"/> class.
/// </summary>
/// <param name="hash">The platform hash.</param>
internal HashAlgorithm(CryptographicHash hash)
{
Requires.NotNull(hash, "Hash");
_hash = hash;
}
public bool CanReuseTransform => true;
public bool CanTransformMultipleBlocks => true;
public byte[] Hash => _hash.GetValueAndReset().ToArray();
public void Append(byte[] data)
{
_hash.Append(data.AsBuffer());
}
public byte[] GetValueAndReset()
{
return _hash.GetValueAndReset().ToArray();
}
#region ICryptoTransform methods
public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
{
byte[] buffer;
if (inputCount < inputBuffer.Length)
{
buffer = new byte[inputCount];
Array.Copy(inputBuffer, inputOffset, buffer, 0, inputCount);
}
else
{
buffer = inputBuffer;
}
Append(buffer);
if (outputBuffer != null)
{
Array.Copy(inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount);
}
return inputCount;
}
public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
{
this.TransformBlock(inputBuffer, inputOffset, inputCount, null, 0);
if (inputCount == inputBuffer.Length)
{
return inputBuffer;
}
var buffer = new byte[inputCount];
Array.Copy(inputBuffer, inputOffset, buffer, 0, inputCount);
return buffer;
}
public byte[] ComputeHash(byte[] value)
{
return ComputeHash(value, 0, value.Length);
}
public byte[] ComputeHash(byte[] value, int offset, int length)
{
if (value == null) throw new ArgumentNullException(nameof(value));
TransformFinalBlock(value, offset, length);
var resBuf = GetValueAndReset();
return resBuf;
}
public void Initialize()
{
}
public void Dispose()
{
}
public void Clear()
{
_hash.GetValueAndReset();
}
#endregion
}
}

View File

@@ -0,0 +1,9 @@
using Windows.Security.Cryptography.Core;
namespace ModernKeePassLib.Cryptography.Hash
{
public class SHA256Managed : HashAlgorithm
{
public SHA256Managed() : base(HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256).CreateHash()) {}
}
}

View File

@@ -0,0 +1,10 @@
using System;
using Windows.Security.Cryptography.Core;
namespace ModernKeePassLib.Cryptography.Hash
{
public class SHA512Managed: HashAlgorithm
{
public SHA512Managed() : base(HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha512).CreateHash()) {}
}
}