summary refs log tree commit diff
path: root/crypto/src/util
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2014-01-21 14:51:32 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2014-01-21 14:51:32 +0700
commit31525ff4a76773fa0cc85d4abcee7d5418cbd984 (patch)
tree0c3fa4c3b580f68c58f1476502fdaa1d25ec44f4 /crypto/src/util
parentBring Fp field element code mostly up-to-date with Java version (diff)
downloadBouncyCastle.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')
-rw-r--r--crypto/src/util/Arrays.cs327
-rw-r--r--crypto/src/util/BigIntegers.cs6
-rw-r--r--crypto/src/util/Enums.cs6
-rw-r--r--crypto/src/util/Platform.cs96
-rw-r--r--crypto/src/util/Strings.cs86
-rw-r--r--crypto/src/util/collections/CollectionUtilities.cs65
6 files changed, 288 insertions, 298 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;
         }
-	}
+    }
 }
diff --git a/crypto/src/util/BigIntegers.cs b/crypto/src/util/BigIntegers.cs
index 9e15d2559..f2d0425cc 100644
--- a/crypto/src/util/BigIntegers.cs
+++ b/crypto/src/util/BigIntegers.cs
@@ -8,14 +8,10 @@ namespace Org.BouncyCastle.Utilities
     /**
      * BigInteger utilities.
      */
