-
Notifications
You must be signed in to change notification settings - Fork 3
/
BetValidator.cs
80 lines (70 loc) · 3.65 KB
/
BetValidator.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
byte[] StringToBytes(string s)
{
return Enumerable
.Range(0, s.Length / 2)
.Select(x =>
{
// $"Converting Hex {s.Substring(x * 2, 2)} to Byte {byte.Parse(s.Substring(x * 2, 2), System.Globalization.NumberStyles.HexNumber)}".Dump();
return byte.Parse(s.Substring(x * 2, 2), System.Globalization.NumberStyles.HexNumber);
})
.ToArray();
}
string BytesToString(byte[] ba)
{
StringBuilder hex = new StringBuilder(ba.Length * 2);
foreach (byte b in ba)
hex.AppendFormat("{0:x2}", b);
return hex.ToString();
}
// serverSeedHash - Given to clientBytes before bet
// clientSeed - generated by clientBytes before bet
// serverSecretSeed - appears in validation page (secret of serverSecretSeedBytes)
// betNumber - always 0
// betResult - the roll. 981774 = 98.1174 = a roll of 98/100
bool validateBetResult(string serverSecretSeed, int clientSeed, int betNumber, long betResult, string serverSeedHash = null)
{
byte[] serverSecretSeedBytes = StringToBytes(serverSecretSeed);
$"serverSecretSeed: {serverSecretSeed}".Dump(); serverSecretSeedBytes.Dump();
byte[] clientBytes = BitConverter.GetBytes(clientSeed).Reverse().ToArray();
$"clientBytes: {clientSeed}".Dump(); clientBytes.Dump();
byte[] betNumberBytes = BitConverter.GetBytes(betNumber).Reverse().ToArray();
$"betNumberBytes: {betNumber}".Dump(); betNumberBytes.Dump();
byte[] serverSeedHashBytes = serverSeedHash == null ? null : StringToBytes(serverSeedHash);
$"serverSeedHash: {serverSeedHash}".Dump(); serverSeedHashBytes.Dump();
using (System.Security.Cryptography.SHA512 sha512 = new System.Security.Cryptography.SHA512Managed())
{
if (serverSeedHashBytes != null)
{
using (System.Security.Cryptography.SHA256 sha256 = new System.Security.Cryptography.SHA256Managed())
{
byte[] serverSecretSeedBytesHashed = sha256.ComputeHash(serverSecretSeedBytes);
$"serverSecretSeedBytesHashed: {BytesToString(serverSecretSeedBytesHashed)}".Dump(); serverSecretSeedBytesHashed.Dump();
$"serverSeedHash: {serverSeedHash}".Dump();
if (!serverSecretSeedBytesHashed.SequenceEqual(serverSeedHashBytes))
{
"Server seed hash does not match serverSecretSeedBytes seed".Dump();
throw new Exception("Server seed hash does not match serverSecretSeedBytes seed");
}
}
}
byte[] data = serverSecretSeedBytes.Concat(clientBytes).Concat(betNumberBytes).ToArray();
$"data: {BytesToString(data)}".Dump(); data.Dump();
byte[] hash = sha512.ComputeHash(sha512.ComputeHash(data));
$"hash: {BytesToString(hash)}".Dump(); hash.Dump();
while (true)
{
for (int x = 0; x <= 61; x += 3)
{
$"{hash[x]} << 16 | ({hash[x + 1]} << 8) | {hash[x + 2]} = {(hash[x] << 16)} | {(hash[x + 1] << 8)} | {hash[x + 2]} = {(hash[x] << 16) | (hash[x + 1] << 8) | hash[x + 2]}".Dump();
long result = (hash[x] << 16) | (hash[x + 1] << 8) | hash[x + 2];
$"{x}: {result}".Dump();
if (result < 16000000)
$"{result % 1000000} == {betResult}".Dump();
return result % 1000000 == betResult;
}
hash = sha512.ComputeHash(hash);
}
}
}
validateBetResult("67381d32d0cf9114b96cf4103cdd69b25c9fd2f0c6780dce650c735753181a80", 734819011, 0, 994102, "3330633c2106b4b6a5a480dc2eabf43618fa172e5ec81e38da44318466fb84d6")
.Dump();