Converting a C ++ Function to C #

I am trying to port the following C ++ function to C #:

QString Engine::FDigest(const QString & input)
{
    if(input.size() != 32) return "";

    int idx[] = {0xe, 0x3, 0x6, 0x8, 0x2},
        mul[] = {2, 2, 5, 4, 3},
        add[] = {0x0, 0xd, 0x10, 0xb, 0x5},
        a, m, i, t, v;

    QString b;
    char tmp[2] = { 0, 0 };

    for(int j = 0; j <= 4; j++)
    {
        a = add[j];
        m = mul[j];
        i = idx[j];

        tmp[0] = input[i].toAscii();
        t = a + (int)(strtol(tmp, NULL, 16));
        v = (int)(strtol(input.mid(t, 2).toLocal8Bit(), NULL, 16));

        snprintf(tmp, 2, "%x", (v * m) % 0x10);
        b += tmp;
    }

    return b;
}

Some of this code is easy to port, but I am having problems with this part:

tmp[0] = input[i].toAscii();
t = a + (int)(strtol(tmp, NULL, 16));
v = (int)(strtol(input.mid(t, 2).toLocal8Bit(), NULL, 16));

snprintf(tmp, 2, "%x", (v * m) % 0x10);

I found that it (int)strtol(tmp, NULL, 16)is equal int.Parse(tmp, "x")in C #, and snprintf- String.Format, however I am not sure about the rest.

How can I port this snippet to C #?

+5
source share
2 answers

Edit I have a suspicion that your code is indeed doing an MD5 input digest. The following is a snippet based on this assumption.

Translation steps

A few tips that should work well 1

Q: tmp[0] = input[i].toAscii();

bytes[] ascii = ASCIIEncoding.GetBytes(input);
tmp[0] = ascii[i];

Q: t = a + (int)(strtol(tmp, NULL, 16));

t = a + int.Parse(string.Format("{0}{1}", tmp[0], tmp[1]),
               System.Globalization.NumberStyles.HexNumber);

Q: v = (int)(strtol(input.mid(t, 2).toLocal8Bit(), NULL, 16));

toLocal8bit, Qt...

Q: snprintf(tmp, 2, "%x", (v * m) % 0x10);

{
    string tmptext = ((v*m % 16)).ToString("X2");
    tmp[0] = tmptext[0];
    tmp[1] = tmptext[1];
}

, ... MD5?

, , , :

using System;

public string FDigest(string input)
{
   MD5 md5 = System.Security.Cryptography.MD5.Create();
   byte[] ascii = System.Text.Encoding.ASCII.GetBytes (input);
   byte[] hash  = md5.ComputeHash (ascii);

   // Convert the byte array to hexadecimal string
   StringBuilder sb = new StringBuilder();
   for (int i = 0; i < hash.Length; i++)
       sb.Append (hash[i].ToString ("X2")); // "x2" for lowercase
   return sb.ToString();
}

1 , ;

+4

:

t , , . , t , . ,

tmp[0] = input[i].toAscii();
t = a + (int)(strtol(tmp, NULL, 16));

int t = a + Convert.ToInt32(input.substring(i, 1), 16); - , . ( , toAscii QString, ASCII strtol, , , .)

v = (int)(strtol(input.mid(t, 2).toLocal8Bit(), NULL, 16));

t, .. input.substring(t, 2), . v = Convert.ToInt32(input.substring(t, 2), 16); , , , , (v * a) % 0x10, . QString , toLocal8Bit , Ascii. , .

, tmp, b

snprintf(tmp, 2, "%x", (v * m) % 0x10);
b += tmp;

(2 - , , 1 ), ..

int digit = (v * m) % 0x10;
b += digit.ToString("x");

. mod 16 & 0xf, , .

, i - , -, ?

,

int t = a + Convert.ToInt32(input.substring(i, 1), 16);
int v = Convert.ToInt32(input.substring(t, 2), 16);
int nextDigit = (v * m) & 0xf;
b += nextDigit.ToString("x");
0

All Articles