Generate SHA1 Hash in Portable Class Library

前端 未结 9 1970
爱一瞬间的悲伤
爱一瞬间的悲伤 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:32

    You might want to check out the new .NET Standard library:

    https://docs.microsoft.com/en-us/dotnet/articles/standard/library

    It is portable, and System.Security.Cryptography is included.

        /// 
        /// Compute hash for string encoded as UTF8
        /// 
        /// String to be hashed.
        /// 40-character hex string.
        public static string GetSha1(string input)
        {
            using (var sha1 = System.Security.Cryptography.SHA1.Create())
            {
                byte[] inputBytes = Encoding.UTF8.GetBytes(input);
                byte[] hash = sha1.ComputeHash(inputBytes);
    
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < hash.Length; i++)
                {
                    sb.Append(hash[i].ToString("X2"));
                }
                return sb.ToString();
            }
        }
    

    You might also get some help (for creating a PCL project with .NET Standard Library) here:

    https://xamarinhelp.com/dot-net-standard-pcl-xamarin-forms/

提交回复
热议问题