c#

c# openssl如何进行哈希计算

小樊
83
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
看了该问题的人还看了