Pavel Matras

Pavel Matras

  • NA
  • 15
  • 613

SHA512 hash with specified number of transformations in C#

Dec 8 2022 1:55 PM

I need to create SHA512 from string:

20191018143572123034102012221314181237774212

And I need same result as it is on this web calculator: podatki.gov.pl/kalkulator-sha-512
Where:
NIP: 1435721230
Numer konta: 34 1020 1222 1314 1812 3777 4212
Wprowadz date: 18-10-2019

When I am going on 1 iteration, I have same result as on web. But from 2 or more iterations, my results are different.
I am trying to use built-in SHA512 class, or BouncyCastle package, but with no luck.
Please how should I prepare data for second and the next iterations in my program?

        static void Main(string[] args)
        {            
            string puvodni = "20191018143572123034102012221314181237774212";
            Console.WriteLine("Puvodni: " + puvodni);

            //varianta 1
            SHA512 sha512 = SHA512.Create();
            byte[] hash = Encoding.UTF8.GetBytes(puvodni);
            for (int i = 0; i < 2; i++)
            {
                hash=sha512.ComputeHash(hash);
                string result = BitConverter.ToString(hash).Replace("-", "");
                Console.WriteLine(i.ToString() + ". Hash: " + result);
            }

            //varianta 2
            byte[] bytes = Encoding.UTF8.GetBytes(puvodni);
            Org.BouncyCastle.Crypto.Digests.Sha512Digest digester = new Org.BouncyCastle.Crypto.Digests.Sha512Digest();
            for (int i = 0; i < 2; i++)
            {                
                byte[] retValue = new byte[digester.GetDigestSize()];
                digester.BlockUpdate(bytes, 0, bytes.Length);
                digester.DoFinal(retValue, 0);
                string result1 = BitConverter.ToString(retValue).Replace("-", "");
                Console.WriteLine("BouncyCastle Hash: " + i.ToString() + ". " + result1);
                bytes=retValue;
            }      
        }