Fix all warnings & Trim (#2226)

* Fix AutoFishing crash
* Fix all warnings
* Remove DotNetZip.
* Fix the usage of HttpClient.
This commit is contained in:
BruceChen 2022-10-02 18:31:08 +08:00 committed by GitHub
parent 4aa6c1c99f
commit 1d52d1eadd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
227 changed files with 2201 additions and 43564 deletions

View file

@ -1,12 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.IO;
using System.Collections.Concurrent;
using System.IO;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Threading.Tasks;
namespace MinecraftClient.Crypto
{
@ -19,8 +16,8 @@ namespace MinecraftClient.Crypto
private bool inStreamEnded = false;
private byte[] ReadStreamIV = new byte[16];
private byte[] WriteStreamIV = new byte[16];
private readonly byte[] ReadStreamIV = new byte[16];
private readonly byte[] WriteStreamIV = new byte[16];
public Stream BaseStream { get; set; }

View file

@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Text;
namespace MinecraftClient.Crypto
{
@ -13,7 +11,7 @@ namespace MinecraftClient.Crypto
public static class CryptoHandler
{
public static byte[]? ClientAESPrivateKey = null;
public static byte[]? ClientAESPrivateKey;
/// <summary>
/// Get a cryptographic service for encrypting data using the server's RSA public key
@ -21,21 +19,21 @@ namespace MinecraftClient.Crypto
/// <param name="x509key">Byte array containing the encoded key</param>
/// <returns>Returns the corresponding RSA Crypto Service</returns>
public static RSACryptoServiceProvider DecodeRSAPublicKey(byte[] x509key)
public static RSACryptoServiceProvider? DecodeRSAPublicKey(byte[] x509key)
{
/* Code from StackOverflow no. 18091460 */
byte[] SeqOID = { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01 };
System.IO.MemoryStream ms = new System.IO.MemoryStream(x509key);
System.IO.BinaryReader reader = new System.IO.BinaryReader(ms);
System.IO.MemoryStream ms = new(x509key);
System.IO.BinaryReader reader = new(ms);
if (reader.ReadByte() == 0x30)
ReadASNLength(reader); //skip the size
else
return null;
int identifierSize = 0; //total length of Object Identifier section
int identifierSize; //total length of Object Identifier section
if (reader.ReadByte() == 0x30)
identifierSize = ReadASNLength(reader);
else
@ -78,10 +76,12 @@ namespace MinecraftClient.Crypto
byte[] exponent = new byte[exponentSize];
reader.Read(exponent, 0, exponent.Length);
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSAParameters RSAKeyInfo = new RSAParameters();
RSAKeyInfo.Modulus = modulus;
RSAKeyInfo.Exponent = exponent;
RSACryptoServiceProvider RSA = new();
RSAParameters RSAKeyInfo = new()
{
Modulus = modulus,
Exponent = exponent
};
RSA.ImportParameters(RSAKeyInfo);
return RSA;
}
@ -120,8 +120,9 @@ namespace MinecraftClient.Crypto
public static byte[] GenerateAESPrivateKey()
{
AesManaged AES = new AesManaged();
AES.KeySize = 128; AES.GenerateKey();
Aes AES = Aes.Create();
AES.KeySize = 128;
AES.GenerateKey();
return AES.Key;
}
@ -133,9 +134,9 @@ namespace MinecraftClient.Crypto
/// <param name="SecretKey">Secret key chosen by the client</param>
/// <returns>Returns the corresponding SHA-1 hex hash</returns>
public static string getServerHash(string serverID, byte[] PublicKey, byte[] SecretKey)
public static string GetServerHash(string serverID, byte[] PublicKey, byte[] SecretKey)
{
byte[] hash = digest(new byte[][] { Encoding.GetEncoding("iso-8859-1").GetBytes(serverID), SecretKey, PublicKey });
byte[] hash = Digest(new byte[][] { Encoding.GetEncoding("iso-8859-1").GetBytes(serverID), SecretKey, PublicKey });
bool negative = (hash[0] & 0x80) == 0x80;
if (negative) { hash = TwosComplementLittleEndian(hash); }
string result = GetHexString(hash).TrimStart('0');
@ -149,13 +150,13 @@ namespace MinecraftClient.Crypto
/// <param name="tohash">array of byte arrays to hash</param>
/// <returns>Returns the hashed data</returns>
private static byte[] digest(byte[][] tohash)
private static byte[] Digest(byte[][] tohash)
{
SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
SHA1 sha1 = SHA1.Create();
for (int i = 0; i < tohash.Length; i++)
sha1.TransformBlock(tohash[i], 0, tohash[i].Length, tohash[i], 0);
sha1.TransformFinalBlock(new byte[] { }, 0, 0);
return sha1.Hash;
sha1.TransformFinalBlock(Array.Empty<byte>(), 0, 0);
return sha1.Hash!;
}
/// <summary>