mirror of
https://github.com/wismna/ModernKeePass.git
synced 2025-10-03 23:50:18 -04:00
WIP 2.37 - ter
This commit is contained in:

committed by
BONNEVILLE Geoffroy

parent
84e7afc819
commit
e95e62f184
@@ -52,15 +52,15 @@ namespace ModernKeePassLib.Cryptography
|
||||
|
||||
byte[] pbHash;
|
||||
|
||||
#if ModernKeePassLib
|
||||
/*#if ModernKeePassLib
|
||||
var h = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256).CreateHash();
|
||||
CryptographicBuffer.CopyToByteArray(h.GetValueAndReset(), out pbHash);
|
||||
#else
|
||||
#else*/
|
||||
using(SHA256Managed h = new SHA256Managed())
|
||||
{
|
||||
pbHash = h.ComputeHash(pbData, iOffset, cbCount);
|
||||
}
|
||||
#endif
|
||||
//#endif
|
||||
|
||||
#if DEBUG
|
||||
// Ensure the data has not been modified
|
||||
@@ -90,15 +90,15 @@ namespace ModernKeePassLib.Cryptography
|
||||
if(cbOut <= 32) pbHash = HashSha256(pbIn, iInOffset, cbIn);
|
||||
else
|
||||
{
|
||||
#if ModernKeePassLib
|
||||
/*#if ModernKeePassLib
|
||||
var h = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha512).CreateHash();
|
||||
CryptographicBuffer.CopyToByteArray(h.GetValueAndReset(), out pbHash);
|
||||
#else
|
||||
#else*/
|
||||
using(SHA512Managed h = new SHA512Managed())
|
||||
{
|
||||
pbHash = h.ComputeHash(pbIn, iInOffset, cbIn);
|
||||
}
|
||||
#endif
|
||||
//#endif
|
||||
}
|
||||
|
||||
if(cbOut == pbHash.Length) return pbHash;
|
||||
|
26
ModernKeePassLib/Cryptography/Hash/DigestManaged.cs
Normal file
26
ModernKeePassLib/Cryptography/Hash/DigestManaged.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
13
ModernKeePassLib/Cryptography/Hash/SHA256Managed.cs
Normal file
13
ModernKeePassLib/Cryptography/Hash/SHA256Managed.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
13
ModernKeePassLib/Cryptography/Hash/SHA512Managed.cs
Normal file
13
ModernKeePassLib/Cryptography/Hash/SHA512Managed.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
@@ -687,7 +687,7 @@ namespace ModernKeePassLib.Cryptography
|
||||
private static void HmacEval(byte[] pbKey, byte[] pbMsg,
|
||||
byte[] pbExpc, string strID)
|
||||
{
|
||||
#if ModernKeePassLib
|
||||
/*#if ModernKeePassLib
|
||||
// WinRT
|
||||
var h = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha256).CreateHash(CryptographicBuffer.CreateFromByteArray(pbKey));
|
||||
h.Append(CryptographicBuffer.CreateFromByteArray(pbMsg));
|
||||
@@ -701,19 +701,19 @@ namespace ModernKeePassLib.Cryptography
|
||||
throw new SecurityException("HMAC-SHA-256-" + strID + "-R");
|
||||
|
||||
// BouncyCastle
|
||||
/*var h = new HMac(new Sha256Digest());
|
||||
h.BlockUpdate(pbMsg, 0, pbMsg.Length);
|
||||
byte[] pbHash = MemUtil.EmptyByteArray;
|
||||
h.DoFinal(pbHash, 0);
|
||||
if (!MemUtil.ArraysEqual(pbHash, pbExpc))
|
||||
throw new SecurityException("HMAC-SHA-256-" + strID);
|
||||
// var h = new HMac(new Sha256Digest());
|
||||
//h.BlockUpdate(pbMsg, 0, pbMsg.Length);
|
||||
//byte[] pbHash = MemUtil.EmptyByteArray;
|
||||
//h.DoFinal(pbHash, 0);
|
||||
//if (!MemUtil.ArraysEqual(pbHash, pbExpc))
|
||||
// throw new SecurityException("HMAC-SHA-256-" + strID);
|
||||
|
||||
h.Reset();
|
||||
h.BlockUpdate(pbMsg, 0, pbMsg.Length);
|
||||
h.DoFinal(pbHash, 0);
|
||||
if (!MemUtil.ArraysEqual(pbHash, pbExpc))
|
||||
throw new SecurityException("HMAC-SHA-256-" + strID + "-R");*/
|
||||
#else
|
||||
// h.Reset();
|
||||
//h.BlockUpdate(pbMsg, 0, pbMsg.Length);
|
||||
//h.DoFinal(pbHash, 0);
|
||||
//if (!MemUtil.ArraysEqual(pbHash, pbExpc))
|
||||
// throw new SecurityException("HMAC-SHA-256-" + strID + "-R");
|
||||
#else*/
|
||||
// Original
|
||||
using(HMACSHA256 h = new HMACSHA256(pbKey))
|
||||
{
|
||||
@@ -733,7 +733,7 @@ namespace ModernKeePassLib.Cryptography
|
||||
if(!MemUtil.ArraysEqual(pbHash, pbExpc))
|
||||
throw new SecurityException("HMAC-SHA-256-" + strID + "-R");
|
||||
}
|
||||
#endif
|
||||
//#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
|
Reference in New Issue
Block a user