C# 使用 HMAC – SHA256 加密

C# 使用 HMAC – SHA256 加密

C# 使用 HMAC – SHA256 加密


資料來源: https://marcus116.blogspot.com/2019/06/how-to-create-hmacsha256-cryptographyin-csharp.html


線上編譯(c# online compiler): https://www.programiz.com/csharp-programming/online-compiler/

線上HMAC – SHA256 加密: https://codebeautify.org/hmac-generator


Code:

// Online C# Editor for free
// Write, Edit and Run your C# code using C# Online Compiler

using System;
using System.Security.Cryptography;
public class HelloWorld
{
    public static string HMACSHA256(string message, string key)
    {
        var encoding = new System.Text.UTF8Encoding();
        byte[] keyByte = encoding.GetBytes(key);
        byte[] messageBytes = encoding.GetBytes(message);
        using (var hmacSHA256 = new HMACSHA256(keyByte))
        {
            byte[] hashMessage = hmacSHA256.ComputeHash(messageBytes);
            return BitConverter.ToString(hashMessage).Replace("-", "").ToLower();
        }
    }    
    public static void Main(string[] args)
    {
        Console.WriteLine ("Try programiz.pro");
        Console.WriteLine (HMACSHA256("TEST", "KEY"));//615dac1c53c9396d8f69a419a0b2d9393a0461d7ad5f7f3d9beb57264129ef12
    }
}

One thought on “C# 使用 HMAC – SHA256 加密

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *