Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c# hmacsha256

作者:——嘘丶低调   发布日期:2025-11-22   浏览:48

using System;
using System.Text;
using System.Security.Cryptography;

class Program
{
    static void Main()
    {
        string secret = "your-secret-key";
        string message = "your-message-to-hash";

        using (var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(secret)))
        {
            byte[] hashMessage = hmac.ComputeHash(Encoding.UTF8.GetBytes(message));
            string hashString = BitConverter.ToString(hashMessage).Replace("-", "").ToLower();
            Console.WriteLine("HMAC SHA256: " + hashString);
        }
    }
}

解释说明:

  1. 导入命名空间System, System.Text, 和 System.Security.Cryptography 是必要的命名空间,用于处理字符串编码和加密操作。
  2. 定义密钥和消息secret 是用于生成 HMAC 的密钥,message 是要哈希的消息。
  3. 创建 HMACSHA256 实例:使用指定的密钥创建 HMACSHA256 对象。
  4. 计算哈希值:使用 ComputeHash 方法计算消息的哈希值。
  5. 转换为字符串:将字节数组转换为十六进制字符串,并去除分隔符(如 -),最后转换为小写。
  6. 输出结果:打印生成的 HMAC SHA256 值。

希望这段代码和解释对你有帮助!

上一篇:c# console

下一篇:c#序列化

大家都在看

c# 二进制

c# datatable group by

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

.net和c#

c#获取系统时间

c#游戏开发

c#网络编程

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站