Generate SHA1 Hash in Portable Class Library

前端 未结 9 2028
爱一瞬间的悲伤
爱一瞬间的悲伤 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条回答
  •  -上瘾入骨i
    2021-01-08 00:53

    I wanted sign OAuth also, and am looking at PCL Crypto - this test shows creation of a HmacSha1 hash, and compares the result to the standard .NET Framework way.

        [Test]
        public void CreateHash_VersusComputeHash_ReturnsEquivalent()
        {
            // USING TRADITIONAL .NET:
            var key = new byte[32];
            var contentBytes = Encoding.UTF8.GetBytes("some kind of content to hash");
            new RNGCryptoServiceProvider().GetBytes(key);
    
            var alg = new HMACSHA1(key); // Bouncy castle usage does not differ from this
            var result = alg.ComputeHash(contentBytes);
    
    
    
    
            // USING PCL CRYPTO:
            var algorithm = WinRTCrypto.MacAlgorithmProvider.OpenAlgorithm(MacAlgorithm.HmacSha1);
    
            byte[] mac;
            using (var hasher = algorithm.CreateHash(key))
            {
                hasher.Append(contentBytes);
                mac = hasher.GetValueAndReset();
            }
    
    
    
    
            // Assert results:
            Assert.AreEqual(result.Length, mac.Length);
    
            for (var i = 0; i < result.Length; i++)
            {
                Assert.AreEqual(result[i], mac[i]);
            }
        }
    

提交回复
热议问题