Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can the random seed of HashCode in C# be considered cryptographically random?

The documentation of HashCode mentions that a ‘random seed’ is used that is ‘only deterministic within the scope of an operating system process’.

My questions are:

  • How is this random seed implemented? Can this random seed be considered cryptographically random?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Let’s take a look. The source for HashCode is here. We can see the line:

private static readonly uint s_seed = GenerateGlobalSeed();

So let’s take a look at GenerateGlobalSeed:

private static unsafe uint GenerateGlobalSeed()
{
    uint result;
    Interop.GetRandomBytes((byte*)&result, sizeof(uint));
    return result;
}

OK, and Interop.GetRandomBytes:

Sys.GetNonCryptographicallySecureRandomBytes(buffer, length);

Pretty big give-away there: NonCryptographicallySecureRandomBytes. This is not a cryptographic source.

If we look further at the implementation, we can see that it uses arc4random_buf or lrand48, which very definitely aren’t cryptographic.

Even if the seed was cryptographic, note that the it’s constant for an entire process. It wouldn’t be particularly hard to figure out what it is, depending on what sort of attack you’re guarding against.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading