開發了 Stable Diffusion 桌面應用程式「Sodalite」
使用 Stable Diffusion 生成圖片的方法有很多種,但 Web UI 類型的工具往往需要建置 Python 環境、管理相依套件,過程相當繁瑣。
想要更簡單、只要下載安裝程式就能使用的桌面應用程式,於是從零開始開發了 Sodalite。
使用 Stable Diffusion 生成圖片的方法有很多種,但 Web UI 類型的工具往往需要建置 Python 環境、管理相依套件,過程相當繁瑣。
想要更簡單、只要下載安裝程式就能使用的桌面應用程式,於是從零開始開發了 Sodalite。
這篇備忘錄記錄了在 C# 中實現 MD5 雜湊的方法。
MD5 (Message-Digest Algorithm 5) 是一種廣泛使用的加密雜湊函數,它生成一個 128 位(16 字節)的雜湊值。儘管 MD5 在加密安全方面已被認為不再安全(存在碰撞攻擊),但它仍然經常用於文件完整性校驗或非安全數據的快速雜湊。
System.Security.Cryptography.MD5 類C# 提供了內置的 MD5 類(位於 System.Security.Cryptography 命名空間)來計算 MD5 雜湊值。
using System;
using System.Security.Cryptography;
using System.Text;
public class Md5Hasher
{
public static string CalculateMd5Hash(string input)
{
// 創建 MD5 雜湊算法的實例
using (MD5 md5 = MD5.Create())
{
// 將輸入字串轉換為字節數組
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
// 計算雜湊值
byte[] hashBytes = md5.ComputeHash(inputBytes);
// 將字節數組轉換為十六進制字串
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("x2")); // "x2" 格式化為兩位十六進制
}
return sb.ToString();
}
}
public static void Main(string[] args)
{
string text = "Hello, MD5!";
string hash = CalculateMd5Hash(text);
Console.WriteLine($"原始字串: {text}");
Console.WriteLine($"MD5 雜湊: {hash}");
// 預期的 MD5 雜湊值 for "Hello, MD5!" (小寫)
// 9e2469446d3288a7c207b04cfd6e01a4
}
}
對文件內容計算 MD5 雜湊通常用於驗證文件是否在傳輸過程中被修改。
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
public class FileMd5Hasher
{
public static string CalculateFileMd5Hash(string filePath)
{
if (!File.Exists(filePath))
{
return "文件不存在。";
}
using (MD5 md5 = MD5.Create())
{
using (FileStream stream = File.OpenRead(filePath))
{
byte[] hashBytes = md5.ComputeHash(stream);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("x2"));
}
return sb.ToString();
}
}
}
public static void Main(string[] args)
{
string filePath = "example.txt"; // 替換為你的文件路徑
// 創建一個範例文件
File.WriteAllText(filePath, "這是一個測試文件的內容。");
string fileHash = CalculateFileMd5Hash(filePath);
Console.WriteLine($"文件路徑: {filePath}");
Console.WriteLine($"文件 MD5 雜湊: {fileHash}");
// 清理範例文件
File.Delete(filePath);
}
}
C# 中的 MD5 類提供了一種簡單直觀的方式來計算 MD5 雜湊值。雖然它在安全性方面存在限制,但在正確的應用場景下(如文件校驗),它仍然是一個有用的工具。務必牢記其安全限制,避免在加密敏感的上下文中使用它。
用結構體將行數、列數與儲存數值的記憶體整合在一起,會更方便操作。
#include <stdio.h>
#include <stdlib.h>
typedef struct {
float *data;
int col_size;
int row_size;
} Mat;
void MatInit(Mat *mat, int row_size, int col_size) {
mat->row_size = row_size;
mat->col_size = col_size;
mat->data = (float *)calloc(row_size * col_size, sizeof(float));
}
float *MatAt(Mat *mat, int i, int j) {
return mat->data + i * mat->col_size + j;
}
void MatFree(Mat *mat) { free(mat->data); }
int main(void) {
Mat mat;
MatInit(&mat, 30, 5); // 初始化 30 行 5 列的矩陣
*MatAt(&mat, 0, 0) = 50; // 將第 0 行第 0 列設為 50
printf("%f\n", *MatAt(&mat, 0, 0)); // 顯示第 0 行第 0 列的數值
MatFree(&mat);
return 0;
}
這篇筆記記錄了 GCD(最大公約數)的 C 語言程式。
GCD(Greatest Common Divisor),中文稱為最大公約數,是指兩個或多個整數共有的約數中最大的一個。
例如,24 和 36 的公約數有 1、2、3、4、6、12,其中最大的公約數是 12。
計算 GCD 最常見且有效的方法是歐幾里得演算法。其原理基於以下定理:
$$ \gcd(a, b) = \gcd(b, a \bmod b) $$
當 $b = 0$ 時,$\gcd(a, 0) = a$。
遞迴版本:
// gcd.c
int gcd(int a, int b){
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
非遞迴版本:
// gcd_iterative.c
int gcd_iterative(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
#include <stdio.h>
// 遞迴版本的 GCD 函數
int gcd(int a, int b){
return !b ? a : gcd(b, a % b);
}
// 非遞迴版本的 GCD 函數
int gcd_iterative(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
int main(void){
printf("GCD(24, 36) = %d
", gcd(24, 36)); // 輸出 12
printf("GCD_iterative(24, 36) = %d
", gcd_iterative(24, 36)); // 輸出 12
printf("GCD(48, 18) = %d
", gcd(48, 18)); // 輸出 6
printf("GCD_iterative(48, 18) = %d
", gcd_iterative(48, 18)); // 輸出 6
return 0;
}
GCD 是數論中的一個基本概念,歐幾里得演算法提供了一種高效的計算方法。無論是遞迴還是非遞迴實現,其核心思想都是利用模運算將問題規模不斷縮小,直到其中一個數變為零。
建立 DLL 並編譯 C 的備忘錄
// gcd.c
int gcd(int a, int b){
return !b ? a : gcd(b, a % b);
}
// gcd.h
#ifndef TEST_H
#define TEST_H
int gcd(int a, int b);
#endif
#include <stdio.h>
#include "gcd.h"
int main(void){
printf("%d
", gcd(24, 36));
}
gcc gcd.c -shared -o gcd.dll
gcc main.c -lgcd -L.