Generate SHA1 Hash in Portable Class Library

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

    Well I needed this too recently and I found much easier to take SHA1 implementation from HashLib : http://hashlib.codeplex.com/

    Mono implementation have some far-going dependencies (localization of exceptions, etc.), while from HashLib you need only to copy few files without any changes in them:

    Converters.cs
    Hash.cs
    HashBuffer.cs
    HashCryptoNotBuildIn.cs
    HashResult.cs
    IHash.cs
    SHA0.cs
    SHA1.cs
    

    55 KB of code total, so nothing too heavy.

    0 讨论(0)
  • 2021-01-08 00:56

    The SHA-1 Wikipedia article contains pseudocode that you could use as a guideline for your own implementation. But, as always with cryptographic functions, I strongly advise to use a tried and tested implementation.

    Assuming you want a SHA-256 implementation, you can find one in BouncyCastle, which is available in source code form. The relevant class there is called Org.BouncyCastle.Crypto.Digests.Sha256Digest (here's its source).

    0 讨论(0)
  • 2021-01-08 00:56

    This worked for me when I had to achieve the same outcome. You can do this with SHA512 and others too.

    using System.Security.Cryptography;
    
    public static string HashSHA1(this string value)
    {
        using (var sha = SHA1.Create())
        {
           return Convert.ToBase64String(sha.ComputeHash(System.Text.Encoding.UTF8.GetBytes(value)));
        }
    }
    

    Code cited from: https://xamarinhelp.com/cryptography-in-xamarin-forms/

    0 讨论(0)
提交回复
热议问题