WIP 2.37 - ter

This commit is contained in:
bg45
2017-10-22 16:44:17 -04:00
committed by BONNEVILLE Geoffroy
parent 84e7afc819
commit e95e62f184
8 changed files with 134 additions and 54 deletions

View File

@@ -0,0 +1,26 @@
using System;
using Org.BouncyCastle.Crypto;
namespace ModernKeePassLib.Cryptography.Hash
{
public abstract class DigestManaged : IDisposable
{
protected IDigest _hash;
public byte[] ComputeHash(byte[] value, int offset, int length)
{
if (value == null) throw new ArgumentNullException(nameof(value));
byte[] resBuf = new byte[_hash.GetDigestSize()];
_hash.BlockUpdate(value, offset, length);
_hash.DoFinal(resBuf, 0);
return resBuf;
}
public void Dispose()
{
_hash.Reset();
}
}
}

View File

@@ -6,30 +6,54 @@ using Org.BouncyCastle.Crypto.Parameters;
namespace ModernKeePassLib.Cryptography.Hash
{
public class HMACSHA256: IDisposable
{
private readonly HMac _hmac;
private readonly HMac _hmac;
public HMACSHA256(byte[] key)
{
_hmac = new HMac(new Sha256Digest());
_hmac.Init(new KeyParameter(key));
}
public byte[] Hash
{
get
{
byte[] resBuf = new byte[_hmac.GetMacSize()];
_hmac.DoFinal(resBuf, 0);
return resBuf;
}
}
public byte[] ComputeHash(byte[] value)
{
if (value == null) throw new ArgumentNullException("value");
public HMACSHA256(byte[] key)
{
_hmac = new HMac(new Sha256Digest());
_hmac.Init(new KeyParameter(key));
}
byte[] resBuf = new byte[_hmac.GetMacSize()];
_hmac.BlockUpdate(value, 0, value.Length);
_hmac.DoFinal(resBuf, 0);
public byte[] ComputeHash(byte[] value)
{
if (value == null) throw new ArgumentNullException(nameof(value));
return resBuf;
}
byte[] resBuf = new byte[_hmac.GetMacSize()];
_hmac.BlockUpdate(value, 0, value.Length);
_hmac.DoFinal(resBuf, 0);
return resBuf;
}
public void Dispose()
{
_hmac.Reset();
}
internal void TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset )
{
_hmac.BlockUpdate(inputBuffer, inputOffset, inputCount);
}
internal void TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
{
_hmac.DoFinal(inputBuffer, inputOffset);
}
internal void Initialize()
{
_hmac.Reset();
}
}
}

View File

@@ -0,0 +1,13 @@
using System;
using Org.BouncyCastle.Crypto.Digests;
namespace ModernKeePassLib.Cryptography.Hash
{
public class SHA256Managed : DigestManaged
{
public SHA256Managed()
{
_hash = new Sha256Digest();
}
}
}

View File

@@ -0,0 +1,13 @@
using System;
using Org.BouncyCastle.Crypto.Digests;
namespace ModernKeePassLib.Cryptography.Hash
{
public class SHA512Managed : DigestManaged
{
public SHA512Managed()
{
_hash = new Sha512Digest();
}
}
}