0

I'm trying to convert a hex string into a byte array to test a SHA2-224 using the test vector from NIST. See: h**ps://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA2_Additional.pdf

The code appears to be correct when testing a string using (Encoding.UTF8.GetBytes) and even correct when testing a hex byte using (StringToByteArrayFastest) to convert. However, when I use the C style hex (0xe5e09924) the results are not as expected. It's only when I removed the 0x (e5e09924) and convert the hex to a byte array that I receive the expected results.

        public static string ComputeSha224Test1(string strHex)
    {
        // WORKS
        // #2) 4 bytes 0xe5e09924
        // fd19e746 90d29146 7ce59f07 7df31163 8f1c3a46 e510d0e4 9a67062d
        //
        Sha224Digest hash224Test = new Sha224Digest();
        // Convert to hex byte to test NIST vector
        byte[] byte3 = StringToByteArrayFastest(strHex);
        // Encode string for hashing
        //byte[] byte3 = Encoding.UTF8.GetBytes(strHex);
        hash224Test.BlockUpdate(byte3, 0, byte3.Length);
        byte[] result = new byte[hash224Test.GetDigestSize()];
        hash224Test.DoFinal(result, 0);
        String Hash = BitConverter.ToString(result).Replace("-", "").ToUpper();
        return Hash;
        //
    }

Is there any way to convert a C style hex into a C# style hex?

I got the (StringToByteArrayFastest) code from (How can I convert a hex string to a byte array?) and I'm using Visual Studio 2015.

Thank you.

DemarcPoint
  • 183
  • 1
  • 9
  • `0x` is just a prefix that tells the C# compiler to expect a hex literal in your source code. It serves no other purpose, which is why removing it solves your problem. – Robert Harvey Apr 29 '20 at 02:05
  • Thank you Robert. If I was thinking I would have realized that because Hex characters do not contain an "x". So I should just be able to use the string replace. string strRemove = hexValues.Replace("0x",""); – DemarcPoint May 01 '20 at 13:30

0 Answers0