mirror of
https://github.com/wismna/ModernKeePass.git
synced 2025-10-04 08:00:16 -04:00
Code cleanup in KeePassLib
This commit is contained in:
@@ -21,8 +21,7 @@ using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
#if ModernKeePassLib
|
||||
using Windows.Security.Cryptography;
|
||||
using Windows.Security.Cryptography.Core;
|
||||
using ModernKeePassLib.Cryptography.Hash;
|
||||
#elif !KeePassUAP
|
||||
using System.Security.Cryptography;
|
||||
#endif
|
||||
@@ -102,16 +101,7 @@ namespace ModernKeePassLib.Cryptography
|
||||
{
|
||||
byte[] pbKey32 = new byte[32];
|
||||
byte[] pbIV12 = new byte[12];
|
||||
#if ModernKeePassLib
|
||||
var h = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha512)
|
||||
.HashData(CryptographicBuffer.CreateFromByteArray(pbKey));
|
||||
byte[] pbHash;
|
||||
CryptographicBuffer.CopyToByteArray(h, out pbHash);
|
||||
|
||||
Array.Copy(pbHash, pbKey32, 32);
|
||||
Array.Copy(pbHash, 32, pbIV12, 0, 12);
|
||||
MemUtil.ZeroByteArray(pbHash);
|
||||
#else
|
||||
|
||||
using(SHA512Managed h = new SHA512Managed())
|
||||
{
|
||||
byte[] pbHash = h.ComputeHash(pbKey);
|
||||
@@ -119,7 +109,6 @@ namespace ModernKeePassLib.Cryptography
|
||||
Array.Copy(pbHash, 32, pbIV12, 0, 12);
|
||||
MemUtil.ZeroByteArray(pbHash);
|
||||
}
|
||||
#endif
|
||||
|
||||
m_chacha20 = new ChaCha20Cipher(pbKey32, pbIV12, true);
|
||||
}
|
||||
|
@@ -22,18 +22,14 @@ using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using ModernKeePassLib.Cryptography.Hash;
|
||||
#if ModernKeePassLib
|
||||
using Org.BouncyCastle.Asn1.Pkcs;
|
||||
using Windows.Security.Cryptography;
|
||||
using Windows.Security.Cryptography.Core;
|
||||
using ModernKeePassLib.Cryptography.Hash;
|
||||
#elif !KeePassUAP
|
||||
using System.Security.Cryptography;
|
||||
#endif
|
||||
|
||||
using ModernKeePassLib.Native;
|
||||
using ModernKeePassLib.Utility;
|
||||
using Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
namespace ModernKeePassLib.Cryptography
|
||||
{
|
||||
@@ -56,21 +52,10 @@ namespace ModernKeePassLib.Cryptography
|
||||
#endif
|
||||
|
||||
byte[] pbHash;
|
||||
|
||||
#if ModernKeePassLib
|
||||
/*var h = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256)
|
||||
.HashData(CryptographicBuffer.CreateFromByteArray(pbData));
|
||||
CryptographicBuffer.CopyToByteArray(h, out pbHash);*/
|
||||
pbHash = new byte[32];
|
||||
var h = new Sha256Digest();
|
||||
h.BlockUpdate(pbData, iOffset, cbCount);
|
||||
h.DoFinal(pbHash, iOffset);
|
||||
#else
|
||||
using(SHA256Managed h = new SHA256Managed())
|
||||
{
|
||||
pbHash = h.ComputeHash(pbData, iOffset, cbCount);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if DEBUG
|
||||
// Ensure the data has not been modified
|
||||
@@ -100,21 +85,11 @@ namespace ModernKeePassLib.Cryptography
|
||||
if(cbOut <= 32) pbHash = HashSha256(pbIn, iInOffset, cbIn);
|
||||
else
|
||||
{
|
||||
#if ModernKeePassLib
|
||||
/*var h = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha512)
|
||||
.HashData(CryptographicBuffer.CreateFromByteArray(pbIn));
|
||||
CryptographicBuffer.CopyToByteArray(h, out pbHash);*/
|
||||
pbHash = new byte[64];
|
||||
var h = new Sha512Digest();
|
||||
h.BlockUpdate(pbIn, iInOffset, cbIn);
|
||||
h.DoFinal(pbHash, iInOffset);
|
||||
#else
|
||||
using(SHA512Managed h = new SHA512Managed())
|
||||
{
|
||||
pbHash = h.ComputeHash(pbIn, iInOffset, cbIn);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
if(cbOut == pbHash.Length) return pbHash;
|
||||
|
||||
|
@@ -16,7 +16,7 @@ namespace ModernKeePassLib.Cryptography.Hash
|
||||
{
|
||||
if (value == null) throw new ArgumentNullException(nameof(value));
|
||||
|
||||
byte[] resBuf = new byte[Hash.GetByteLength()];
|
||||
byte[] resBuf = new byte[Hash.GetDigestSize()];
|
||||
Hash.BlockUpdate(value, 0, length);
|
||||
Hash.DoFinal(resBuf, 0);
|
||||
|
||||
|
@@ -21,10 +21,8 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
|
||||
#if ModernKeePassLib
|
||||
using Windows.Security.Cryptography;
|
||||
using Windows.Security.Cryptography.Core;
|
||||
using ModernKeePassLib.Cryptography.Hash;
|
||||
#elif !KeePassUAP
|
||||
using System.Security.Cryptography;
|
||||
#endif
|
||||
@@ -87,19 +85,11 @@ namespace ModernKeePassLib.Cryptography.PasswordGenerator
|
||||
Debug.Assert(pbKey.Length >= 64);
|
||||
if((pbAdditionalEntropy != null) && (pbAdditionalEntropy.Length > 0))
|
||||
{
|
||||
#if ModernKeePassLib
|
||||
var h = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha512)
|
||||
.HashData(CryptographicBuffer.CreateFromByteArray(pbAdditionalEntropy));
|
||||
byte[] pbHash;
|
||||
CryptographicBuffer.CopyToByteArray(h, out pbHash);
|
||||
MemUtil.XorArray(pbHash, 0, pbKey, 0, pbHash.Length);
|
||||
#else
|
||||
using(SHA512Managed h = new SHA512Managed())
|
||||
{
|
||||
byte[] pbHash = h.ComputeHash(pbAdditionalEntropy);
|
||||
MemUtil.XorArray(pbHash, 0, pbKey, 0, pbHash.Length);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
return new CryptoRandomStream(CrsAlgorithm.ChaCha20, pbKey);
|
||||
|
Reference in New Issue
Block a user