123123
This commit is contained in:
@@ -1,24 +1,26 @@
|
||||
namespace ZelWiki.Security
|
||||
{
|
||||
/// <summary>
|
||||
/// Performs 32-bit reversed cyclic redundancy checks.
|
||||
///
|
||||
/// </summary>
|
||||
public class Crc32
|
||||
{
|
||||
#region Constants
|
||||
#region
|
||||
|
||||
/// <summary>
|
||||
/// Generator polynomial (modulo 2) for the reversed CRC32 algorithm.
|
||||
/// 逆CRC32算法的生成多项式(模2)
|
||||
/// </summary>
|
||||
private const UInt32 s_generator = 0xEDB88320;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
#region
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of the Crc32 class.
|
||||
/// 创建Crc32类的新实例
|
||||
/// </summary>
|
||||
public Crc32()
|
||||
{
|
||||
// Constructs the checksum lookup table. Used to optimize the checksum.
|
||||
m_checksumTable = Enumerable.Range(0, 256).Select(i =>
|
||||
{
|
||||
var tableEntry = (uint)i;
|
||||
@@ -28,46 +30,53 @@
|
||||
? (s_generator ^ (tableEntry >> 1))
|
||||
: (tableEntry >> 1);
|
||||
}
|
||||
|
||||
return tableEntry;
|
||||
}).ToArray();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
#region
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the checksum of the byte stream.
|
||||
/// 计算字节流的校验和
|
||||
/// </summary>
|
||||
/// <param name="byteStream">The byte stream to calculate the checksum for.</param>
|
||||
/// <returns>A 32-bit reversed checksum.</returns>
|
||||
/// <param name="byteStream"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public UInt32 Get<T>(IEnumerable<T> byteStream)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Initialize checksumRegister to 0xFFFFFFFF and calculate the checksum.
|
||||
return ~byteStream.Aggregate(0xFFFFFFFF, (checksumRegister, currentByte) =>
|
||||
(m_checksumTable[(checksumRegister & 0xFF) ^ Convert.ToByte(currentByte)] ^ (checksumRegister >> 8)));
|
||||
(m_checksumTable[(checksumRegister & 0xFF) ^ Convert.ToByte(currentByte)] ^
|
||||
(checksumRegister >> 8)));
|
||||
}
|
||||
catch (FormatException e)
|
||||
{
|
||||
throw new Exception("Could not read the stream out as bytes.", e);
|
||||
throw new Exception("无法以字节形式读取流", e);
|
||||
}
|
||||
catch (InvalidCastException e)
|
||||
{
|
||||
throw new Exception("Could not read the stream out as bytes.", e);
|
||||
throw new Exception("无法以字节形式读取流", e);
|
||||
}
|
||||
catch (OverflowException e)
|
||||
{
|
||||
throw new Exception("Could not read the stream out as bytes.", e);
|
||||
throw new Exception("无法以字节形式读取流", e);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Fields
|
||||
#region
|
||||
|
||||
/// <summary>
|
||||
/// Contains a cache of calculated checksum chunks.
|
||||
/// 包含计算校验和块的缓存
|
||||
/// </summary>
|
||||
private readonly UInt32[] m_checksumTable;
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ namespace ZelWiki.Security
|
||||
public static class Helpers
|
||||
{
|
||||
private static string? _machineKey;
|
||||
|
||||
public static string MachineKey
|
||||
=> _machineKey ??= Sha1(Environment.MachineName);
|
||||
|
||||
@@ -35,22 +36,23 @@ namespace ZelWiki.Security
|
||||
public static string Sha256(string value)
|
||||
{
|
||||
var hash = new StringBuilder();
|
||||
byte[] crypto = SHA256.HashData(Encoding.UTF8.GetBytes(value));
|
||||
var crypto = SHA256.HashData(Encoding.UTF8.GetBytes(value));
|
||||
foreach (byte theByte in crypto)
|
||||
{
|
||||
hash.Append(theByte.ToString("x2"));
|
||||
}
|
||||
|
||||
return hash.ToString();
|
||||
}
|
||||
|
||||
public static string EncryptString(string key, string plainText)
|
||||
{
|
||||
using var aes = Aes.Create();
|
||||
byte[] iv = new byte[16];
|
||||
byte[] keyBytes = SHA256.HashData(Encoding.Unicode.GetBytes(key));
|
||||
byte[] vector = (byte[])keyBytes.Clone();
|
||||
var iv = new byte[16];
|
||||
var keyBytes = SHA256.HashData(Encoding.Unicode.GetBytes(key));
|
||||
var vector = (byte[])keyBytes.Clone();
|
||||
|
||||
for (int i = 0; i < 16; i++)
|
||||
for (var i = 0; i < 16; i++)
|
||||
{
|
||||
iv[i] = vector[i];
|
||||
}
|
||||
@@ -72,14 +74,14 @@ namespace ZelWiki.Security
|
||||
|
||||
public static string DecryptString(string key, string cipherText)
|
||||
{
|
||||
byte[] buffer = Convert.FromBase64String(cipherText);
|
||||
var buffer = Convert.FromBase64String(cipherText);
|
||||
|
||||
using var aes = Aes.Create();
|
||||
byte[] iv = new byte[16];
|
||||
byte[] keyBytes = SHA256.HashData(Encoding.Unicode.GetBytes(key));
|
||||
byte[] vector = (byte[])keyBytes.Clone();
|
||||
var iv = new byte[16];
|
||||
var keyBytes = SHA256.HashData(Encoding.Unicode.GetBytes(key));
|
||||
var vector = (byte[])keyBytes.Clone();
|
||||
|
||||
for (int i = 0; i < 16; i++)
|
||||
for (var i = 0; i < 16; i++)
|
||||
{
|
||||
iv[i] = vector[i];
|
||||
}
|
||||
@@ -95,4 +97,4 @@ namespace ZelWiki.Security
|
||||
return streamReader.ReadToEnd();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user