Copy&paste typo fix
[NovacoinLibrary.git] / Novacoin / Interop.cs
index 149774e..5c6d317 100644 (file)
@@ -1,84 +1,74 @@
-\feffusing System;
+\feff/**
+ *  Novacoin classes library
+ *  Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
+
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU Affero General Public License as
+ *  published by the Free Software Foundation, either version 3 of the
+ *  License, or (at your option) any later version.
+
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Affero General Public License for more details.
+
+ *  You should have received a copy of the GNU Affero General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using System;
 using System.Collections.Generic;
-using System.Linq;
 using System.Text;
-using System.Threading.Tasks;
 
 namespace Novacoin
 {
-    class Interop
+    /// <summary>
+    /// Miscellaneous functions
+    /// </summary>
+    public class Interop
     {
-        public static byte[] LEBytes(ushort n)
+        /// <summary>
+        /// Reverse byte array
+        /// </summary>
+        /// <param name="source">Source array</param>
+        /// <returns>Result array</returns>
+        public static byte[] ReverseBytes(byte[] source)
         {
-            byte[] resultBytes = BitConverter.GetBytes(n);
-
-            if (!BitConverter.IsLittleEndian)
-            {
-                Array.Reverse(resultBytes);
-            }
-
-            return resultBytes;
-        }
-
-        public static byte[] LEBytes(uint n)
-        {
-            byte[] resultBytes = BitConverter.GetBytes(n);
-
-            if (!BitConverter.IsLittleEndian)
-            {
-                Array.Reverse(resultBytes);
-            }
+            var b = new byte[source.Length];
 
-            return resultBytes;
-        }
+            source.CopyTo(b, 0);
 
-        public static byte[] LEBytes(ulong n)
-        {
-            byte[] resultBytes = BitConverter.GetBytes(n);
+            Array.Reverse(b);
 
-            if (!BitConverter.IsLittleEndian)
-            {
-                Array.Reverse(resultBytes);
-            }
-
-            return resultBytes;
+            return b;
         }
 
-        public static byte[] BEBytes(ushort n)
+        public static byte[] HexToArray(string hex)
         {
-            byte[] resultBytes = BitConverter.GetBytes(n);
+            int nChars = hex.Length;
+            var bytes = new byte[nChars / 2];
 
-            if (BitConverter.IsLittleEndian)
+            for (int i = 0; i < nChars; i += 2)
             {
-                Array.Reverse(resultBytes);
+                bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
             }
 
-            return resultBytes;
+            return bytes;
         }
 
-        public static byte[] BEBytes(uint n)
+        public static string ToHex(byte[] bytes)
         {
-            byte[] resultBytes = BitConverter.GetBytes(n);
-
-            if (BitConverter.IsLittleEndian)
+            var sb = new StringBuilder();
+            foreach (var b in bytes)
             {
-                Array.Reverse(resultBytes);
+                sb.AppendFormat("{0:x2}", b);
             }
-
-            return resultBytes;
+            return sb.ToString();
         }
 
-        public static byte[] BEBytes(ulong n)
+        public static int GetTime()
         {
-            byte[] resultBytes = BitConverter.GetBytes(n);
-
-            if (BitConverter.IsLittleEndian)
-            {
-                Array.Reverse(resultBytes);
-            }
-
-            return resultBytes;
+            return (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
         }
-
     }
 }