Exemple #1
0
    public void TestHashUpdate()
    {
        const int N      = 1_000_000;
        var       random = new Random(42);
        var       data   = new byte[N];

        random.NextBytes(data);

        // CRC-32
        var crc32 = new Crc32();

        for (var n = 0; n < 1000; n++)
        {
            var span = data.AsSpan(0, n);
            var h    = BitConverter.ToUInt32(crc32.GetHash(span));
            var h2   = Crc32.Hash32(span);
            Assert.Equal(h, h2);
        }

        this.TestHashUpdate_do(crc32, data, random);

        // Adler-32
        var adler32 = new Adler32();

        for (var n = 0; n < 1000; n++)
        {
            var span = data.AsSpan(0, n);
            var h    = BitConverter.ToUInt32(adler32.GetHash(span));
            var h2   = Adler32.Hash32(span);
            Assert.Equal(h, h2);
        }

        this.TestHashUpdate_do(adler32, data, random);

        // FarmHash
        var farm = new FarmHash();

        for (var n = 0; n < 1000; n++)
        {
            var span = data.AsSpan(0, n);
            var h    = BitConverter.ToUInt64(farm.GetHash(span));
            var h2   = FarmHash.Hash64(span);
            Assert.Equal(h, h2);
        }

        this.TestHashUpdate_do(farm, data, random);

        // xxHash32
        var xxh32 = new XXHash32();

        for (var n = 0; n < 1000; n++)
        {
            var span = data.AsSpan(0, n);
            var h    = BitConverter.ToUInt32(xxh32.GetHash(span));
            var h2   = XXHash32.Hash32(span);
            Assert.Equal(h, h2);
        }

        this.TestHashUpdate_do(xxh32, data, random);

        // xxHash64
        var xxh64 = new XxHash64();

        for (var n = 0; n < 1000; n++)
        {
            var span = data.AsSpan(0, n);
            var h    = BitConverter.ToUInt64(xxh64.GetHash(span));
            var h2   = XxHash64.Hash64(span);
            Assert.Equal(h, h2);
        }

        this.TestHashUpdate_do(xxh64, data, random);

        // Sha1
        using var sha1 = new Arc.Crypto.Sha1();
        this.TestHashUpdate_do(sha1, data, random);

        // Sha2_256
        using var sha2_256 = new Arc.Crypto.Sha2_256();
        this.TestHashUpdate_do(sha2_256, data, random);

        // Sha2_384
        using var sha2_384 = new Arc.Crypto.Sha2_384();
        this.TestHashUpdate_do(sha2_384, data, random);

        // Sha2_512
        using var sha2_512 = new Arc.Crypto.Sha2_512();
        this.TestHashUpdate_do(sha2_512, data, random);

        // Sha3_256
        var sha3_256 = new Arc.Crypto.Sha3_256();

        this.TestHashUpdate_do(sha3_256, data, random);

        // Sha3_384
        var sha3_384 = new Arc.Crypto.Sha3_384();

        this.TestHashUpdate_do(sha3_384, data, random);

        // Sha3_512
        var sha3_512 = new Arc.Crypto.Sha3_512();

        this.TestHashUpdate_do(sha3_512, data, random);
    }