Generate SHA1 Hash in Portable Class Library

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

    I have used this BouncyCastle Nuget package: https://www.nuget.org/packages/BouncyCastle-PCL/ and it works just fine for me (cross platforms Windows Store App, .Net Framework 4.5, Silverlight 5, Windows Phone 8, Xamarin.Android, Xamarin.iOS)

    Use HMACSHA1 to generate signature like this:

    public string GenerateSignature(string key, string signatureBase)
    {
       var keyBytes = Encoding.UTF8.GetBytes(key);
       HMACSHA1 hashAlgorithm = new HMACSHA1(keyBytes);            
       byte[] dataBuffer = Encoding.UTF8.GetBytes(signatureBase);
       byte[] hashBytes = hashAlgorithm.ComputeHash(dataBuffer);
       return Convert.ToBase64String(hashBytes);
    }
    

提交回复
热议问题