简体   繁体   English

在本机反应中将字节数组转换为 MD5 Hash

[英]Convert an byte array to MD5 Hash in react native

I have a system in C# which receives a password and this password is encrypted into a MD5 Hash using this function.我在 C# 中有一个系统,它接收密码,并且使用此 function 将此密码加密为 MD5 Hash。 I had read a lot of posts and suggestion, but I couldn't create the MD5 byte array as in C#.我已经阅读了很多帖子和建议,但我无法像在 C# 中那样创建 MD5 字节数组。

public static string GetMD5HashData(string data)
{
    //create new instance of md5
    MD5 md5 = MD5.Create();

    //convert the input text to array of bytes
    byte[] hashData = md5.ComputeHash(Encoding.Default.GetBytes(data));

    //create new instance of StringBuilder to save hashed data
    StringBuilder returnValue = new StringBuilder();

    //loop for each byte and add it to StringBuilder
    for (int i = 0; i < hashData.Length; i++)
    {
        returnValue.Append(hashData[i].ToString());
    }

    // return hexadecimal string
    return returnValue.ToString();
}

The return of this function is this string 207154234292557519022585191701391052252168 .这个 function 的返回是这个字符串207154234292557519022585191701391052252168 I need to generate the same string in React Native.我需要在 React Native 中生成相同的字符串。 This part Encoding.Default.GetBytes(data) in the C# function I've reproduced in React native, so both C# and React native return the same array of bytes from the input string . C# function 中的这部分Encoding.Default.GetBytes(data)我已经在 React native 中重现,因此 C# 和 React native 从输入字符串返回相同的字节数组

Input string: 'system123' byte array: '[115, 121, 115, 116, 101, 109, 49, 50, 51]'输入字符串:'system123' 字节数组:'[115, 121, 115, 116, 101, 109, 49, 50, 51]'

The React native function to generate the array of bytes. React 原生 function 生成字节数组。

convertStringToByteArray = (str) =>{
    var bufferedVal = Buffer.from(str, 'utf8').toString('hex');
    String.prototype.encodeHex = function () {
        var bytes = [];
        for (var i = 0; i < this.length; ++i) {
            bytes.push(this.charCodeAt(i));
        }
    
        return bytes;
    };
   
    var byteArray = str.encodeHex();
    return byteArray;
};

I've tried some libs like crypto-js for react-native to create the MD5 hash, but could not generate the same value as C# ' 207154234292557519022585191701391052252168 '. I've tried some libs like crypto-js for react-native to create the MD5 hash, but could not generate the same value as C# ' 207154234292557519022585191701391052252168 '. Could someone help me?有人可以帮助我吗?

Applying CryptoJS and assuming UTF8 encoding, the C# logic can be implemented as follows:应用 CryptoJS 并假设 UTF8 编码,C# 逻辑可以实现如下:

 var result = ''; var hashBytes = CryptoJS.MD5('system123').toString(CryptoJS.enc.Latin1); for (var i = 0; i < hashBytes.length; i++) result += hashBytes.codePointAt(i).toString(); console.log(result); // 207154234292557519022585191701391052252168
 <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>

Explanation: CryptoJS.MD5() implicitly performs a UTF-8 encoding since the data is passed as string ( here ).说明: CryptoJS.MD5()隐式执行 UTF-8 编码,因为数据作为字符串传递(此处)。 The Latin1 encoder converts the WordArray into a bytes string. Latin1 编码器WordArray转换为字节字符串。 In the loop, the Unicode code point value for each byte is determined as non-negative integer, converted to a string, and concatenated.在循环中,每个字节的 Unicode 代码点值被确定为非负 integer,转换为字符串,并连接起来。

The issue is that you use a different encoding in your C# code compared to your js code.问题在于,与 js 代码相比,您在 C# 代码中使用了不同的编码。 Try to use Encoding.UTF8 instead of Encoding.Default in your code.尝试在您的代码中使用Encoding.UTF8而不是Encoding.Default

public static string GetMD5HashData(string data)
{
    //create new instance of md5
    MD5 md5 = MD5.Create();

    //convert the input text to array of bytes
    byte[] hashData = md5.ComputeHash(Encoding.UTF8.GetBytes(data));

    //create new instance of StringBuilder to save hashed data
    StringBuilder returnValue = new StringBuilder();

    //loop for each byte and add it to StringBuilder
    for (int i = 0; i < hashData.Length; i++)
    {
        returnValue.Append(hashData[i].ToString());
    }

    // return hexadecimal string
    return returnValue.ToString();

}

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

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