Generate SHA1 Hash in Portable Class Library

前端 未结 9 1969
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-07 23:56

I\'m trying to build a portable class library that generates OAuth urls for other classes/applications to use. This class library using OAuth has to be a portable class libr

9条回答
  •  花落未央
    2021-01-08 00:42

    I think the easiest way is to use the PCLCrypto nuget package. Then you can do:

    private static string CalculateSha1Hash(string input)
    {
            // step 1, calculate MD5 hash from input
            var hasher = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha1);
            byte[] inputBytes = Encoding.UTF8.GetBytes(input);
            byte[] hash = hasher.HashData(inputBytes);
    
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("X2"));
            }
            return sb.ToString();
    }
    

提交回复
热议问题