Skip to content

Commit af024bc

Browse files
committed
Added error handling for .NET 2.0 Subset parsing
1 parent 46de603 commit af024bc

File tree

1 file changed

+88
-4
lines changed

1 file changed

+88
-4
lines changed

MLAPI/NetworkingManagerComponents/Cryptography/DiffieHellman.cs

Lines changed: 88 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,100 @@
33
using IntXLib;
44
using System.Text;
55
using System.Security.Cryptography;
6+
using UnityEngine;
67

78
namespace MLAPI.Cryptography
89
{
910
internal class EllipticDiffieHellman
1011
{
1112
protected static readonly RNGCryptoServiceProvider rand = new RNGCryptoServiceProvider();
12-
public static readonly IntX DEFAULT_PRIME = (new IntX(1) << 255) - 19;
13-
public static readonly IntX DEFAULT_ORDER = (new IntX(1) << 252) + IntX.Parse("27742317777372353535851937790883648493");
14-
public static readonly EllipticCurve DEFAULT_CURVE = new EllipticCurve(486662, 1, DEFAULT_PRIME, EllipticCurve.CurveType.Montgomery);
15-
public static readonly CurvePoint DEFAULT_GENERATOR = new CurvePoint(9, IntX.Parse("14781619447589544791020593568409986887264606134616475288964881837755586237401"));
13+
14+
public static IntX DEFAULT_PRIME
15+
{
16+
get
17+
{
18+
if (defaultPrime == null)
19+
{
20+
try
21+
{
22+
defaultPrime = (new IntX(1) << 255) - 19;
23+
}
24+
catch (Exception e)
25+
{
26+
Debug.LogError("[MLAPI] CryptoLib failed to parse BigInt. If you are using .NET 2.0 Subset, switch to .NET 2.0 or .NET 4.5");
27+
}
28+
}
29+
30+
return defaultPrime;
31+
}
32+
}
33+
private static IntX defaultPrime;
34+
35+
36+
public static IntX DEFAULT_ORDER
37+
{
38+
get
39+
{
40+
if (defaultOrder == null)
41+
{
42+
try
43+
{
44+
defaultOrder = (new IntX(1) << 252) + IntX.Parse("27742317777372353535851937790883648493");
45+
}
46+
catch (Exception e)
47+
{
48+
Debug.LogError("[MLAPI] CryptoLib failed to parse BigInt. If you are using .NET 2.0 Subset, switch to .NET 2.0 or .NET 4.5");
49+
}
50+
}
51+
52+
return defaultOrder;
53+
}
54+
}
55+
56+
private static IntX defaultOrder;
57+
58+
59+
public static EllipticCurve DEFAULT_CURVE
60+
{
61+
get
62+
{
63+
if (defaultCurve == null)
64+
{
65+
try
66+
{
67+
defaultCurve = new EllipticCurve(486662, 1, DEFAULT_PRIME, EllipticCurve.CurveType.Montgomery);
68+
}
69+
catch (Exception e)
70+
{
71+
Debug.LogError("[MLAPI] CryptoLib failed to parse BigInt. If you are using .NET 2.0 Subset, switch to .NET 2.0 or .NET 4.5");
72+
}
73+
}
74+
75+
return defaultCurve;
76+
}
77+
}
78+
private static EllipticCurve defaultCurve;
79+
80+
public static CurvePoint DEFAULT_GENERATOR
81+
{
82+
get
83+
{
84+
if (defaultGenerator == null)
85+
{
86+
try
87+
{
88+
defaultGenerator = new CurvePoint(9, IntX.Parse("14781619447589544791020593568409986887264606134616475288964881837755586237401"));
89+
}
90+
catch (Exception e)
91+
{
92+
Debug.LogError("[MLAPI] CryptoLib failed to parse BigInt. If you are using .NET 2.0 Subset, switch to .NET 2.0 or .NET 4.5");
93+
}
94+
}
95+
96+
return defaultGenerator;
97+
}
98+
}
99+
private static CurvePoint defaultGenerator;
16100

17101
protected readonly EllipticCurve curve;
18102
public readonly IntX priv;

0 commit comments

Comments
 (0)