-    public sealed class BigIntegers
+    public abstract class BigIntegers
     {
         private const int MaxIterations = 1000;
 
-        private BigIntegers()
-        {
-        }
-
         /**
         * Return the passed in value as an unsigned byte array.
         *
diff --git a/crypto/src/util/Enums.cs b/crypto/src/util/Enums.cs
index eaf49e1b3..8bd9c4053 100644
--- a/crypto/src/util/Enums.cs
+++ b/crypto/src/util/Enums.cs
@@ -10,12 +10,8 @@ using Org.BouncyCastle.Utilities.Date;
 
 namespace Org.BouncyCastle.Utilities
 {
-    internal sealed class Enums
+    internal abstract class Enums
     {
-        private Enums()
-        {
-        }
-
         internal static Enum GetEnumValue(System.Type enumType, string s)
         {
             if (!enumType.IsEnum)
diff --git a/crypto/src/util/Platform.cs b/crypto/src/util/Platform.cs
index cdfba789e..99d3982ea 100644
--- a/crypto/src/util/Platform.cs
+++ b/crypto/src/util/Platform.cs
@@ -11,22 +11,18 @@ using System.Collections;
 
 namespace Org.BouncyCastle.Utilities
 {
-	internal sealed class Platform
-	{
-		private Platform()
-		{
-		}
-
+    internal abstract class Platform
+    {
 #if NETCF_1_0 || NETCF_2_0
-		private static string GetNewLine()
-		{
-			MemoryStream buf = new MemoryStream();
-			StreamWriter w = new StreamWriter(buf, Encoding.UTF8);
-			w.WriteLine();
-			w.Close();
-			byte[] bs = buf.ToArray();
+        private static string GetNewLine()
+        {
+            MemoryStream buf = new MemoryStream();
+            StreamWriter w = new StreamWriter(buf, Encoding.UTF8);
+            w.WriteLine();
+            w.Close();
+            byte[] bs = buf.ToArray();
             return Encoding.UTF8.GetString(bs, 0, bs.Length);
-		}
+        }
 #else
         private static string GetNewLine()
         {
@@ -44,47 +40,47 @@ namespace Org.BouncyCastle.Utilities
         }
 
 #if NETCF_1_0 || NETCF_2_0 || SILVERLIGHT
-		internal static string GetEnvironmentVariable(
-			string variable)
-		{
-			return null;
-		}
+        internal static string GetEnvironmentVariable(
+            string variable)
+        {
+            return null;
+        }
 #else
-		internal static string GetEnvironmentVariable(
-			string variable)
-		{
-			try
-			{
-				return Environment.GetEnvironmentVariable(variable);
-			}
-			catch (System.Security.SecurityException)
-			{
-				// We don't have the required permission to read this environment variable,
-				// which is fine, just act as if it's not set
-				return null;
-			}
-		}
+        internal static string GetEnvironmentVariable(
+            string variable)
+        {
+            try
+            {
+                return Environment.GetEnvironmentVariable(variable);
+            }
+            catch (System.Security.SecurityException)
+            {
+                // We don't have the required permission to read this environment variable,
+                // which is fine, just act as if it's not set
+                return null;
+            }
+        }
 #endif
 
 #if NETCF_1_0
-		internal static Exception CreateNotImplementedException(
-			string message)
-		{
-			return new Exception("Not implemented: " + message);
-		}
+        internal static Exception CreateNotImplementedException(
+            string message)
+        {
+            return new Exception("Not implemented: " + message);
+        }
 
-		internal static bool Equals(
-			object	a,
-			object	b)
-		{
-			return a == b || (a != null && b != null && a.Equals(b));
-		}
+        internal static bool Equals(
+            object	a,
+            object	b)
+        {
+            return a == b || (a != null && b != null && a.Equals(b));
+        }
 #else
-		internal static Exception CreateNotImplementedException(
-			string message)
-		{
-			return new NotImplementedException(message);
-		}
+        internal static Exception CreateNotImplementedException(
+            string message)
+        {
+            return new NotImplementedException(message);
+        }
 #endif
 
 #if SILVERLIGHT
@@ -178,5 +174,5 @@ namespace Org.BouncyCastle.Utilities
         }
 
         internal static readonly string NewLine = GetNewLine();
-	}
+    }
 }
diff --git a/crypto/src/util/Strings.cs b/crypto/src/util/Strings.cs
index 96032bdc5..a6080f427 100644
--- a/crypto/src/util/Strings.cs
+++ b/crypto/src/util/Strings.cs
@@ -3,33 +3,29 @@ using System.Text;
 
 namespace Org.BouncyCastle.Utilities
 {
-	/// <summary> General string utilities.</summary>
-	public sealed class Strings
-	{
-		private Strings()
-		{
-		}
-
-		internal static bool IsOneOf(string s, params string[] candidates)
-		{
-			foreach (string candidate in candidates)
-			{
-				if (s == candidate)
-					return true;
-			}
-			return false;
-		}
+    /// <summary> General string utilities.</summary>
+    public abstract class Strings
+    {
+        internal static bool IsOneOf(string s, params string[] candidates)
+        {
+            foreach (string candidate in candidates)
+            {
+                if (s == candidate)
+                    return true;
+            }
+            return false;
+        }
 
-		public static string FromByteArray(
-			byte[] bs)
-		{
-			char[] cs = new char[bs.Length];
-			for (int i = 0; i < cs.Length; ++i)
-			{
-				cs[i] = Convert.ToChar(bs[i]);
-			}
-			return new string(cs);
-		}
+        public static string FromByteArray(
+            byte[] bs)
+        {
+            char[] cs = new char[bs.Length];
+            for (int i = 0; i < cs.Length; ++i)
+            {
+                cs[i] = Convert.ToChar(bs[i]);
+            }
+            return new string(cs);
+        }
 
         public static byte[] ToByteArray(
             char[] cs)
@@ -43,15 +39,15 @@ namespace Org.BouncyCastle.Utilities
         }
 
         public static byte[] ToByteArray(
-			string s)
-		{
-			byte[] bs = new byte[s.Length];
-			for (int i = 0; i < bs.Length; ++i)
-			{
-				bs[i] = Convert.ToByte(s[i]);
-			}
-			return bs;
-		}
+            string s)
+        {
+            byte[] bs = new byte[s.Length];
+            for (int i = 0; i < bs.Length; ++i)
+            {
+                bs[i] = Convert.ToByte(s[i]);
+            }
+            return bs;
+        }
 
         public static string FromAsciiByteArray(
             byte[] bytes)
@@ -87,10 +83,10 @@ namespace Org.BouncyCastle.Utilities
         }
 
         public static string FromUtf8ByteArray(
-			byte[] bytes)
-		{
-			return Encoding.UTF8.GetString(bytes, 0, bytes.Length);
-		}
+            byte[] bytes)
+        {
+            return Encoding.UTF8.GetString(bytes, 0, bytes.Length);
+        }
 
         public static byte[] ToUtf8ByteArray(
             char[] cs)
@@ -98,10 +94,10 @@ namespace Org.BouncyCastle.Utilities
             return Encoding.UTF8.GetBytes(cs);
         }
 
-		public static byte[] ToUtf8ByteArray(
-			string s)
-		{
-			return Encoding.UTF8.GetBytes(s);
-		}
-	}
+        public static byte[] ToUtf8ByteArray(
+            string s)
+        {
+            return Encoding.UTF8.GetBytes(s);
+        }
+    }
 }
diff --git a/crypto/src/util/collections/CollectionUtilities.cs b/crypto/src/util/collections/CollectionUtilities.cs
index fd0bdcc7a..18fcb6774 100644
--- a/crypto/src/util/collections/CollectionUtilities.cs
+++ b/crypto/src/util/collections/CollectionUtilities.cs
@@ -4,13 +4,9 @@ using System.Text;
 
 namespace Org.BouncyCastle.Utilities.Collections
 {
-	public sealed class CollectionUtilities
-	{
-		private CollectionUtilities()
-		{
-		}
-
-        public static void AddRange(IList to, ICollection range)
+    public abstract class CollectionUtilities
+    {
+        public static void AddRange(IList to, IEnumerable range)
         {
             foreach (object o in range)
             {
@@ -18,17 +14,15 @@ namespace Org.BouncyCastle.Utilities.Collections
             }
         }
 
-        public static bool CheckElementsAreOfType(
-			IEnumerable e,
-			Type		t)
-		{
-			foreach (object o in e)
-			{
-				if (!t.IsInstanceOfType(o))
-					return false;
-			}
-			return true;
-		}
+        public static bool CheckElementsAreOfType(IEnumerable e, Type t)
+        {
+            foreach (object o in e)
+            {
+                if (!t.IsInstanceOfType(o))
+                    return false;
+            }
+            return true;
+        }
 
         public static IDictionary ReadOnly(IDictionary d)
         {
@@ -45,27 +39,26 @@ namespace Org.BouncyCastle.Utilities.Collections
             return new UnmodifiableSetProxy(s);
         }
 
-        public static string ToString(
-			IEnumerable c)
-		{
-			StringBuilder sb = new StringBuilder("[");
+        public static string ToString(IEnumerable c)
+        {
+            StringBuilder sb = new StringBuilder("[");
 
-			IEnumerator e = c.GetEnumerator();
+            IEnumerator e = c.GetEnumerator();
 
-			if (e.MoveNext())
-			{
-				sb.Append(e.Current.ToString());
+            if (e.MoveNext())
+            {
+                sb.Append(e.Current.ToString());
 
-				while (e.MoveNext())
-				{
-					sb.Append(", ");
-					sb.Append(e.Current.ToString());
-				}
-			}
+                while (e.MoveNext())
+                {
+                    sb.Append(", ");
+                    sb.Append(e.Current.ToString());
+                }
+            }
 
-			sb.Append(']');
+            sb.Append(']');
 
-			return sb.ToString();
-		}
-	}
+            return sb.ToString();
+        }
+    }
 }