龙腾猫跃 hace 5 meses
padre
commit
3d91c024e2
Se han modificado 1 ficheros con 5 adiciones y 5 borrados
  1. 5 5
      Shared/Utils/CryptographyUtils.cs

+ 5 - 5
Shared/Utils/CryptographyUtils.cs

@@ -28,7 +28,7 @@ public static class CryptographyUtils {
     /// <summary>
     /// <summary>
     /// 计算字节数组的 Hash。返回十六进制小写字符串。
     /// 计算字节数组的 Hash。返回十六进制小写字符串。
     /// </summary>
     /// </summary>
-    public static string ComputeBytesHash(byte[] input, HashMethod method) {
+    public static string ComputeHash(byte[] input, HashMethod method) {
         using HashAlgorithm hashImpl = GetHashAlgorithm(method);
         using HashAlgorithm hashImpl = GetHashAlgorithm(method);
         return BitConverter.ToString(hashImpl.ComputeHash(input)).Replace("-", "").ToLower();
         return BitConverter.ToString(hashImpl.ComputeHash(input)).Replace("-", "").ToLower();
     }
     }
@@ -37,23 +37,23 @@ public static class CryptographyUtils {
     /// 计算字符串的 Hash。返回十六进制小写字符串。
     /// 计算字符串的 Hash。返回十六进制小写字符串。
     /// 使用 UTF-8 将字符串转换为字节数组。
     /// 使用 UTF-8 将字符串转换为字节数组。
     /// </summary>
     /// </summary>
-    public static string ComputeStringHash(string input, HashMethod method) 
-        => ComputeBytesHash(Encoding.UTF8.GetBytes(input), method);
+    public static string ComputeHash(string input, HashMethod method) 
+        => ComputeHash(Encoding.UTF8.GetBytes(input), method);
 
 
     #endregion
     #endregion
 
 
     #region ECDSA
     #region ECDSA
 
 
     /// <summary>
     /// <summary>
-    /// 进行 ECDSA P-256 签名验证。如果失败则抛出异常。
+    /// 进行 ECDSA P-256 签名验证。如果验证失败则抛出 <see cref="CryptographicException"/>。
     /// </summary>
     /// </summary>
     public static void EcdsaVerify(string sourceString, string sign,
     public static void EcdsaVerify(string sourceString, string sign,
         string publicKeyBase64 = "RUNTMSAAAAC4QTUNAewh23Q4Q6koHkyIrDIIZUSbua23sf2DiZmIRwSzadISDRyTVTbuWniH3KR7rKj8XBsabms1be6i3c+S") {
         string publicKeyBase64 = "RUNTMSAAAAC4QTUNAewh23Q4Q6koHkyIrDIIZUSbua23sf2DiZmIRwSzadISDRyTVTbuWniH3KR7rKj8XBsabms1be6i3c+S") {
-        // 使用 Windows CNG API 直接处理原始公钥和签名数据,以避免 .NET API 的兼容性问题
         using var sha256 = GetHashAlgorithm(HashMethod.Sha256);
         using var sha256 = GetHashAlgorithm(HashMethod.Sha256);
         byte[] hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(sourceString));
         byte[] hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(sourceString));
         IntPtr algorithmHandle = IntPtr.Zero;
         IntPtr algorithmHandle = IntPtr.Zero;
         IntPtr keyHandle = IntPtr.Zero;
         IntPtr keyHandle = IntPtr.Zero;
+        // 直接调用 DLL,以避免 .NET API 依赖的系统服务出现问题时导致验证失败
         try {
         try {
             int status = BCryptOpenAlgorithmProvider(out algorithmHandle, "ECDSA_P256", null, 0);
             int status = BCryptOpenAlgorithmProvider(out algorithmHandle, "ECDSA_P256", null, 0);
             if (status < 0) throw new CryptographicException($"{nameof(BCryptOpenAlgorithmProvider)} 失败,错误码 {status}");
             if (status < 0) throw new CryptographicException($"{nameof(BCryptOpenAlgorithmProvider)} 失败,错误码 {status}");