From bed8ba7d67357e840b967a639c37262a1a416fcb Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 4 Jul 2024 16:23:59 +0800 Subject: [PATCH] Use `sha256` to calculate the cache key. Resolve #20164 --- .../System/AbpStringExtensions.cs | 30 +++++++++++++++++++ ...IdentityModelDiscoveryDocumentCacheItem.cs | 2 +- .../IdentityModelTokenCacheItem.cs | 2 +- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/framework/src/Volo.Abp.Core/System/AbpStringExtensions.cs b/framework/src/Volo.Abp.Core/System/AbpStringExtensions.cs index 52258a12ee..bdfd644b28 100644 --- a/framework/src/Volo.Abp.Core/System/AbpStringExtensions.cs +++ b/framework/src/Volo.Abp.Core/System/AbpStringExtensions.cs @@ -400,6 +400,36 @@ public static class AbpStringExtensions } } + public static string ToSha256(this string str) + { + using (var sha = SHA256.Create()) + { + var data = sha.ComputeHash(Encoding.UTF8.GetBytes(str)); + + var sb = new StringBuilder(); + foreach (var d in data) + { + sb.Append(d.ToString("x2")); + } + return sb.ToString(); + } + } + + public static string ToSha512(this string str) + { + using (var sha = SHA512.Create()) + { + var data = sha.ComputeHash(Encoding.UTF8.GetBytes(str)); + + var sb = new StringBuilder(); + foreach (var d in data) + { + sb.Append(d.ToString("x2")); + } + return sb.ToString(); + } + } + /// /// Converts camelCase string to PascalCase string. /// diff --git a/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelDiscoveryDocumentCacheItem.cs b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelDiscoveryDocumentCacheItem.cs index 6cabe5d9b9..5a8e617423 100644 --- a/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelDiscoveryDocumentCacheItem.cs +++ b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelDiscoveryDocumentCacheItem.cs @@ -24,6 +24,6 @@ public class IdentityModelDiscoveryDocumentCacheItem public static string CalculateCacheKey(IdentityClientConfiguration configuration) { - return configuration.Authority.ToLower().ToMd5(); + return configuration.Authority.ToLower().ToSha256(); } } diff --git a/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelTokenCacheItem.cs b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelTokenCacheItem.cs index 72beed1577..1970d7aba3 100644 --- a/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelTokenCacheItem.cs +++ b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelTokenCacheItem.cs @@ -21,6 +21,6 @@ public class IdentityModelTokenCacheItem public static string CalculateCacheKey(IdentityClientConfiguration configuration) { - return string.Join(",", configuration.Select(x => x.Key + ":" + x.Value)).ToMd5(); + return string.Join(",", configuration.Select(x => x.Key + ":" + x.Value)).ToSha256(); } }