コード例 #1
0
ファイル: HashTest.cs プロジェクト: archi-Doc/Arc.Crypto
    public void QuickStart()
    {
        var   data = new byte[100];
        ulong hash64;
        uint  hash32;

        byte[] array;

        hash64 = Arc.Crypto.FarmHash.Hash64(data.AsSpan());                   // The fastest and best algorithm.
        hash64 = Arc.Crypto.XxHash64.Hash64(data.AsSpan());                   // As fast as FarmHash.
        hash32 = Arc.Crypto.FarmHash.Hash32(data.AsSpan());                   // 32 bit version is slower than 64 bit version.
        hash32 = Arc.Crypto.XXHash32.Hash32(data.AsSpan());                   // Same as above.
        hash32 = unchecked ((uint)Arc.Crypto.FarmHash.Hash64(data.AsSpan())); // I recommend getting 64 bit and discarding half.
        hash32 = Arc.Crypto.Adler32.Hash32(data.AsSpan());                    // Slow
        hash32 = Arc.Crypto.Crc32.Hash32(data.AsSpan());                      // Slowest

        // IHash is an interface to get a hash of large data.
        // For XxHash64, IHash version is a bit slower than static method version.
        // For FarmHash64, IHash version is twice as slow. XxHash64 is recommended.
        var ihash = new Arc.Crypto.XxHash64();

        ihash.HashInitialize();
        ihash.HashUpdate(data);
        Assert.True(ihash.HashFinal().SequenceEqual(ihash.GetHash(data)));

        // Secure Hash Algorithm (SHA1, SHA2, SHA3 supported)
        var sha3_512 = new Arc.Crypto.Sha3_512();

        array = sha3_512.GetHash(data.AsSpan());

        sha3_512.HashInitialize(); // Another way
        sha3_512.HashUpdate(data.AsSpan());
        Assert.True(sha3_512.HashFinal().SequenceEqual(array));
    }
コード例 #2
0
ファイル: HashTest.cs プロジェクト: archi-Doc/Arc.Crypto
    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);
    }