SHA512 hash with specified number of transformations in C#

Pavel Matras 1 Reputation point
2022-12-08T13:17:37.23+00:00

I need to create SHA512 from string:

20191018143572123034102012221314181237774212

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

When I am going on 1 iteration, I have same result as on web. But from 2 or more iteration, 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 = System.Text.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;  
    }        
}  
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,399 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Pavel Matras 1 Reputation point
    2022-12-09T09:37:29.24+00:00

    Yes, and it's not correct, because on web calculator second iteration is:

    b5857dcdbc756f3a9dd3ffe0ee22714e69befc8d0a47097151edd4c72a238604428bb45ca381edcb577fbc6468f48cc142383fb92541b651c04247f3bfdda08f  
    

    I already found solution, between iterations it should not stay in bytes, but must be converter back to string, and then again to bytes, and then start next iteration

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.