Chain trust score computation.
[NovacoinLibrary.git] / Novacoin / Interop.cs
1 \feff/**
2  *  Novacoin classes library
3  *  Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
4
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Affero General Public License as
7  *  published by the Free Software Foundation, either version 3 of the
8  *  License, or (at your option) any later version.
9
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Affero General Public License for more details.
14
15  *  You should have received a copy of the GNU Affero General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 using System;
20 using System.Collections.Generic;
21 using System.Text;
22
23 namespace Novacoin
24 {
25     /// <summary>
26     /// Miscellaneous functions
27     /// </summary>
28     public class Interop
29     {
30         /// <summary>
31         /// Convert array of unsigned integers to array of bytes.
32         /// </summary>
33         /// <param name="values">Array of unsigned integer values.</param>
34         /// <returns>Byte array</returns>
35         public static byte[] LEBytes(uint[] values)
36         {
37             var result = new byte[values.Length * sizeof(uint)];
38             Buffer.BlockCopy(values, 0, result, 0, result.Length);
39
40             return result;
41         }
42
43         /// <summary>
44         /// Convert byte array to array of unsigned integers.
45         /// </summary>
46         /// <param name="bytes">Byte array.</param>
47         /// <returns>Array of integers</returns>
48         public static uint[] ToUInt32Array(byte[] bytes)
49         {
50             var result = new uint[bytes.Length / sizeof(uint)];
51             Buffer.BlockCopy(bytes, 0, result, 0, bytes.Length);
52
53             return result;
54         }
55
56         /// <summary>
57         /// Reverse byte array
58         /// </summary>
59         /// <param name="source">Source array</param>
60         /// <returns>Result array</returns>
61         public static byte[] ReverseBytes(byte[] source)
62         {
63             var b = new byte[source.Length];
64
65             source.CopyTo(b, 0);
66
67             Array.Reverse(b);
68
69             return b;
70         }
71
72         public static byte[] HexToArray(string hex)
73         {
74             int nChars = hex.Length;
75             var bytes = new byte[nChars / 2];
76
77             for (int i = 0; i < nChars; i += 2)
78             {
79                 bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
80             }
81
82             return bytes;
83         }
84
85         public static byte[] TrimArray(byte[] bytes)
86         {
87             int trimStart = bytes.Length - 1;
88             while (trimStart >= 0 && bytes[trimStart] == 0)
89             {
90                 trimStart--;
91             }
92
93             byte[] result = new byte[trimStart + 1];
94             Array.Copy(bytes, 0, result, 0, trimStart + 1);
95
96             return result;
97         }
98
99         public static string ToHex(byte[] bytes)
100         {
101             var sb = new StringBuilder();
102             foreach (var b in bytes)
103             {
104                 sb.AppendFormat("{0:x2}", b);
105             }
106             return sb.ToString();
107         }
108
109         public static uint GetTime()
110         {
111             return (uint)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
112         }
113     }
114 }