-
Notifications
You must be signed in to change notification settings - Fork 12
/
Utils.cs
80 lines (69 loc) · 2.37 KB
/
Utils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CentralMine.NET
{
static class Utils
{
public static string UIntToHexString(uint val)
{
byte[] vbytes = new byte[4];
vbytes[0] = (byte)(val >> 24);
vbytes[1] = (byte)(val >> 16);
vbytes[2] = (byte)(val >> 8);
vbytes[3] = (byte)val;
return ByteArrayToHexString(vbytes);
}
public static uint HexStringToUInt(string hex)
{
byte[] h = HexStringToByteArray(hex);
uint val = (uint)((h[0] << 24) | (h[1] << 16) | (h[2] << 8) | h[3]);
return val;
}
public static byte[] HexStringToByteArray(string hex)
{
if (hex.Length % 2 == 1)
throw new Exception("The binary key cannot have an odd number of digits");
byte[] arr = new byte[hex.Length >> 1];
for (int i = 0; i < hex.Length >> 1; ++i)
{
arr[i] = (byte)((GetHexVal(hex[i << 1]) << 4) + (GetHexVal(hex[(i << 1) + 1])));
}
return arr;
}
static int GetHexVal(char hex)
{
int val = (int)hex;
//For uppercase A-F letters:
//return val - (val < 58 ? 48 : 55);
//For lowercase a-f letters:
//return val - (val < 58 ? 48 : 87);
//Or the two combined, but a bit slower:
return val - (val < 58 ? 48 : (val < 97 ? 55 : 87));
}
public static string ByteArrayToHexString(byte[] barr)
{
string str = "";
foreach (byte b in barr)
{
str += string.Format("{0:X2}", b);
}
return str;
}
public static void ByteSwapIntegers(byte[] buffer)
{
uint[] ints = new uint[16];
Buffer.BlockCopy(buffer, 0, ints, 0, 32);
for (int i = 0; i < ints.Length; i++)
ints[i] = MinerLib_cs.Scrypt.ByteReverse(ints[i]);
Buffer.BlockCopy(ints, 0, buffer, 0, 32);
}
public static UInt64 UnixTime()
{
TimeSpan span = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc));
double unixTime = span.TotalSeconds;
return (UInt64)unixTime;
}
}
}