mirror of
https://github.com/wismna/ModernKeePass.git
synced 2025-10-03 23:50:18 -04:00
Changed test project type to WIndows 8.1
Changed test project framework from Nunit to MSTest Changed HashAlgorithm from BouncyCastle to WinRT crypto WIP progress bar in opendatabaseusercontrol TextBox with button made generic WIP implement copy on button click in Entry Page
This commit is contained in:
@@ -1,25 +1,78 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Crypto;
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using Windows.Security.Cryptography.Core;
|
||||
using Validation;
|
||||
|
||||
namespace ModernKeePassLib.Cryptography.Hash
|
||||
{
|
||||
public abstract class HashAlgorithm: IDisposable
|
||||
{
|
||||
protected IDigest Digest;
|
||||
/// <summary>
|
||||
/// The platform-specific hash object.
|
||||
/// </summary>
|
||||
private readonly CryptographicHash _hash;
|
||||
|
||||
public byte[] Hash
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="HashAlgorithm"/> class.
|
||||
/// </summary>
|
||||
/// <param name="hash">The platform hash.</param>
|
||||
internal HashAlgorithm(CryptographicHash hash)
|
||||
{
|
||||
get
|
||||
{
|
||||
var result = new byte[Digest.GetDigestSize()];
|
||||
Digest.DoFinal(result, 0);
|
||||
return result;
|
||||
}
|
||||
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);
|
||||
@@ -29,33 +82,19 @@ namespace ModernKeePassLib.Cryptography.Hash
|
||||
{
|
||||
if (value == null) throw new ArgumentNullException(nameof(value));
|
||||
|
||||
byte[] resBuf = new byte[Digest.GetDigestSize()];
|
||||
Digest.BlockUpdate(value, 0, length);
|
||||
Digest.DoFinal(resBuf, 0);
|
||||
TransformFinalBlock(value, offset, length);
|
||||
var resBuf = GetValueAndReset();
|
||||
|
||||
return resBuf;
|
||||
}
|
||||
|
||||
|
||||
public void TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
|
||||
public void Initialize()
|
||||
{
|
||||
Digest.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)
|
||||
{
|
||||
Digest.BlockUpdate(inputBuffer, inputOffset, inputCount);
|
||||
byte[] outputBytes = new byte[inputCount];
|
||||
if (inputCount != 0)
|
||||
Buffer.BlockCopy(inputBuffer, inputOffset, outputBytes, 0, inputCount);
|
||||
return outputBytes;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Digest.Reset();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user