public static void Blake2Prime(Argon2Memory memory, LittleEndianActiveStream dataStream, int size = -1)
        {
            var hashStream = new LittleEndianActiveStream();

            if (size < 0 || size > (memory.Length * 8))
            {
                size = memory.Length * 8;
            }

            hashStream.Expose(size);
            hashStream.Expose(dataStream);


            if (size <= 64)
            {
                var blake2 = new HMACBlake2B(8 * size);
                blake2.Initialize();

                memory.Blit(blake2.ComputeHash(hashStream), 0, 0, size);
            }
            else
            {
                var blake2 = new HMACBlake2B(512);
                blake2.Initialize();

                int offset = 0;
                var chunk  = blake2.ComputeHash(hashStream);

                memory.Blit(chunk, offset, 0, 32); // copy half of the chunk
                offset += 4;
                size   -= 32;

                while (size > 64)
                {
                    blake2.Initialize();
                    chunk = blake2.ComputeHash(chunk);
                    memory.Blit(chunk, offset, 0, 32); // half again

                    offset += 4;
                    size   -= 32;
                }

                blake2 = new HMACBlake2B(size * 8);
                blake2.Initialize();
                memory.Blit(blake2.ComputeHash(chunk), offset, 0, size); // copy the rest
            }
        }
Пример #2
0
        public void AllocationIsManagedAppropriately()
        {
            LittleEndianActiveStream stream = new LittleEndianActiveStream();

            stream.Expose((uint)0x01010101);
            stream.Expose((uint)0x30303030);
            stream.Expose(new byte[] { 0x45, 0x61, 0xac, 0x4c, 0xf0, 0x00, 0x0b });

            byte[] buffer = new byte[5];
            Assert.Equal(5, stream.Read(buffer, 0, 5));
            Assert.Equal(new byte[] { 0x1, 0x1, 0x1, 0x1, 0x30 }, buffer);

            Assert.Equal(5, stream.Read(buffer, 0, 5));
            Assert.Equal(new byte[] { 0x30, 0x30, 0x30, 0x45, 0x61 }, buffer);

            Assert.Equal(5, stream.Read(buffer, 0, 5));
            Assert.Equal(new byte[] { 0xac, 0x4c, 0xf0, 0x00, 0x0b }, buffer);
        }
Пример #3
0
        public void CrossesBoundariesAsExpected()
        {
            LittleEndianActiveStream stream = new LittleEndianActiveStream();

            stream.Expose((uint)0x01010101);
            stream.Expose((uint)0x30303030);
            stream.Expose((uint)0x0a0a0a0a);
            stream.Expose(new byte[] { 0x45, 0x61 });

            byte[] buffer = new byte[5];
            Assert.Equal(5, stream.Read(buffer, 0, 5));
            Assert.Equal(new byte[] { 0x1, 0x1, 0x1, 0x1, 0x30 }, buffer);

            Assert.Equal(5, stream.Read(buffer, 0, 5));
            Assert.Equal(new byte[] { 0x30, 0x30, 0x30, 0x0a, 0x0a }, buffer);

            Assert.Equal(4, stream.Read(buffer, 0, 5));
            Assert.Equal(new byte[] { 0x0a, 0x0a, 0x45, 0x61 }, buffer.Take(4));
        }
Пример #4
0
        public void EndsWhereLastByteAvailable()
        {
            LittleEndianActiveStream stream = new LittleEndianActiveStream();

            stream.Expose((ushort)0x4501);
            byte[] buffer = new byte[4];

            Assert.Equal(2, stream.Read(buffer, 0, 4));
            Assert.Equal(0x01, buffer[0]);
            Assert.Equal(0x45, buffer[1]);
        }