English 中文(简体)
2. 通用标准 便携式级图书馆哈希姆
原标题:Generate SHA1 Hash in Portable Class Library
  • 时间:2012-04-20 22:28:57
  •  标签:
  • c#
  • sha1

I m试图建造一个便携式教室,为其他班级/应用程序提供OAuth url。 这个使用OAuth的班级图书馆必须是一个便携式级图书馆,以便它能够使用DroopBox AP Im大楼的不同版本。

这一类别的一部分需要产生一种SHA1的异构体,以产生 o。

我知道,便携式班级图书馆是无支持系统。 安全。 密码是这样,这一类人能够产生一种没有这种等级的SHA1 has吗?

最佳回答

Mono 提供,用于管理对SHA1的实施工作。 (但如“CodeInChaos”所建议的,它并不位于Mono. Security.dll。)

它具有开放源,经过了很好的测试,意在与微软实施完全一样(例如,它源自SHA1,HashAlgorith. 实施ICrypto Transform<>>/code>......),因此,它应当易于成为退约的替代物。

问题回答

我认为最容易的是使用PCLCrypto nuget Pack。 然后,你可以做到:

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();
}

我使用了这一丛书:。 https://www.nuget.org/ Packages/BouncyCastle-PCL/。 并且对我只做罚款(横跨平台Windows Store App, .Net Framework 4.5,银光5,Windows 8,Xamarin.Android, Xamarin.iOS)。

利用HMACSHA1号这样的签名:

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);
}

我也希望签署奥阿特,并且正在研究PCL的密码——这一测试表明,HmacSha1'h的形成,并将结果与标准进行比较。 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]);
        }
    }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签