添加项目文件。

This commit is contained in:
Zel
2025-01-22 23:31:03 +08:00
parent 1b8ba6771f
commit 2ae76476fb
894 changed files with 774558 additions and 0 deletions

View File

@@ -0,0 +1,73 @@
namespace TightWiki.Security
{
/// <summary>
/// Performs 32-bit reversed cyclic redundancy checks.
/// </summary>
public class Crc32
{
#region Constants
/// <summary>
/// Generator polynomial (modulo 2) for the reversed CRC32 algorithm.
/// </summary>
private const UInt32 s_generator = 0xEDB88320;
#endregion
#region Constructors
/// <summary>
/// Creates a new instance of the Crc32 class.
/// </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;
for (var j = 0; j < 8; ++j)
{
tableEntry = ((tableEntry & 1) != 0)
? (s_generator ^ (tableEntry >> 1))
: (tableEntry >> 1);
}
return tableEntry;
}).ToArray();
}
#endregion
#region Methods
/// <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>
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)));
}
catch (FormatException e)
{
throw new Exception("Could not read the stream out as bytes.", e);
}
catch (InvalidCastException e)
{
throw new Exception("Could not read the stream out as bytes.", e);
}
catch (OverflowException e)
{
throw new Exception("Could not read the stream out as bytes.", e);
}
}
#endregion
#region Fields
/// <summary>
/// Contains a cache of calculated checksum chunks.
/// </summary>
private readonly UInt32[] m_checksumTable;
#endregion
}
}

View File

@@ -0,0 +1,98 @@
using System.Security.Cryptography;
using System.Text;
namespace TightWiki.Security
{
public static class Helpers
{
private static string? _machineKey;
public static string MachineKey
=> _machineKey ??= Sha1(Environment.MachineName);
public static string GenerateRandomString(int maxLength = 10)
{
using var crypto = Aes.Create();
crypto.GenerateKey();
var result = Convert.ToBase64String(crypto.Key).Replace("/", "").Replace("=", "").Replace("+", "");
if (result.Length > maxLength)
{
result = result.Substring(0, maxLength);
}
return result.ToUpper();
}
public static int Crc32(string text)
=> (int)(new Crc32()).Get(Encoding.Unicode.GetBytes(text));
public static int Crc32(byte[] bytes)
=> (int)(new Crc32()).Get(bytes);
public static string Sha1(string text)
=> BitConverter.ToString(SHA1.HashData(Encoding.Unicode.GetBytes(text))).Replace("-", "");
public static string Sha256(string value)
{
var hash = new StringBuilder();
byte[] 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();
for (int i = 0; i < 16; i++)
{
iv[i] = vector[i];
}
aes.Key = keyBytes;
aes.IV = iv;
var encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
using var memoryStream = new MemoryStream();
using var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
using (var streamWriter = new StreamWriter(cryptoStream))
{
streamWriter.Write(plainText);
}
return Convert.ToBase64String(memoryStream.ToArray());
}
public static string DecryptString(string key, string cipherText)
{
byte[] 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();
for (int i = 0; i < 16; i++)
{
iv[i] = vector[i];
}
aes.Key = keyBytes;
aes.IV = iv;
var cryptoTransform = aes.CreateDecryptor(aes.Key, aes.IV);
using var memoryStream = new MemoryStream(buffer);
using var cryptoStream = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Read);
using var streamReader = new StreamReader(cryptoStream);
return streamReader.ReadToEnd();
}
}
}

View File

@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<Version>2.20.1</Version>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Release'">
<DebugSymbols>False</DebugSymbols>
<DebugType>None</DebugType>
</PropertyGroup>
</Project>