C#SHA256 ComputeHash导致与CryptoJS SHA256功能不同 [英] C# SHA256 ComputeHash result different with CryptoJS SHA256 function

查看:2377
本文介绍了C#SHA256 ComputeHash导致与CryptoJS SHA256功能不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下一个C#功能:

I have a C# function as below:

string stringvalue = "530500480530490480530480480520570480520510500490";
var encodedvalue= Encoding.Unicode.GetBytes(stringvalue);
using (HashAlgorithm ssp = System.Security.Cryptography.HashAlgorithm.Create("SHA256"))
        {

            var digest = ssp.ComputeHash(encodedvalue);

            return BitConverter.ToString(digest); 

        }



我需要创建代码匹配上面的JavaScript函数使得用于C#和JS最终结果是一样的。

I need to create a javascript function that match the code above so that the end result for both C# and JS is the same.

在我的JS代码现在,我使用这个:

Currently in my JS code, I'm using this:

var hash = CryptoJS.SHA256("530500480530490480530480480520570480520510500490");
var hexhash = hash.toString(CryptoJS.enc.hex);

这是我的hexhash的结果:

This is the result of my hexhash:

d956678c8f12c65299daf35019a9a1eb3e6eb9855fd850aeb5aafe46057d179e

但在我的C#代码,这行 VAR消化= ssp.ComputeHash(bPass); 返回下面的数组:

But in my C# code, this line of var digest = ssp.ComputeHash(bPass); return the following array:

我不知道很多关于编码。请告诉我被填充在上面的C#代码是什么类型的结果?如果我没有记错的话,在ComputeHash将返回字节,但我需要大量的阅读,以确认这是学习

I don't know much about encoding. Please tell me what type of result is being populated in the c# code above? If I'm not mistaken, the ComputeHash is returning bytes but I need lots of reading to confirm that which is another long hour of studying

我试过转换的多种不同方式的再长小时在JS代码,SHA256,但没有运气。我被困在这个特殊行几乎一天。

I tried many different ways of converting the JS Sha256 code but no luck. I'm stuck at this particular line for almost a day.

请帮忙。谢谢

编辑:
很抱歉的代码错误。我已经更新了C#代码。 ComputeHash接受阵列

Sorry for the code error. I had updated the C# code. ComputeHash accept an array

推荐答案

我终于找到了试验和错误的不可数小时后答案。

I finally found the answer after uncountable hours of trial and error.

C#代码 VAR消化= ssp.ComputeHash(encodedvalue)是从返回的结果字节数组 VAR encodedvalue = Encoding.Unicode.GetBytes(字符串值); 吉恩回答。为了创建在Javascript功能,我需要确保encodedvalue是生产,就像​​一个在C#中的正确的编码格式和大小。

The C# code var digest = ssp.ComputeHash(encodedvalue) is returning byte array from the result of var encodedvalue= Encoding.Unicode.GetBytes(stringvalue); as Jean replied. In order to create the function in Javascript, I need to ensure that the encodedvalue is producing the correct encoding format and size just like the one in C#.

仅使用CryptoJS,我设法从下面

Using only CryptoJS, I manage to get the matching result from below

function GetHexFromString() {
var stringVal = '8563A578-7402-4567-A6CE-4DE4E0825B021234';
// Convert the string to UTF 16 little-endian
// Result: 560530540510650530550560450550520480500450520530540550450650540670690450520680690520690480560500530660480500490500510520
var utf16le = CryptoJS.enc.Utf16LE.parse(stringVal);  

// Convert to Sha256 format and get the word array
var utf16Sha256 = CryptoJS.SHA256(utf16le);
// Convert the Sha256 word array to Uint8Array to get the 32 byte array just to see the result to ensure it match with the C# function
// Result: 94,203,69,29,35,202,209,149,121,144,44,6,98,250,141,161,102,7,238,35,228,117,111,236,118,115,51,113,134,72,52,69
var utf16sha256Array = convertWordArrayToUint8Array(utf16Sha256); 

// Convert the Sha256 to hex (if i'm not mistaken, it's base 16) format
var hexSha256 = utf16Sha256.toString(CryptoJS.enc.hex);

 // Insert a dash in between 2 characters in the string
 hexSha256 = hexSha256.replace(/(\S{2})/g, "$1-");
 // Remove the last dash in the string
 hexSha256 = hexSha256.replace(/-$/, "");

// Final Result: 5E-CB-45-1D-23-CA-D1-95-79-90-2C-06-62-FA-8D-A1-66-07-EE-23-E4-75-6F-EC-76-73-33-71-86-48-34-45
return hexSha256.toUpperCase();
}

function convertWordArrayToUint8Array(wordArray) {
        var len = wordArray.words.length,
            u8_array = new Uint8Array(len << 2),
            offset = 0, word, i
        ;
        for (i = 0; i < len; i++) {
            var word = wordArray.words[i];                

            u8_array[offset++] = word >> 24;
            u8_array[offset++] = (word >> 16) & 0xff;
            u8_array[offset++] = (word >> 8) & 0xff;
            u8_array[offset++] = word & 0xff;                                              
        }
        return u8_array;
    }



希望它能帮助无论是谁需要这样的方法

Hope it help whoever that need such method

这篇关于C#SHA256 ComputeHash导致与CryptoJS SHA256功能不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