diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2014-01-21 14:51:32 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2014-01-21 14:51:32 +0700 |
commit | 31525ff4a76773fa0cc85d4abcee7d5418cbd984 (patch) | |
tree | 0c3fa4c3b580f68c58f1476502fdaa1d25ec44f4 /crypto/src/util/Arrays.cs | |
parent | Bring Fp field element code mostly up-to-date with Java version (diff) | |
download | BouncyCastle.NET-ed25519-31525ff4a76773fa0cc85d4abcee7d5418cbd984.tar.xz |
Make static utility classes abstract instead of sealed
Add Arrays.GetHashCode for int[] Formatting
Diffstat (limited to 'crypto/src/util/Arrays.cs')
-rw-r--r-- | crypto/src/util/Arrays.cs | 327 |
1 files changed, 170 insertions, 157 deletions
diff --git a/crypto/src/util/Arrays.cs b/crypto/src/util/Arrays.cs index 3c083034e..5eab42bd7 100644 --- a/crypto/src/util/Arrays.cs +++ b/crypto/src/util/Arrays.cs @@ -3,26 +3,21 @@ using System.Text; namespace Org.BouncyCastle.Utilities { - /// <summary> General array utilities.</summary> - public sealed class Arrays + public abstract class Arrays { - private Arrays() + public static bool AreEqual( + bool[] a, + bool[] b) { - } - - public static bool AreEqual( - bool[] a, - bool[] b) - { - if (a == b) - return true; + if (a == b) + return true; - if (a == null || b == null) - return false; + if (a == null || b == null) + return false; return HaveSameContents(a, b); - } + } public static bool AreEqual( char[] a, @@ -44,61 +39,61 @@ namespace Org.BouncyCastle.Utilities /// <param name="b">Right side.</param> /// <returns>True if equal.</returns> public static bool AreEqual( - byte[] a, - byte[] b) - { - if (a == b) - return true; - - if (a == null || b == null) - return false; - - return HaveSameContents(a, b); - } - - [Obsolete("Use 'AreEqual' method instead")] - public static bool AreSame( - byte[] a, - byte[] b) - { - return AreEqual(a, b); - } - - /// <summary> - /// A constant time equals comparison - does not terminate early if - /// test will fail. - /// </summary> - /// <param name="a">first array</param> - /// <param name="b">second array</param> - /// <returns>true if arrays equal, false otherwise.</returns> - public static bool ConstantTimeAreEqual( - byte[] a, - byte[] b) - { - int i = a.Length; - if (i != b.Length) - return false; - int cmp = 0; - while (i != 0) - { - --i; - cmp |= (a[i] ^ b[i]); - } - return cmp == 0; - } - - public static bool AreEqual( - int[] a, - int[] b) - { - if (a == b) - return true; - - if (a == null || b == null) - return false; - - return HaveSameContents(a, b); - } + byte[] a, + byte[] b) + { + if (a == b) + return true; + + if (a == null || b == null) + return false; + + return HaveSameContents(a, b); + } + + [Obsolete("Use 'AreEqual' method instead")] + public static bool AreSame( + byte[] a, + byte[] b) + { + return AreEqual(a, b); + } + + /// <summary> + /// A constant time equals comparison - does not terminate early if + /// test will fail. + /// </summary> + /// <param name="a">first array</param> + /// <param name="b">second array</param> + /// <returns>true if arrays equal, false otherwise.</returns> + public static bool ConstantTimeAreEqual( + byte[] a, + byte[] b) + { + int i = a.Length; + if (i != b.Length) + return false; + int cmp = 0; + while (i != 0) + { + --i; + cmp |= (a[i] ^ b[i]); + } + return cmp == 0; + } + + public static bool AreEqual( + int[] a, + int[] b) + { + if (a == b) + return true; + + if (a == null || b == null) + return false; + + return HaveSameContents(a, b); + } private static bool HaveSameContents( bool[] a, @@ -133,95 +128,113 @@ namespace Org.BouncyCastle.Utilities } private static bool HaveSameContents( - byte[] a, - byte[] b) - { - int i = a.Length; - if (i != b.Length) - return false; - while (i != 0) - { - --i; - if (a[i] != b[i]) - return false; - } - return true; - } - - private static bool HaveSameContents( - int[] a, - int[] b) - { - int i = a.Length; - if (i != b.Length) - return false; - while (i != 0) - { - --i; - if (a[i] != b[i]) - return false; - } - return true; - } + byte[] a, + byte[] b) + { + int i = a.Length; + if (i != b.Length) + return false; + while (i != 0) + { + --i; + if (a[i] != b[i]) + return false; + } + return true; + } + + private static bool HaveSameContents( + int[] a, + int[] b) + { + int i = a.Length; + if (i != b.Length) + return false; + while (i != 0) + { + --i; + if (a[i] != b[i]) + return false; + } + return true; + } public static string ToString( - object[] a) - { - StringBuilder sb = new StringBuilder('['); - if (a.Length > 0) - { - sb.Append(a[0]); - for (int index = 1; index < a.Length; ++index) - { - sb.Append(", ").Append(a[index]); - } - } - sb.Append(']'); - return sb.ToString(); - } - - public static int GetHashCode( - byte[] data) - { - if (data == null) - { - return 0; - } - - int i = data.Length; - int hc = i + 1; - - while (--i >= 0) - { - hc *= 257; - hc ^= data[i]; - } - - return hc; - } - - public static byte[] Clone( - byte[] data) - { - return data == null ? null : (byte[]) data.Clone(); - } - - public static int[] Clone( - int[] data) - { - return data == null ? null : (int[]) data.Clone(); - } - - public static void Fill( - byte[] buf, - byte b) - { - int i = buf.Length; - while (i > 0) - { - buf[--i] = b; - } - } + object[] a) + { + StringBuilder sb = new StringBuilder('['); + if (a.Length > 0) + { + sb.Append(a[0]); + for (int index = 1; index < a.Length; ++index) + { + sb.Append(", ").Append(a[index]); + } + } + sb.Append(']'); + return sb.ToString(); + } + + public static int GetHashCode(byte[] data) + { + if (data == null) + { + return 0; + } + + int i = data.Length; + int hc = i + 1; + + while (--i >= 0) + { + hc *= 257; + hc ^= data[i]; + } + + return hc; + } + + public static int GetHashCode(int[] data) + { + if (data == null) + { + return 0; + } + + int i = data.Length; + int hc = i + 1; + + while (--i >= 0) + { + hc *= 257; + hc ^= data[i]; + } + + return hc; + } + + public static byte[] Clone( + byte[] data) + { + return data == null ? null : (byte[]) data.Clone(); + } + + public static int[] Clone( + int[] data) + { + return data == null ? null : (int[]) data.Clone(); + } + + public static void Fill( + byte[] buf, + byte b) + { + int i = buf.Length; + while (i > 0) + { + buf[--i] = b; + } + } public static byte[] Copy(byte[] data, int off, int len) { @@ -229,5 +242,5 @@ namespace Org.BouncyCastle.Utilities Array.Copy(data, off, result, 0, len); return result; } - } + } } |