You've already forked lidgren-network-gen3
mirror of
https://github.com/lidgren/lidgren-network-gen3.git
synced 2026-05-15 22:56:30 +09:00
major update; gen 3.5
This commit is contained in:
@@ -75,7 +75,7 @@ namespace Lidgren.Network
|
||||
/// String to hash for key
|
||||
/// </summary>
|
||||
public NetXtea(string key)
|
||||
: this(NetSha.Hash(Encoding.ASCII.GetBytes(key)), 32)
|
||||
: this(SHA1.Create().ComputeHash(Encoding.ASCII.GetBytes(key)), 32)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -138,195 +138,4 @@ namespace Lidgren.Network
|
||||
destination[destinationOffset++] = (byte)value;
|
||||
}
|
||||
}
|
||||
|
||||
public static class NetSha
|
||||
{
|
||||
// TODO: switch to SHA256
|
||||
private static SHA1 m_sha;
|
||||
private static object s_lock = new object();
|
||||
|
||||
public static byte[] Hash(byte[] data)
|
||||
{
|
||||
lock (s_lock)
|
||||
{
|
||||
if (m_sha == null)
|
||||
m_sha = SHA1Managed.Create();
|
||||
return m_sha.ComputeHash(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class NetSRP
|
||||
{
|
||||
public static readonly BigInteger N = new BigInteger(NetUtility.ToByteArray("0115b8b692e0e045692cf280b436735c77a5a9e8a9e7ed56c965f87db5b2a2ece3"));
|
||||
public static readonly BigInteger g = new BigInteger((uint)2);
|
||||
public static readonly BigInteger k = ComputeMultiplier();
|
||||
|
||||
/// <summary>
|
||||
/// Compute multiplier (k)
|
||||
/// </summary>
|
||||
private static BigInteger ComputeMultiplier()
|
||||
{
|
||||
string one = NetUtility.ToHexString(N.GetBytes());
|
||||
string two = NetUtility.ToHexString(g.GetBytes());
|
||||
byte[] cc = NetUtility.ToByteArray(one + two.PadLeft(one.Length, '0'));
|
||||
BigInteger retval = BigInteger.Modulus(new BigInteger(NetSha.Hash(cc)), N);
|
||||
return retval;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a verifier that the server can use to authenticate users later on (v)
|
||||
/// </summary>
|
||||
public static void ComputePasswordVerifier(string username, string password, byte[] salt, out byte[] serverVerifier, out byte[] clientVerifier)
|
||||
{
|
||||
byte[] tmp = Encoding.ASCII.GetBytes(username + ":" + password);
|
||||
byte[] innerHash = NetSha.Hash(tmp);
|
||||
|
||||
byte[] total = new byte[innerHash.Length + salt.Length];
|
||||
Buffer.BlockCopy(salt, 0, total, 0, salt.Length);
|
||||
Buffer.BlockCopy(innerHash, 0, total, salt.Length, innerHash.Length);
|
||||
|
||||
clientVerifier = NetSha.Hash(total);
|
||||
|
||||
// Verifier (v) = g^x (mod N)
|
||||
BigInteger xx = new BigInteger(clientVerifier);
|
||||
serverVerifier = g.ModPow(xx, N).GetBytes();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get 256 random bits
|
||||
/// </summary>
|
||||
public static byte[] CreateRandomKey()
|
||||
{
|
||||
byte[] retval = new byte[32];
|
||||
NetRandom.Instance.NextBytes(retval);
|
||||
return retval;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets 80 random bits
|
||||
/// </summary>
|
||||
public static byte[] CreateRandomSalt()
|
||||
{
|
||||
byte[] retval = new byte[10];
|
||||
NetRandom.Instance.NextBytes(retval);
|
||||
return retval;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compute client challenge (A)
|
||||
/// </summary>
|
||||
public static byte[] ComputeClientPublicKey(byte[] clientPrivateKey) // a
|
||||
{
|
||||
BigInteger a = new BigInteger(clientPrivateKey);
|
||||
|
||||
BigInteger retval = g.ModPow(a, N);
|
||||
|
||||
string gs = NetUtility.ToHexString(g.GetBytes());
|
||||
|
||||
|
||||
Console.WriteLine("a: " + NetUtility.ToHexString(a.GetBytes()));
|
||||
Console.WriteLine("A: " + NetUtility.ToHexString(retval.GetBytes()));
|
||||
|
||||
return retval.GetBytes();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compute server challenge (B)
|
||||
/// </summary>
|
||||
public static byte[] ComputeServerPublicKey(byte[] serverPrivateKey, byte[] verifier) // b
|
||||
{
|
||||
BigInteger salt = new BigInteger(serverPrivateKey);
|
||||
|
||||
var bb = g.ModPow(salt, N);
|
||||
var B = BigInteger.Modulus((bb + (new BigInteger(verifier) * k)), N);
|
||||
|
||||
return B.GetBytes();
|
||||
}
|
||||
|
||||
public static byte[] ComputeU(byte[] clientPublicKey, byte[] serverPublicKey) // u
|
||||
{
|
||||
byte[] A = clientPublicKey;
|
||||
byte[] B = serverPublicKey;
|
||||
|
||||
string one = NetUtility.ToHexString(A);
|
||||
string two = NetUtility.ToHexString(B);
|
||||
string compound = one.PadLeft(66, '0') + two.PadLeft(66, '0');
|
||||
|
||||
byte[] cc = NetUtility.ToByteArray(compound);
|
||||
|
||||
return NetSha.Hash(cc);
|
||||
}
|
||||
|
||||
public static byte[] ComputeServerSessionKey(byte[] clientPublicKey, byte[] verifier, byte[] u, byte[] serverPrivateKey) // Ss
|
||||
{
|
||||
// S = (Av^u) ^ b (mod N)
|
||||
// return vv.modPow(uu, N).multiply(A).mod(N).modPow(bb, N);
|
||||
|
||||
BigInteger verBi = new BigInteger(verifier);
|
||||
BigInteger uBi = new BigInteger(u);
|
||||
BigInteger ABi = new BigInteger(clientPublicKey); // A
|
||||
BigInteger bBi = new BigInteger(serverPrivateKey); // b
|
||||
|
||||
Console.WriteLine("Ss input v: " + NetUtility.ToHexString(verifier));
|
||||
Console.WriteLine("Ss input u: " + NetUtility.ToHexString(u));
|
||||
Console.WriteLine("Ss input A: " + NetUtility.ToHexString(clientPublicKey));
|
||||
Console.WriteLine("Ss input A: " + ABi.ToString(16));
|
||||
Console.WriteLine("Ss input b: " + NetUtility.ToHexString(serverPrivateKey));
|
||||
|
||||
BigInteger retval = verBi.ModPow(uBi, N).Multiply(ABi).Modulus(N).ModPow(bBi, N).Modulus(N);
|
||||
Console.WriteLine("Ss (trad): " + NetUtility.ToHexString(retval.GetBytes()));
|
||||
BigInteger f1 = verBi.ModPow(uBi, N);
|
||||
Console.WriteLine("f1 (trad): " + NetUtility.ToHexString(f1.GetBytes()));
|
||||
|
||||
//return retval.GetBytes();
|
||||
|
||||
|
||||
// own
|
||||
// BigInteger tmp1 = verBi.ModPow(uBi, N).ModPow(bBi, N).Modulus(N);
|
||||
BigInteger tmp1 = (ABi * verBi.ModPow(uBi, N)).ModPow(bBi, N);
|
||||
Console.WriteLine("Ss (own): " + NetUtility.ToHexString(tmp1.GetBytes()));
|
||||
|
||||
|
||||
|
||||
// bc
|
||||
BigIntegerBC verBi2 = new BigIntegerBC(verifier);
|
||||
BigIntegerBC ABi2 = new BigIntegerBC(clientPublicKey); // A
|
||||
BigIntegerBC uBi2 = new BigIntegerBC(u);
|
||||
BigIntegerBC bBi2 = new BigIntegerBC(serverPrivateKey);
|
||||
BigIntegerBC N2 = new BigIntegerBC(N.GetBytes());
|
||||
|
||||
BigIntegerBC retval2 = verBi2.ModPow(uBi2, N2).Multiply(ABi2).Modulus(N2).ModPow(bBi2, N2).Modulus(N2);
|
||||
Console.WriteLine("Ss (bc): " + NetUtility.ToHexString(retval2.ToByteArray()));
|
||||
BigIntegerBC f12 = verBi2.ModPow(uBi2, N2);
|
||||
Console.WriteLine("f1 (bc): " + NetUtility.ToHexString(f12.ToByteArray()));
|
||||
|
||||
|
||||
// own bc
|
||||
BigIntegerBC tmp2 = verBi2.ModPow(uBi2, N2).ModPow(bBi2, N2).Modulus(N2);
|
||||
Console.WriteLine("Ss (ownBC): " + NetUtility.ToHexString(tmp2.ToByteArray()));
|
||||
|
||||
|
||||
|
||||
return retval.GetBytes();
|
||||
|
||||
//return NetSha.Hash(retval.GetBytes());
|
||||
}
|
||||
|
||||
public static byte[] ComputeClientSessionKey(byte[] serverPublicKey, byte[] x, byte[] u, byte[] clientPrivateKey) // Sc
|
||||
{
|
||||
BigInteger xBi = new BigInteger(x);
|
||||
BigInteger BBi = new BigInteger(serverPublicKey); // B
|
||||
BigInteger uBi = new BigInteger(u);
|
||||
BigInteger aBi = new BigInteger(clientPrivateKey); // a
|
||||
|
||||
BigInteger retval = (BBi + (N - ((k * g.ModPow(xBi, N)) % N))).ModPow(aBi + uBi * xBi, N);
|
||||
|
||||
return retval.GetBytes();
|
||||
|
||||
//return NetSha.Hash(retval.GetBytes());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user