更新图数据库相关数据
This commit is contained in:
@@ -1,13 +1,18 @@
|
||||
using Microsoft.VisualBasic;
|
||||
using NPOI.SS.UserModel;
|
||||
using NPOI.XSSF.UserModel;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Threading;
|
||||
|
||||
namespace Ramitta.lib
|
||||
@@ -25,14 +30,14 @@ namespace Ramitta.lib
|
||||
#endregion
|
||||
|
||||
#region 调试函数
|
||||
public static void DebugBar(Label obj, String text, SolidColorBrush? color=null)
|
||||
public static void DebugBar(Label obj, String? text, SolidColorBrush? color=null)
|
||||
{
|
||||
if (obj == null) return;
|
||||
// 通过Dispatcher确保在UI线程执行
|
||||
obj.Dispatcher.Invoke(() =>
|
||||
{
|
||||
obj.Content = text;
|
||||
if(color!=null)obj.Background = color;
|
||||
if (text!=null)obj.Content = text;
|
||||
if (color != null) obj.Background = color;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -82,21 +87,108 @@ namespace Ramitta.lib
|
||||
}
|
||||
#endregion
|
||||
|
||||
public static void FileWrite(string outputPath, string outputText)
|
||||
#region 文件解析
|
||||
public static bool IsPathExist(string databasePath)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(databasePath))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return Directory.Exists(databasePath);
|
||||
}
|
||||
public static void FileWrite(string outputPath, string outputText, bool createDirectories = true,bool fileAdd = false)
|
||||
{
|
||||
string directory = Path.GetDirectoryName(outputPath);
|
||||
if (createDirectories && !Directory.Exists(directory))
|
||||
{
|
||||
Directory.CreateDirectory(directory);
|
||||
}
|
||||
|
||||
// 使用 StreamWriter 写入文件
|
||||
using (StreamWriter writer = new StreamWriter(outputPath, false))
|
||||
{
|
||||
Debug.WriteLine(outputText);
|
||||
|
||||
writer.WriteLine(outputText);
|
||||
}
|
||||
}
|
||||
|
||||
public static string FileRead(string outputPath)
|
||||
{
|
||||
return File.ReadAllText(outputPath);
|
||||
}
|
||||
|
||||
// 获取路径下所有文件和文件夹,支持传入判断文件的函数
|
||||
public static List<string> GetFilesAndDirectories(string path, bool? isDirectory = null, Func<string, bool> fileFilter = null, Func<string, string> outputFormat = null)
|
||||
{
|
||||
List<string> result = new List<string>();
|
||||
|
||||
// 获取文件夹
|
||||
var directories = Directory.GetDirectories(path);
|
||||
foreach (var dir in directories)
|
||||
{
|
||||
if (isDirectory == null || isDirectory == true)
|
||||
{
|
||||
// 如果指定了输出格式函数,则使用格式化函数;否则使用原始路径
|
||||
result.Add(outputFormat == null ? dir : outputFormat(dir));
|
||||
}
|
||||
}
|
||||
|
||||
// 获取文件
|
||||
var files = Directory.GetFiles(path);
|
||||
foreach (var file in files)
|
||||
{
|
||||
// 如果传入了 fileFilter 函数,调用该函数进行筛选
|
||||
if (fileFilter == null || fileFilter(file))
|
||||
{
|
||||
if (isDirectory == null || isDirectory == false)
|
||||
{
|
||||
// 如果指定了输出格式函数,则使用格式化函数;否则使用原始路径
|
||||
result.Add(outputFormat == null ? file : outputFormat(file));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// 生成截图
|
||||
public static PngBitmapEncoder CaptureWindowScreenshot(Window window, string? filePath = null)
|
||||
{
|
||||
// 获取当前窗口的 DPI 缩放因子
|
||||
var dpiScale = VisualTreeHelper.GetDpi(window);
|
||||
|
||||
// 获取 DPI 缩放因子的 X 和 Y 值,通常 X 和 Y 应该是相等的
|
||||
double dpiX = dpiScale.DpiScaleX;
|
||||
double dpiY = dpiScale.DpiScaleY;
|
||||
|
||||
// 根据 DPI 缩放因子调整截图的尺寸
|
||||
int width = (int)(window.ActualWidth * dpiX); // 使用 dpiX
|
||||
int height = (int)(window.ActualHeight * dpiY); // 使用 dpiY
|
||||
|
||||
// 创建一个 RenderTargetBitmap 对象,并且考虑 DPI 缩放
|
||||
RenderTargetBitmap rtb = new RenderTargetBitmap(width, height, 96 * dpiX, 96 * dpiY, System.Windows.Media.PixelFormats.Pbgra32);
|
||||
|
||||
// 使用 VisualTreeHelper 进行渲染
|
||||
rtb.Render(window);
|
||||
|
||||
// 创建一个 PngBitmapEncoder 来保存为 PNG 格式
|
||||
PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
|
||||
pngEncoder.Frames.Add(BitmapFrame.Create(rtb));
|
||||
|
||||
if (!string.IsNullOrEmpty(filePath))
|
||||
{
|
||||
// 保存图像到文件
|
||||
using (FileStream fs = new FileStream(filePath, FileMode.Create))
|
||||
{
|
||||
pngEncoder.Save(fs);
|
||||
}
|
||||
}
|
||||
return pngEncoder;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 任务操作
|
||||
public static async Task<int> RunExternalCommand(string? applicationPath = "explorer.exe", string arguments="", bool UseShellExecute = false, bool CreateNoWindow = false)
|
||||
{
|
||||
ProcessStartInfo startInfo = new ProcessStartInfo
|
||||
@@ -176,7 +268,146 @@ namespace Ramitta.lib
|
||||
throw; // 重新抛出异常以便调用者捕获
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
// 生成哈希
|
||||
public static string GenerateHash(string input)
|
||||
{
|
||||
// 创建SHA256对象
|
||||
using (SHA256 sha256 = SHA256.Create())
|
||||
{
|
||||
// 将输入字符串转换为字节数组并计算哈希值
|
||||
byte[] bytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(input));
|
||||
|
||||
// 将哈希值转换为十六进制字符串
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
foreach (byte b in bytes)
|
||||
{
|
||||
stringBuilder.Append(b.ToString("x2"));
|
||||
}
|
||||
|
||||
// 返回最终的哈希字符串
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
public class CryptoHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 生成RSA密钥对
|
||||
/// </summary>
|
||||
/// <param name="keySize">密钥长度(通常为2048、4096)</param>
|
||||
/// <returns>包含公钥和私钥的元组</returns>
|
||||
public static (string publicKey, string privateKey) GenerateKeyPair(int keySize = 2048)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var rsa = RSA.Create(keySize))
|
||||
{
|
||||
string publicKey = rsa.ToXmlString(false);
|
||||
string privateKey = rsa.ToXmlString(true);
|
||||
|
||||
return (publicKey, privateKey);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"密钥生成失败: {ex.Message}");
|
||||
return (null, null);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用私钥对数据进行签名
|
||||
/// </summary>
|
||||
public static string? SignData(string data, string privateKey)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var rsa = RSA.Create())
|
||||
{
|
||||
rsa.FromXmlString(privateKey);
|
||||
|
||||
byte[] dataBytes = Encoding.UTF8.GetBytes(data);
|
||||
byte[] signature = rsa.SignData(dataBytes, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
|
||||
|
||||
return Convert.ToBase64String(signature);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用公钥验证签名
|
||||
/// </summary>
|
||||
public static bool? VerifySignature(string data, string signature, string publicKey)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var rsa = RSA.Create())
|
||||
{
|
||||
rsa.FromXmlString(publicKey);
|
||||
|
||||
byte[] dataBytes = Encoding.UTF8.GetBytes(data);
|
||||
byte[] signatureBytes = Convert.FromBase64String(signature);
|
||||
|
||||
return rsa.VerifyData(dataBytes, signatureBytes, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用公钥加密数据
|
||||
/// </summary>
|
||||
public static string? Encrypt(string data, string publicKey)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var rsa = RSA.Create())
|
||||
{
|
||||
rsa.FromXmlString(publicKey);
|
||||
|
||||
byte[] dataBytes = Encoding.UTF8.GetBytes(data);
|
||||
byte[] encryptedBytes = rsa.Encrypt(dataBytes, RSAEncryptionPadding.OaepSHA256);
|
||||
|
||||
return Convert.ToBase64String(encryptedBytes);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用私钥解密数据
|
||||
/// </summary>
|
||||
public static string? Decrypt(string encryptedData, string privateKey)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var rsa = RSA.Create())
|
||||
{
|
||||
rsa.FromXmlString(privateKey);
|
||||
|
||||
byte[] encryptedBytes = Convert.FromBase64String(encryptedData);
|
||||
byte[] decryptedBytes = rsa.Decrypt(encryptedBytes, RSAEncryptionPadding.OaepSHA256);
|
||||
|
||||
return Encoding.UTF8.GetString(decryptedBytes);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user