Skip to content

Commit

Permalink
Small refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
Haik committed Oct 27, 2023
1 parent 0d0e3ba commit 5cc95f6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
8 changes: 4 additions & 4 deletions Pandatech.Crypto/Argon2Id.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ public static bool VerifyHash(string password, byte[] hash)
return ConstantTimeComparison(hash, newHash);
}

private static bool ConstantTimeComparison(byte[] a, byte[] b)
private static bool ConstantTimeComparison(IReadOnlyList<byte> a, IReadOnlyList<byte> b)
{
var diff = (uint)a.Length ^ (uint)b.Length;
for (var i = 0; i < a.Length && i < b.Length; i++)
var diff = (ushort)a.Count ^ (ushort)b.Count;
for (var i = 0; i < a.Count && i < b.Count; i++)
{
diff |= (uint)(a[i] ^ b[i]);
diff |= (ushort)(a[i] ^ b[i]);
}

return diff == 0;
Expand Down
2 changes: 1 addition & 1 deletion Pandatech.Crypto/Pandatech.Crypto.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.1.4</Version>
<Version>1.1.5</Version>
<Title>Pandatech.Crypto</Title>
<Authors>Pandatech</Authors>
<PackageIcon>pandatech.png</PackageIcon>
Expand Down
9 changes: 5 additions & 4 deletions Pandatech.Crypto/Sha3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ public static byte[] Hash(string data)
public static bool VerifyHash(string data, byte[] hash)
{
var newHash = Hash(data);

return ConstantTimeComparison(hash, newHash);
}

private static bool ConstantTimeComparison(byte[] a, byte[] b)
private static bool ConstantTimeComparison(IReadOnlyList<byte> a, IReadOnlyList<byte> b)
{
var diff = (uint)a.Length ^ (uint)b.Length;
for (var i = 0; i < a.Length && i < b.Length; i++)
var diff = (ushort)a.Count ^ (ushort)b.Count;
for (var i = 0; i < a.Count && i < b.Count; i++)
{
diff |= (uint)(a[i] ^ b[i]);
diff |= (ushort)(a[i] ^ b[i]);
}

return diff == 0;
Expand Down

0 comments on commit 5cc95f6

Please sign in to comment.