using System;
using System.Security.Cryptography;

/// <summary>
/// 生成密钥
/// </summary>
public class KeyGenerator
{
    /// <summary>
    /// 根据ID与当前日期生成密钥
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public static string GenerateKey(string id)
    {
        // 将ID和当前日期组合成一个字符串
        string combinedString = id + DateTime.Now.ToString("yyyyMMdd");

        // 将字符串转换成字节数组
        byte[] combinedBytes = System.Text.Encoding.UTF8.GetBytes(combinedString);

        // 使用SHA256哈希算法生成密钥
        using (var sha256 = SHA256.Create())
        {
            byte[] hashBytes = sha256.ComputeHash(combinedBytes);

            // 将字节数组转换成字符串表示形式作为最终的密钥
            string key = BitConverter.ToString(hashBytes).Replace("-", "").ToLower();

            return key;
        }
    }
}
