Home > Software design >  Convert an byte array to MD5 Hash in react native
Convert an byte array to MD5 Hash in react native

Time:08-12

I have a system in C# which receives a password and this password is encrypted into a MD5 Hash using this function. I had read a lot of posts and suggestion, but I couldn't create the MD5 byte array as in C#.

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 . I need to generate the same string in 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.

Input string: 'system123' byte array: '[115, 121, 115, 116, 101, 109, 49, 50, 51]'

The React native function to generate the array of bytes.

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'. Could someone help me?

CodePudding user response:

Applying CryptoJS and assuming UTF8 encoding, the C# logic can be implemented as follows:

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). The Latin1 encoder converts the WordArray into a bytes string. In the loop, the Unicode code point value for each byte is determined as non-negative integer, converted to a string, and concatenated.

CodePudding user response:

The issue is that you use a different encoding in your C# code compared to your js code. Try to use Encoding.UTF8 instead of Encoding.Default in your code.

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

}
  • Related