mirror of
https://github.com/Leonmmcoset/CMLeonOS.git
synced 2026-03-03 15:30:27 +00:00
整理代码1
This commit is contained in:
163
utils/Base64.cs
Normal file
163
utils/Base64.cs
Normal file
@@ -0,0 +1,163 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace CMLeonOS
|
||||
{
|
||||
/// <summary>
|
||||
/// Cosmos裸机专属Base64编码/解码工具类
|
||||
/// 无.NET原生Convert依赖,纯手写实现,适配Cosmos System2
|
||||
/// </summary>
|
||||
public static class Base64Helper
|
||||
{
|
||||
// Base64标准编码表(0-63对应),裸机环境直接硬编码,无需动态生成
|
||||
private const string Base64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
/// <summary>
|
||||
/// Base64编码(加密):将字符串转为Base64编码串(默认UTF8编码)
|
||||
/// </summary>
|
||||
/// <param name="input">原始字符串</param>
|
||||
/// <returns>Base64编码后的字符串</returns>
|
||||
public static string Encode(string input)
|
||||
{
|
||||
// 空值校验,裸机环境需严格判空
|
||||
if (string.IsNullOrEmpty(input))
|
||||
return string.Empty;
|
||||
|
||||
// 将字符串转为UTF8字节数组(Cosmos支持基础UTF8/ASCII)
|
||||
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
|
||||
// 调用字节数组的编码核心方法
|
||||
return EncodeBytes(inputBytes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base64编码(核心):将二进制字节数组转为Base64编码串
|
||||
/// 适配任意二进制数据(文件、网络流、字符串字节)
|
||||
/// </summary>
|
||||
/// <param name="inputBytes">原始二进制字节数组</param>
|
||||
/// <returns>Base64编码后的字符串</returns>
|
||||
public static string EncodeBytes(byte[] inputBytes)
|
||||
{
|
||||
if (inputBytes == null || inputBytes.Length == 0)
|
||||
return string.Empty;
|
||||
|
||||
// 构建结果字符串,裸机用StringBuilder更高效
|
||||
StringBuilder result = new StringBuilder();
|
||||
int inputLength = inputBytes.Length;
|
||||
// 按3字节为一组遍历,处理所有完整组
|
||||
for (int i = 0; i < inputLength; i += 3)
|
||||
{
|
||||
// 取3个字节,不足的补0(位运算用,不修改原数组)
|
||||
byte b1 = i < inputLength ? inputBytes[i] : (byte)0;
|
||||
byte b2 = i + 1 < inputLength ? inputBytes[i + 1] : (byte)0;
|
||||
byte b3 = i + 2 < inputLength ? inputBytes[i + 2] : (byte)0;
|
||||
|
||||
// 核心位运算:3字节(24位)拆分为4个6位
|
||||
// 第一个6位:b1的高6位(右移2位,与0x3F过滤低2位)
|
||||
int idx1 = (b1 >> 2) & 0x3F;
|
||||
// 第二个6位:b1的低2位 + b2的高4位(左移4位 + b2右移4位,与0x3F过滤)
|
||||
int idx2 = ((b1 & 0x03) << 4) | ((b2 >> 4) & 0x0F);
|
||||
// 第三个6位:b2的低4位 + b3的高2位(左移2位 + b3右移6位,与0x3F过滤)
|
||||
int idx3 = ((b2 & 0x0F) << 2) | ((b3 >> 6) & 0x03);
|
||||
// 第四个6位:b3的低6位(与0x3F过滤高2位)
|
||||
int idx4 = b3 & 0x3F;
|
||||
|
||||
// 根据索引取Base64字符,添加到结果
|
||||
result.Append(Base64Table[idx1]);
|
||||
result.Append(Base64Table[idx2]);
|
||||
|
||||
// 补位处理:不足3字节时,用=替代
|
||||
result.Append(i + 1 < inputLength ? Base64Table[idx3] : '=');
|
||||
result.Append(i + 2 < inputLength ? Base64Table[idx4] : '=');
|
||||
}
|
||||
|
||||
return result.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base64解码(解密):将Base64编码串转回原始字符串(默认UTF8编码)
|
||||
/// </summary>
|
||||
/// <param name="input">Base64编码串</param>
|
||||
/// <returns>解码后的原始字符串</returns>
|
||||
/// <exception cref="ArgumentException">Base64格式错误时抛出</exception>
|
||||
public static string Decode(string input)
|
||||
{
|
||||
if (string.IsNullOrEmpty(input))
|
||||
return string.Empty;
|
||||
|
||||
// 解码为字节数组,再转为UTF8字符串
|
||||
byte[] outputBytes = DecodeToBytes(input);
|
||||
return Encoding.UTF8.GetString(outputBytes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base64解码(核心):将Base64编码串转回原始二进制字节数组
|
||||
/// 适配任意Base64编码的二进制数据
|
||||
/// </summary>
|
||||
/// <param name="input">Base64编码串</param>
|
||||
/// <returns>解码后的二进制字节数组</returns>
|
||||
/// <exception cref="ArgumentException">Base64格式错误时抛出</exception>
|
||||
public static byte[] DecodeToBytes(string input)
|
||||
{
|
||||
if (string.IsNullOrEmpty(input))
|
||||
return Array.Empty<byte>();
|
||||
|
||||
// 预处理:过滤所有非Base64有效字符(仅保留编码表字符和=)
|
||||
StringBuilder cleanInput = new StringBuilder();
|
||||
foreach (char c in input)
|
||||
{
|
||||
if (Base64Table.Contains(c) || c == '=')
|
||||
cleanInput.Append(c);
|
||||
}
|
||||
string base64 = cleanInput.ToString();
|
||||
int inputLength = base64.Length;
|
||||
|
||||
// 基础格式校验:Base64长度必须是4的倍数
|
||||
if (inputLength % 4 != 0)
|
||||
throw new ArgumentException("Invalid Base64 string: Length is not a multiple of 4");
|
||||
|
||||
// 计算补位符数量(=的个数,只能是0/1/2)
|
||||
int padCount = 0;
|
||||
if (base64[inputLength - 1] == '=') padCount++;
|
||||
if (base64[inputLength - 2] == '=') padCount++;
|
||||
|
||||
// 计算解码后的字节数:(4*分组数 - 补位符数) / 3
|
||||
int outputLength = (inputLength * 6) / 8 - padCount;
|
||||
byte[] outputBytes = new byte[outputLength];
|
||||
int outputIndex = 0;
|
||||
|
||||
// 按4个字符为一组遍历,处理所有组
|
||||
for (int i = 0; i < inputLength; i += 4)
|
||||
{
|
||||
// 取4个Base64字符,转换为对应的6位索引(0-63)
|
||||
int idx1 = Base64Table.IndexOf(base64[i]);
|
||||
int idx2 = Base64Table.IndexOf(base64[i + 1]);
|
||||
// 补位的=索引为-1,转为0处理
|
||||
int idx3 = i + 2 < inputLength ? Base64Table.IndexOf(base64[i + 2]) : 0;
|
||||
int idx4 = i + 3 < inputLength ? Base64Table.IndexOf(base64[i + 3]) : 0;
|
||||
|
||||
// 基础校验:无效字符(索引为-1且非=)
|
||||
if (idx1 == -1 || idx2 == -1 || (idx3 == -1 && base64[i+2] != '=') || (idx4 == -1 && base64[i+3] != '='))
|
||||
throw new ArgumentException("Invalid Base64 string: Contains invalid characters");
|
||||
|
||||
// 核心位运算:4个6位拼接为24位,拆分为3个字节
|
||||
uint combined = (uint)((idx1 << 18) | (idx2 << 12) | (idx3 << 6) | idx4);
|
||||
// 第一个字节:24位的高8位
|
||||
byte b1 = (byte)((combined >> 16) & 0xFF);
|
||||
// 第二个字节:24位的中间8位
|
||||
byte b2 = (byte)((combined >> 8) & 0xFF);
|
||||
// 第三个字节:24位的低8位
|
||||
byte b3 = (byte)(combined & 0xFF);
|
||||
|
||||
// 将字节写入结果数组,根据补位符数量跳过多余字节
|
||||
if (outputIndex < outputLength)
|
||||
outputBytes[outputIndex++] = b1;
|
||||
if (outputIndex < outputLength)
|
||||
outputBytes[outputIndex++] = b2;
|
||||
if (outputIndex < outputLength)
|
||||
outputBytes[outputIndex++] = b3;
|
||||
}
|
||||
|
||||
return outputBytes;
|
||||
}
|
||||
}
|
||||
}
|
||||
172
utils/EnvironmentVariableManager.cs
Normal file
172
utils/EnvironmentVariableManager.cs
Normal file
@@ -0,0 +1,172 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace CMLeonOS
|
||||
{
|
||||
public class EnvironmentVariableManager
|
||||
{
|
||||
private static EnvironmentVariableManager instance;
|
||||
private string envFilePath = @"0:\system\env.dat";
|
||||
private Dictionary<string, string> environmentVariables;
|
||||
|
||||
private EnvironmentVariableManager()
|
||||
{
|
||||
environmentVariables = new Dictionary<string, string>();
|
||||
LoadEnvironmentVariables();
|
||||
}
|
||||
|
||||
public static EnvironmentVariableManager Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (instance == null)
|
||||
{
|
||||
instance = new EnvironmentVariableManager();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadEnvironmentVariables()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (File.Exists(envFilePath))
|
||||
{
|
||||
string[] lines = File.ReadAllLines(envFilePath);
|
||||
foreach (string line in lines)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(line) && line.Contains("="))
|
||||
{
|
||||
int equalIndex = line.IndexOf('=');
|
||||
string varName = line.Substring(0, equalIndex).Trim();
|
||||
string varValue = line.Substring(equalIndex + 1).Trim();
|
||||
environmentVariables[varName] = varValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error loading environment variables: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private void SaveEnvironmentVariables()
|
||||
{
|
||||
try
|
||||
{
|
||||
Console.WriteLine($"Saving environment variables to: {envFilePath}");
|
||||
Console.WriteLine($"Variables to save: {environmentVariables.Count}");
|
||||
|
||||
// 构建文件内容
|
||||
string content = "";
|
||||
foreach (var kvp in environmentVariables)
|
||||
{
|
||||
content += $"{kvp.Key}={kvp.Value}\n";
|
||||
}
|
||||
|
||||
Console.WriteLine("Environment variables content:");
|
||||
Console.WriteLine(content);
|
||||
|
||||
// 使用FileSystem的WriteFile方法来确保在Cosmos中正常工作
|
||||
// 注意:这里需要访问FileSystem实例,但EnvironmentVariableManager是独立的
|
||||
// 所以我们使用File.WriteAllText,但添加重试机制
|
||||
|
||||
int retryCount = 0;
|
||||
bool success = false;
|
||||
|
||||
while (retryCount < 3 && !success)
|
||||
{
|
||||
try
|
||||
{
|
||||
File.WriteAllText(envFilePath, content);
|
||||
success = true;
|
||||
Console.WriteLine("Environment variables saved successfully.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
retryCount++;
|
||||
Console.WriteLine($"Save attempt {retryCount} failed: {ex.Message}");
|
||||
// Thread.Sleep(1000); // 等待1秒后重试
|
||||
}
|
||||
}
|
||||
|
||||
if (!success)
|
||||
{
|
||||
Console.WriteLine("Failed to save environment variables after 3 attempts.");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error saving environment variables: {ex.Message}");
|
||||
Console.WriteLine($"Exception type: {ex.GetType().Name}");
|
||||
}
|
||||
}
|
||||
|
||||
public bool SetVariable(string varName, string varValue)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(varName))
|
||||
{
|
||||
Console.WriteLine("Error: Variable name cannot be empty");
|
||||
return false;
|
||||
}
|
||||
|
||||
environmentVariables[varName] = varValue;
|
||||
SaveEnvironmentVariables();
|
||||
Console.WriteLine($"Environment variable '{varName}' set to '{varValue}'");
|
||||
return true;
|
||||
}
|
||||
|
||||
public string GetVariable(string varName)
|
||||
{
|
||||
if (environmentVariables.ContainsKey(varName))
|
||||
{
|
||||
return environmentVariables[varName];
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Error: Environment variable '{varName}' not found");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void ListVariables()
|
||||
{
|
||||
if (environmentVariables.Count == 0)
|
||||
{
|
||||
Console.WriteLine("No environment variables set.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("====================================");
|
||||
Console.WriteLine(" Environment Variables");
|
||||
Console.WriteLine("====================================");
|
||||
Console.WriteLine();
|
||||
foreach (var kvp in environmentVariables)
|
||||
{
|
||||
Console.WriteLine($" {kvp.Key}={kvp.Value}");
|
||||
}
|
||||
Console.WriteLine();
|
||||
Console.WriteLine($"Total: {environmentVariables.Count} variables");
|
||||
}
|
||||
}
|
||||
|
||||
public bool DeleteVariable(string varName)
|
||||
{
|
||||
if (environmentVariables.ContainsKey(varName))
|
||||
{
|
||||
environmentVariables.Remove(varName);
|
||||
SaveEnvironmentVariables();
|
||||
Console.WriteLine($"Environment variable '{varName}' deleted");
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Error: Environment variable '{varName}' not found");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
127
utils/NetworkConfigManager.cs
Normal file
127
utils/NetworkConfigManager.cs
Normal file
@@ -0,0 +1,127 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace CMLeonOS
|
||||
{
|
||||
public class NetworkConfigManager
|
||||
{
|
||||
private static NetworkConfigManager instance;
|
||||
private string configFilePath = @"0:\system\network.dat";
|
||||
|
||||
private string dns = "1.1.1.1";
|
||||
private string gateway = "192.168.0.1";
|
||||
|
||||
private NetworkConfigManager()
|
||||
{
|
||||
LoadConfig();
|
||||
}
|
||||
|
||||
public static NetworkConfigManager Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (instance == null)
|
||||
{
|
||||
instance = new NetworkConfigManager();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadConfig()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (File.Exists(configFilePath))
|
||||
{
|
||||
string[] lines = File.ReadAllLines(configFilePath);
|
||||
|
||||
foreach (string line in lines)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(line) || line.StartsWith("#"))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
string[] parts = line.Split('=');
|
||||
if (parts.Length == 2)
|
||||
{
|
||||
string key = parts[0].Trim().ToLower();
|
||||
string value = parts[1].Trim();
|
||||
|
||||
if (key == "dns")
|
||||
{
|
||||
dns = value;
|
||||
}
|
||||
else if (key == "gateway")
|
||||
{
|
||||
gateway = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error loading network config: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private void SaveConfig()
|
||||
{
|
||||
try
|
||||
{
|
||||
string directory = Path.GetDirectoryName(configFilePath);
|
||||
if (!Directory.Exists(directory))
|
||||
{
|
||||
Directory.CreateDirectory(directory);
|
||||
}
|
||||
|
||||
using (StreamWriter writer = new StreamWriter(configFilePath))
|
||||
{
|
||||
writer.WriteLine("# CMLeonOS Network Configuration");
|
||||
writer.WriteLine($"# Generated on {DateTime.Now}");
|
||||
writer.WriteLine();
|
||||
writer.WriteLine($"DNS={dns}");
|
||||
writer.WriteLine($"Gateway={gateway}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error saving network config: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public string GetDNS()
|
||||
{
|
||||
return dns;
|
||||
}
|
||||
|
||||
public string GetGateway()
|
||||
{
|
||||
return gateway;
|
||||
}
|
||||
|
||||
public void SetDNS(string dnsIp)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(dnsIp))
|
||||
{
|
||||
throw new ArgumentException("DNS address cannot be empty");
|
||||
}
|
||||
|
||||
dns = dnsIp;
|
||||
SaveConfig();
|
||||
}
|
||||
|
||||
public void SetGateway(string gatewayIp)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(gatewayIp))
|
||||
{
|
||||
throw new ArgumentException("Gateway address cannot be empty");
|
||||
}
|
||||
|
||||
gateway = gatewayIp;
|
||||
SaveConfig();
|
||||
}
|
||||
}
|
||||
}
|
||||
226
utils/SHA256.cs
Normal file
226
utils/SHA256.cs
Normal file
@@ -0,0 +1,226 @@
|
||||
using System;
|
||||
|
||||
namespace CMLeonOS
|
||||
{
|
||||
public class Sha256
|
||||
{
|
||||
private static readonly uint[] K = new uint[64] {
|
||||
0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
|
||||
0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3, 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
|
||||
0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC, 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
|
||||
0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
|
||||
0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13, 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
|
||||
0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3, 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
|
||||
0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
|
||||
0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208, 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2
|
||||
};
|
||||
|
||||
private static uint ROTL(uint x, byte n)
|
||||
{
|
||||
if (n >= 32) throw new ArgumentException("n");
|
||||
return x << n | x >> 32 - n;
|
||||
}
|
||||
|
||||
private static uint ROTR(uint x, byte n)
|
||||
{
|
||||
if (n >= 32) throw new ArgumentException("n");
|
||||
return x >> n | x << 32 - n;
|
||||
}
|
||||
|
||||
private static uint Ch(uint x, uint y, uint z)
|
||||
{
|
||||
return x & y ^ ~x & z;
|
||||
}
|
||||
|
||||
private static uint Maj(uint x, uint y, uint z)
|
||||
{
|
||||
return x & y ^ x & z ^ y & z;
|
||||
}
|
||||
|
||||
private static uint Sigma0(uint x)
|
||||
{
|
||||
return ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22);
|
||||
}
|
||||
|
||||
private static uint Sigma1(uint x)
|
||||
{
|
||||
return ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25);
|
||||
}
|
||||
|
||||
private static uint sigma0(uint x)
|
||||
{
|
||||
return ROTR(x, 7) ^ ROTR(x, 18) ^ x >> 3;
|
||||
}
|
||||
|
||||
private static uint sigma1(uint x)
|
||||
{
|
||||
return ROTR(x, 17) ^ ROTR(x, 19) ^ x >> 10;
|
||||
}
|
||||
|
||||
|
||||
private uint[] H = new uint[8] {
|
||||
0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19
|
||||
};
|
||||
|
||||
private byte[] pendingBlock = new byte[64];
|
||||
private uint pendingBlockOff = 0;
|
||||
private uint[] uintBuffer = new uint[16];
|
||||
|
||||
private ulong bitsProcessed = 0;
|
||||
|
||||
private bool closed = false;
|
||||
|
||||
private void ProcessBlock(uint[] M)
|
||||
{
|
||||
if (M.Length != 16) throw new ArgumentException("M");
|
||||
|
||||
// 1. Prepare the message schedule (W[t]):
|
||||
uint[] W = new uint[64];
|
||||
for (int t = 0; t < 16; ++t)
|
||||
{
|
||||
W[t] = M[t];
|
||||
}
|
||||
|
||||
for (int t = 16; t < 64; ++t)
|
||||
{
|
||||
W[t] = sigma1(W[t - 2]) + W[t - 7] + sigma0(W[t - 15]) + W[t - 16];
|
||||
}
|
||||
|
||||
// 2. Initialize the eight working variables with the (i-1)-st hash value:
|
||||
uint a = H[0],
|
||||
b = H[1],
|
||||
c = H[2],
|
||||
d = H[3],
|
||||
e = H[4],
|
||||
f = H[5],
|
||||
g = H[6],
|
||||
h = H[7];
|
||||
|
||||
// 3. For t=0 to 63:
|
||||
for (int t = 0; t < 64; ++t)
|
||||
{
|
||||
uint T1 = h + Sigma1(e) + Ch(e, f, g) + K[t] + W[t];
|
||||
uint T2 = Sigma0(a) + Maj(a, b, c);
|
||||
h = g;
|
||||
g = f;
|
||||
f = e;
|
||||
e = d + T1;
|
||||
d = c;
|
||||
c = b;
|
||||
b = a;
|
||||
a = T1 + T2;
|
||||
}
|
||||
|
||||
// 4. Compute the intermediate hash value H:
|
||||
H[0] = a + H[0];
|
||||
H[1] = b + H[1];
|
||||
H[2] = c + H[2];
|
||||
H[3] = d + H[3];
|
||||
H[4] = e + H[4];
|
||||
H[5] = f + H[5];
|
||||
H[6] = g + H[6];
|
||||
H[7] = h + H[7];
|
||||
}
|
||||
|
||||
internal void AddData(byte[] data, uint offset, uint len)
|
||||
{
|
||||
if (closed)
|
||||
throw new InvalidOperationException("Adding data to a closed hasher.");
|
||||
|
||||
if (len == 0)
|
||||
return;
|
||||
|
||||
bitsProcessed += len * 8;
|
||||
|
||||
while (len > 0)
|
||||
{
|
||||
uint amount_to_copy;
|
||||
|
||||
if (len < 64)
|
||||
{
|
||||
if (pendingBlockOff + len > 64)
|
||||
amount_to_copy = 64 - pendingBlockOff;
|
||||
else
|
||||
amount_to_copy = len;
|
||||
}
|
||||
else
|
||||
{
|
||||
amount_to_copy = 64 - pendingBlockOff;
|
||||
}
|
||||
|
||||
Array.Copy(data, offset, pendingBlock, pendingBlockOff, amount_to_copy);
|
||||
len -= amount_to_copy;
|
||||
offset += amount_to_copy;
|
||||
pendingBlockOff += amount_to_copy;
|
||||
|
||||
if (pendingBlockOff == 64)
|
||||
{
|
||||
toUintArray(pendingBlock, uintBuffer);
|
||||
ProcessBlock(uintBuffer);
|
||||
pendingBlockOff = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal byte[] GetHash()
|
||||
{
|
||||
return toByteArray(GetHashUint());
|
||||
}
|
||||
|
||||
internal uint[] GetHashUint()
|
||||
{
|
||||
if (!closed)
|
||||
{
|
||||
ulong size_temp = bitsProcessed;
|
||||
|
||||
AddData(new byte[1] { 0x80 }, 0, 1);
|
||||
|
||||
uint available_space = 64 - pendingBlockOff;
|
||||
|
||||
if (available_space < 8)
|
||||
available_space += 64;
|
||||
|
||||
// 0-initialized
|
||||
byte[] padding = new byte[available_space];
|
||||
// Insert length ulong
|
||||
for (uint i = 1; i <= 8; ++i)
|
||||
{
|
||||
padding[padding.Length - i] = (byte)size_temp;
|
||||
size_temp >>= 8;
|
||||
}
|
||||
|
||||
AddData(padding, 0u, (uint)padding.Length);
|
||||
|
||||
if (pendingBlockOff != 0) throw new Exception("Pending block offset should be 0.");
|
||||
|
||||
closed = true;
|
||||
}
|
||||
|
||||
return H;
|
||||
}
|
||||
|
||||
private static void toUintArray(byte[] src, uint[] dest)
|
||||
{
|
||||
for (uint i = 0, j = 0; i < dest.Length; ++i, j += 4)
|
||||
{
|
||||
dest[i] = (uint)src[j + 0] << 24 | (uint)src[j + 1] << 16 | (uint)src[j + 2] << 8 | src[j + 3];
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] toByteArray(uint[] src)
|
||||
{
|
||||
byte[] dest = new byte[src.Length * 4];
|
||||
int pos = 0;
|
||||
|
||||
for (int i = 0; i < src.Length; ++i)
|
||||
{
|
||||
dest[pos++] = (byte)(src[i] >> 24);
|
||||
dest[pos++] = (byte)(src[i] >> 16);
|
||||
dest[pos++] = (byte)(src[i] >> 8);
|
||||
dest[pos++] = (byte)src[i];
|
||||
}
|
||||
|
||||
return dest;
|
||||
}
|
||||
}
|
||||
}
|
||||
26
utils/Version.cs
Normal file
26
utils/Version.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
|
||||
namespace CMLeonOS
|
||||
{
|
||||
public static class Version
|
||||
{
|
||||
public static string Major = "1";
|
||||
public static string Minor = "0";
|
||||
public static string Patch = "0";
|
||||
|
||||
public static string FullVersion
|
||||
{
|
||||
get { return $"{Major}.{Minor}.{Patch}"; }
|
||||
}
|
||||
|
||||
public static string ShortVersion
|
||||
{
|
||||
get { return $"{Major}.{Minor}.{Patch}"; }
|
||||
}
|
||||
|
||||
public static string DisplayVersion
|
||||
{
|
||||
get { return $"CMLeonOS v{ShortVersion}"; }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user