简体   繁体   English

生成字节数组的 Base64 MD5 哈希

[英]Generate Base64 MD5 hash of byte array

I have following security encoding implemented in my c# web api:我在我的 c# web api 中实现了以下安全编码:

string testStr = "test";
ASCIIEncoding encoding = new ASCIIEncoding();    //using System.Text;
byte[] byteData = encoding.GetBytes(testStr);

MD5 md5 = MD5.Create();    //using System.Security.Cryptography;
string hash = md5.ComputeHash(byteData);
string md5Base64 = Convert.ToBase64String(hash);

I bind this md5Base64 string in header and compare it in API request.我将这个md5Base64字符串绑定在标头中并在 API 请求中进行比较。 This works fine when I hit the API from C# code.当我从 C# 代码点击 API 时,这很好用。 Now I need to use it in javascript, so will need js equivalent of above code.现在我需要在 javascript 中使用它,因此需要与上述代码等效的 js。

I have tried following but it is giving different output:我试过以下,但它给出了不同的输出:

var testStr = 'test';
var byteData = testStr.split ('').map(function (c) { return c.charCodeAt (0); });
var hash = MD5(value.join(','));
var md5Base64 = btoa(hash);

the MD5 function used here is from https://stackoverflow.com/a/33486055/7519287这里使用的MD5函数来自https://stackoverflow.com/a/33486055/7519287

Please let me know what is wrong here.请让我知道这里有什么问题。

The problem with your JavaScript code is that you're doing unnecessary conversions: MD5 already takes a string.您的 JavaScript 代码的问题在于您进行了不必要的转换: MD5已经接受了一个字符串。 Furthermore, more conversions after hashing are required.此外,散列后需要更多的转换。

If we have the following C# code:如果我们有以下 C# 代码:

string tmp = "test";
byte[] bTmp = System.Text.Encoding.UTF8.GetBytes(tmp);
byte[] hashed = null;
using (System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider())
{
    hashed = md5.ComputeHash(bTmp);
}

Console.WriteLine(Convert.ToBase64String(hashed));

Fiddle小提琴

then the equivalent JavaScript code is:那么等效的 JavaScript 代码是:

var tmp = 'test';
var hashed = hex2a(MD5(tmp)); // md5 src: https://stackoverflow.com/a/33486055/7519287

// src: https://stackoverflow.com/a/3745677/3181933
function hex2a(hexx) {
    var hex = hexx.toString();//force conversion
    var str = '';
    for (var i = 0; i < hex.length; i += 2)
        str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
    return str;
}

alert(btoa(hashed));

Fiddle小提琴

Because MD5 returns a hex string, you have to convert that to ASCII before you can base64 encode it.因为 MD5 返回一个十六进制字符串,所以您必须先将其转换为 ASCII,然后才能对其进行 base64 编码。 I wonder if you need base64 encoding?我想知道你是否需要base64编码? MD5 is usually represented as a hex string. MD5 通常表示为十六进制字符串。 Perhaps on the C# side, instead of Convert.ToBase64String(hashed) , you could use BitConverter.ToString(hashed).Replace("-", "") to get a hex string for the MD5 hash?也许在 C# 方面,您可以使用BitConverter.ToString(hashed).Replace("-", "")来获取 MD5 哈希的十六进制字符串,而不是Convert.ToBase64String(hashed) Then you could simply just use MD5(tmp) in JavaScript.然后你可以简单地在 JavaScript 中使用MD5(tmp)

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

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