summary refs log tree commit diff
path: root/crypto/src
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src')
-rw-r--r--crypto/src/math/BigInteger.cs10
-rw-r--r--crypto/src/math/ec/ECFieldElement.cs55
-rw-r--r--crypto/src/math/ec/LongArray.cs15
-rw-r--r--crypto/src/util/Arrays.cs78
4 files changed, 107 insertions, 51 deletions
diff --git a/crypto/src/math/BigInteger.cs b/crypto/src/math/BigInteger.cs
index e5ab22e92..588adaf77 100644
--- a/crypto/src/math/BigInteger.cs
+++ b/crypto/src/math/BigInteger.cs
@@ -5,6 +5,9 @@ using System.Globalization;
 #if NETCOREAPP3_0_OR_GREATER
 using System.Numerics;
 #endif
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+using System.Runtime.InteropServices;
+#endif
 using System.Runtime.Serialization;
 using System.Text;
 
@@ -1464,6 +1467,12 @@ namespace Org.BouncyCastle.Math
 
         public override int GetHashCode()
         {
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+            HashCode hc = default;
+            hc.AddBytes(MemoryMarshal.AsBytes(magnitude.AsSpan()));
+            hc.Add(sign);
+            return hc.ToHashCode();
+#else
             int hc = magnitude.Length;
             if (magnitude.Length > 0)
             {
@@ -1476,6 +1485,7 @@ namespace Org.BouncyCastle.Math
             }
 
             return sign < 0 ? ~hc : hc;
+#endif
         }
 
         // TODO Make public?
diff --git a/crypto/src/math/ec/ECFieldElement.cs b/crypto/src/math/ec/ECFieldElement.cs
index 3afc843cd..e2306a7a9 100644
--- a/crypto/src/math/ec/ECFieldElement.cs
+++ b/crypto/src/math/ec/ECFieldElement.cs
@@ -516,29 +516,24 @@ namespace Org.BouncyCastle.Math.EC
             return x3;
         }
 
-        public override bool Equals(
-            object obj)
+        public override bool Equals(object obj) => obj is FpFieldElement that && Equals(that);
+
+        public virtual bool Equals(FpFieldElement other)
         {
-            if (obj == this)
+            if (this == other)
                 return true;
 
-            FpFieldElement other = obj as FpFieldElement;
-
-            if (other == null)
-                return false;
-
-            return Equals(other);
-        }
-
-        public virtual bool Equals(
-            FpFieldElement other)
-        {
-            return q.Equals(other.q) && base.Equals(other);
+            return q.Equals(other.q)
+                && x.Equals(other.x);
         }
 
         public override int GetHashCode()
         {
-            return q.GetHashCode() ^ base.GetHashCode();
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+            return HashCode.Combine(q, x);
+#else
+            return q.GetHashCode() ^ x.GetHashCode();
+#endif
         }
     }
 
@@ -910,32 +905,26 @@ namespace Org.BouncyCastle.Math.EC
             get { return this.ks.Length >= 3 ? this.ks[2] : 0; }
         }
 
-        public override bool Equals(
-            object obj)
+        public override bool Equals(object obj) => obj is F2mFieldElement that && Equals(that);
+
+        public virtual bool Equals(F2mFieldElement other)
         {
-            if (obj == this)
+            if (this == other)
                 return true;
 
-            F2mFieldElement other = obj as F2mFieldElement;
-
-            if (other == null)
-                return false;
-
-            return Equals(other);
-        }
-
-        public virtual bool Equals(
-            F2mFieldElement other)
-        {
-            return ((this.m == other.m)
-                && (this.representation == other.representation)
+            return this.m == other.m
+                && this.representation == other.representation
                 && Arrays.AreEqual(this.ks, other.ks)
-                && (this.x.Equals(other.x)));
+                && this.x.Equals(other.x);
         }
 
         public override int GetHashCode()
         {
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+            return HashCode.Combine(x, m, Arrays.GetHashCode(ks));
+#else
             return x.GetHashCode() ^ m ^ Arrays.GetHashCode(ks);
+#endif
         }
     }
 }
diff --git a/crypto/src/math/ec/LongArray.cs b/crypto/src/math/ec/LongArray.cs
index 3475e9ecc..e492f2913 100644
--- a/crypto/src/math/ec/LongArray.cs
+++ b/crypto/src/math/ec/LongArray.cs
@@ -1245,20 +1245,7 @@ namespace Org.BouncyCastle.Math.EC
             return true;
         }
 
-        public override int GetHashCode()
-        {
-            int usedLen = GetUsedLength();
-            int hash = 1;
-            for (int i = 0; i < usedLen; i++)
-            {
-                ulong mi = m_data[i];
-                hash *= 31;
-                hash ^= (int)mi;
-                hash *= 31;
-                hash ^= (int)(mi >> 32);
-            }
-            return hash;
-        }
+        public override int GetHashCode() => Arrays.GetHashCode(m_data, 0, GetUsedLength());
 
         public LongArray Copy()
         {
diff --git a/crypto/src/util/Arrays.cs b/crypto/src/util/Arrays.cs
index ab96357b1..acb0b07c6 100644
--- a/crypto/src/util/Arrays.cs
+++ b/crypto/src/util/Arrays.cs
@@ -1,6 +1,7 @@
 using System;
 using System.Runtime.CompilerServices;
 #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+using System.Runtime.InteropServices;
 using System.Security.Cryptography;
 #endif
 using System.Text;
@@ -403,10 +404,13 @@ namespace Org.BouncyCastle.Utilities
         public static int GetHashCode(byte[] data)
         {
             if (data == null)
-            {
                 return 0;
-            }
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+            HashCode hc = default;
+            hc.AddBytes(data);
+            return hc.ToHashCode();
+#else
             int i = data.Length;
             int hc = i + 1;
 
@@ -417,15 +421,19 @@ namespace Org.BouncyCastle.Utilities
             }
 
             return hc;
+#endif
         }
 
         public static int GetHashCode(byte[] data, int off, int len)
         {
             if (data == null)
-            {
                 return 0;
-            }
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+            HashCode hc = default;
+            hc.AddBytes(data.AsSpan(off, len));
+            return hc.ToHashCode();
+#else
             int i = len;
             int hc = i + 1;
 
@@ -436,6 +444,7 @@ namespace Org.BouncyCastle.Utilities
             }
 
             return hc;
+#endif
         }
 
         public static int GetHashCode(int[] data)
@@ -443,6 +452,11 @@ namespace Org.BouncyCastle.Utilities
             if (data == null)
                 return 0;
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+            HashCode hc = default;
+            hc.AddBytes(MemoryMarshal.AsBytes(data.AsSpan()));
+            return hc.ToHashCode();
+#else
             int i = data.Length;
             int hc = i + 1;
 
@@ -453,6 +467,7 @@ namespace Org.BouncyCastle.Utilities
             }
 
             return hc;
+#endif
         }
 
         [CLSCompliant(false)]
@@ -461,6 +476,11 @@ namespace Org.BouncyCastle.Utilities
             if (data == null)
                 return 0;
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+            HashCode hc = default;
+            hc.AddBytes(MemoryMarshal.AsBytes(data.AsSpan()));
+            return hc.ToHashCode();
+#else
             int i = data.Length;
             int hc = i + 1;
 
@@ -471,6 +491,7 @@ namespace Org.BouncyCastle.Utilities
             }
 
             return hc;
+#endif
         }
 
         public static int GetHashCode(int[] data, int off, int len)
@@ -478,6 +499,11 @@ namespace Org.BouncyCastle.Utilities
             if (data == null)
                 return 0;
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+            HashCode hc = default;
+            hc.AddBytes(MemoryMarshal.AsBytes(data.AsSpan(off, len)));
+            return hc.ToHashCode();
+#else
             int i = len;
             int hc = i + 1;
 
@@ -488,6 +514,7 @@ namespace Org.BouncyCastle.Utilities
             }
 
             return hc;
+#endif
         }
 
         [CLSCompliant(false)]
@@ -496,6 +523,11 @@ namespace Org.BouncyCastle.Utilities
             if (data == null)
                 return 0;
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+            HashCode hc = default;
+            hc.AddBytes(MemoryMarshal.AsBytes(data.AsSpan()));
+            return hc.ToHashCode();
+#else
             int i = data.Length;
             int hc = i + 1;
 
@@ -506,6 +538,7 @@ namespace Org.BouncyCastle.Utilities
             }
 
             return hc;
+#endif
         }
 
         [CLSCompliant(false)]
@@ -514,6 +547,11 @@ namespace Org.BouncyCastle.Utilities
             if (data == null)
                 return 0;
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+            HashCode hc = default;
+            hc.AddBytes(MemoryMarshal.AsBytes(data.AsSpan(off, len)));
+            return hc.ToHashCode();
+#else
             int i = len;
             int hc = i + 1;
 
@@ -524,6 +562,7 @@ namespace Org.BouncyCastle.Utilities
             }
 
             return hc;
+#endif
         }
 
         [CLSCompliant(false)]
@@ -532,6 +571,11 @@ namespace Org.BouncyCastle.Utilities
             if (data == null)
                 return 0;
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+            HashCode hc = default;
+            hc.AddBytes(MemoryMarshal.AsBytes(data.AsSpan()));
+            return hc.ToHashCode();
+#else
             int i = data.Length;
             int hc = i + 1;
 
@@ -545,6 +589,7 @@ namespace Org.BouncyCastle.Utilities
             }
 
             return hc;
+#endif
         }
 
         [CLSCompliant(false)]
@@ -553,6 +598,11 @@ namespace Org.BouncyCastle.Utilities
             if (data == null)
                 return 0;
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+            HashCode hc = default;
+            hc.AddBytes(MemoryMarshal.AsBytes(data.AsSpan(off, len)));
+            return hc.ToHashCode();
+#else
             int i = len;
             int hc = i + 1;
 
@@ -566,6 +616,7 @@ namespace Org.BouncyCastle.Utilities
             }
 
             return hc;
+#endif
         }
 
         public static int GetHashCode(object[] data)
@@ -574,6 +625,15 @@ namespace Org.BouncyCastle.Utilities
                 return 0;
 
             int len = data.Length;
+
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+            HashCode hc = default;
+            for (int i = 0; i < len; ++i)
+            {
+                hc.Add(data[i]);
+            }
+            return hc.ToHashCode();
+#else
             int hc = len + 1;
             for (int i = 0; i < len; ++i)
             {
@@ -581,6 +641,7 @@ namespace Org.BouncyCastle.Utilities
                 hc ^= Objects.GetHashCode(data[i]);
             }
             return hc;
+#endif
         }
 
         public static int GetHashCode(object[] data, int off, int len)
@@ -588,6 +649,14 @@ namespace Org.BouncyCastle.Utilities
             if (data == null)
                 return 0;
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+            HashCode hc = default;
+            for (int i = 0; i < len; ++i)
+            {
+                hc.Add(data[off + i]);
+            }
+            return hc.ToHashCode();
+#else
             int hc = len + 1;
             for (int i = 0; i < len; ++i)
             {
@@ -595,6 +664,7 @@ namespace Org.BouncyCastle.Utilities
                 hc ^= Objects.GetHashCode(data[off + i]);
             }
             return hc;
+#endif
         }
 
         public static bool[] Clone(bool[] data)