亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

c# openssl如何進行哈希計算

c#
小樊
82
2024-10-18 18:53:37
欄目: 云計算

在C#中使用OpenSSL進行哈希計算,你可以使用OpenSSL的EVP_Digest函數。以下是一個示例代碼,展示了如何使用OpenSSL的SHA256哈希算法來計算字符串的哈希值:

using System;
using System.Runtime.InteropServices;

class Program
{
    static void Main()
    {
        string input = "Hello, world!";
        string hash = ComputeHash(input, "SHA256");
        Console.WriteLine($"Hash of '{input}' is: {hash}");
    }

    static string ComputeHash(string input, string algorithm)
    {
        byte[] inputBytes = Encoding.UTF8.GetBytes(input);
        byte[] hashBytes = ComputeHash(inputBytes, algorithm);

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < hashBytes.Length; i++)
        {
            sb.Append(hashBytes[i].ToString("x2"));
        }
        return sb.ToString();
    }

    static unsafe byte[] ComputeHash(byte[] input, string algorithm)
    {
        int hashSize = GetHashSize(algorithm);
        byte[] hash = new byte[hashSize];

        fixed (byte* inputPtr = input)
        fixed (byte* hashPtr = hash)
        {
            using (OpenSSLHandle handle = OpenSSLHandle.Create())
            {
                int result = OpenSSLNative.EVP_Digest(handle, inputPtr, input.Length, hashPtr, hashSize, algorithm);
                if (result != 0)
                {
                    throw new Exception("OpenSSL error: " + result);
                }
            }
        }

        return hash;
    }

    static int GetHashSize(string algorithm)
    {
        switch (algorithm)
        {
            case "MD5":
                return OpenSSLNative.EVP_MD_size("MD5");
            case "SHA1":
                return OpenSSLNative.EVP_MD_size("SHA1");
            case "SHA256":
                return OpenSSLNative.EVP_MD_size("SHA256");
            case "SHA384":
                return OpenSSLNative.EVP_MD_size("SHA384");
            case "SHA512":
                return OpenSSLNative.EVP_MD_size("SHA512");
            default:
                throw new ArgumentException("Unsupported algorithm", nameof(algorithm));
        }
    }
}

class OpenSSLHandle : IDisposable
{
    private IntPtr handle;

    public OpenSSLHandle()
    {
        handle = OpenSSLNative.EVP_MD_CTX_new();
        if (handle == IntPtr.Zero)
        {
            throw new Exception("Failed to create OpenSSL handle");
        }
    }

    public void Dispose()
    {
        if (handle != IntPtr.Zero)
        {
            OpenSSLNative.EVP_MD_CTX_free(handle);
            handle = IntPtr.Zero;
        }
    }
}

static class OpenSSLNative
{
    [DllImport("libssl", EntryPoint = "EVP_Digest")]
    public static extern int EVP_Digest(IntPtr ctx, byte* input, int inputLen, byte* output, int outputLen, string mdName);

    [DllImport("libssl", EntryPoint = "EVP_MD_size")]
    public static extern int EVP_MD_size(string mdName);

    [DllImport("libssl", EntryPoint = "EVP_MD_CTX_new")]
    public static extern IntPtr EVP_MD_CTX_new();

    [DllImport("libssl", EntryPoint = "EVP_MD_CTX_free")]
    public static extern void EVP_MD_CTX_free(IntPtr ctx);
}

請注意,這個示例代碼使用了OpenSSLNative類來調用OpenSSL的本地庫函數。你需要確保在你的項目中引用了OpenSSL庫,并且在編譯時設置了正確的庫路徑。此外,這個示例代碼僅用于演示目的,實際應用中可能需要更多的錯誤處理和內存管理。

0
武城县| 抚远县| 万山特区| 阿拉善盟| 乡宁县| 义马市| 襄城县| 安吉县| 兴隆县| 墨竹工卡县| 苗栗市| 嘉鱼县| 大庆市| 荣昌县| 定襄县| 株洲县| 三河市| 济宁市| 武山县| 化德县| 岢岚县| 鄂伦春自治旗| 阳春市| 阿鲁科尔沁旗| 柘城县| 琼海市| 白银市| 仁怀市| 安化县| 什邡市| 杭州市| 前郭尔| 万源市| 临海市| 手机| 远安县| 南木林县| 德化县| 叙永县| 虞城县| 都匀市|