【问题标题】:HashAlgorithms in CoreCLRCoreCLR 中的哈希算法
【发布时间】:2015-10-20 19:28:28
【问题描述】:

我正在尝试在我的 coreclr 项目中使用此 class,但我似乎无法为 SHA256Managed 找到正确的包。我尝试过使用 System.Security.Cryptography.Algorithms": "4.0.0-beta-23409" 但它不包含 SHA2565Managed 的​​实现。在 coreclr 中计算哈希值还有其他替代方法吗?

【问题讨论】:

    标签: asp.net-core coreclr


    【解决方案1】:

    你可以使用命名空间System.Security.Cryptography中的SHA256.Create()

    (组件:System.Security.Cryptography.Algorithms

    using (var algorithm = SHA256.Create())
    {
        // Create the at_hash using the access token returned by CreateAccessTokenAsync.
        var hash = algorithm.ComputeHash(Encoding.ASCII.GetBytes(response.AccessToken));
    
        // Note: only the left-most half of the hash of the octets is used.
        // See http://openid.net/specs/openid-connect-core-1_0.html#CodeIDToken
        identity.AddClaim(JwtRegisteredClaimNames.AtHash, Base64UrlEncoder.Encode(hash, 0, hash.Length / 2));
    }
    

    【讨论】:

    • 实际上是在System.Security.Cryptography,而不是System.Security.Cryptography.Algorithms
    • @Pavel 包名是System.Security.Cryptography.Algorithms,命名空间是System.Security.Cryptography
    • 哦,我明白了。我没有意识到这是一个 NuGet 包。
    • 仅供参考,它不是 NuGet 包。它是 .Net Core 的一部分。
    • 我在 .NET Core 上遇到了 PlatformNotSupportedException 的问题。用 (HashAlgorithm)CryptoConfig.CreateFromName("SHA-256"); 修复它github.com/dotnet/corefx/issues/22626
    猜你喜欢
    • 2016-06-23
    • 2012-06-03
    • 2021-05-03
    • 2013-09-19
    • 1970-01-01
    • 1970-01-01
    • 2017-01-30
    • 2011-06-08
    • 2011-10-10
    相关资源
    最近更新 更多