简体   繁体   English

如何在 C# 中使用密钥和 MD5 算法散列字符串?

[英]How to hash a string with a secret key and MD5 algorithm in C#?

I am working on an API which requires a header for API authentication.我正在开发一个需要 API 身份验证标头的 API。 The header contains a hash string which is created using md5 algorithm and a secret key.标头包含使用 md5 算法和密钥创建的哈希字符串。 I want to write a function like this:我想写一个这样的函数:

public string CreateMD5Hash(string input, string secretKey)
{
    return output;
}

I tried to use bouncy castle API.我尝试使用充气城堡 API。 But I couldn't find proper documentation.但我找不到合适的文档。 That is why I couldn't make it use.这就是我无法使用它的原因。

Okay BASICALLY let's break this into two simple concepts Cryptography and Hashing .好的,基本上让我们将其分解为两个简单的概念CryptographyHashing

Cryptography密码学

There are three fields secrete key a value and an encrypted value , and two methods.有三个字段secrete key一个value和一个encrypted value ,以及两种方法。

  • Encrypt(value, secret-key) this method gets value and secret key and returns the encrypted value . Encrypt(value, secret-key)此方法获取valuesecret key并返回encrypted value
  • Decrypt(encrypted-value, secret-key) and this method gets encrypted value and secret key and returns the value . Decrypt(encrypted-value, secret-key) ,该方法获取encrypted valuesecret key并返回value
    like AES , DES , etc ...AESDES等...

Hashing散列

There are just two fields value and hashed value , and one method.只有两个字段valuehashed value ,以及一种方法。

  • Hash(value) this method gets the value and returns the hashed value . Hash(value)此方法获取value并返回hashed value

like MD5 , SHA family, etc ...MD5SHA系列等...

Simple right!简单对!

So your question is not correct because MD5 is a hashing algorithm that usually is used for hashing passwords and comparing the hash of them.所以你的问题是不正确的,因为MD5是一种散列算法,通常用于散列密码并比较它们的散列。

Now I recommend taking a look at the API you are talking about for authentication.现在,我建议查看您正在谈论的用于身份验证的 API。

You can share the link of documentation or an already encrypted header (if it is not sensitive data) to help you.您可以共享文档链接或已加密的标题(如果不是敏感数据)来帮助您。

Update更新

According to the link, it is using HMAC with MD5 .根据链接,它使用带有MD5 HMAC This is the sample in the document, written in PHP .这是文档中的示例,用PHP

 $hash = hash_hmac('md5', $string, $key);

You can use this code for C#:您可以将此代码用于 C#:

using System.Security.Cryptography;
using System.Text;

...

public string HashHmacMD5(string message, string secret)
{
    Encoding encoding = Encoding.UTF8;
    using (HMACMD5 hmac = new HMACMD5(encoding.GetBytes(secret)))
    {
        var msg = encoding.GetBytes(message);
        var hash = hmac.ComputeHash(msg);
        return BitConverter.ToString(hash).ToLower().Replace("-", string.Empty);
    }
}

Your question is somewhat confusing.你的问题有点令人困惑。

For simple API authentication, you want to generate a secret that you want to share with the caller.对于简单的 API 身份验证,您希望生成要与调用者共享的机密。 This secret should be validated on your side.这个秘密应该在你这边得到验证。

Normally you would generate a random secure string with length about 50 - this is the secret.通常你会生成一个长度约为 50 的随机安全字符串 - 这是秘密。 You share this secret with the caller - normally you warn the caller to securely store the value, since there is no way to recover it.您与调用者共享此秘密 - 通常您会警告调用者安全地存储该值,因为无法恢复它。 Do not store this secret in the database.不要将此秘密存储在数据库中。

You would generate a salt and hash the secret.您将生成盐并散列秘密。 You store both the salt and the hashed secret in the database.您将盐和散列的秘密都存储在数据库中。 When the request comes in, you extract the value in the header, hash it with the salt and compare the result with the hashed secret in the database.当请求进来时,您提取标头中的值,用盐对其进行散列,并将结果与​​数据库中散列的秘密进行比较。 User would be authenticated if they are match.如果它们匹配,用户将被验证。

Is this more inline with your thinking?这是否更符合您的想法? Is your question, how to hash it properly?你的问题是,如何正确地散列它?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM