示例#1
0
    public static int Compare <T>(IList <T> source, int sourceIndex, IList <T> destination, int destinationIndex, int length)
    {
        if (source is byte[] x && destination is byte[] y)
        {
            if ((source.Count - sourceIndex) < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(sourceIndex));
            }
            if ((destination.Count - destinationIndex) < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(destinationIndex));
            }
            if (length > (source.Count - sourceIndex))
            {
                throw new ArgumentOutOfRangeException(nameof(length));
            }
            if (length > (destination.Count - destinationIndex))
            {
                throw new ArgumentOutOfRangeException(nameof(length));
            }

            return(BytesOperations.Compare(x.AsSpan(sourceIndex, length), y.AsSpan(destinationIndex, length)));
        }

        var compare = Comparer <T> .Default;

        return(CollectionHelper.Compare(source, sourceIndex, destination, destinationIndex, length, compare));
    }
示例#2
0
    public static bool Equals <T>(IList <T> source, int sourceIndex, IList <T> destination, int destinationIndex, int length)
    {
        if (source is byte[] x && destination is byte[] y)
        {
            if ((x.Length - sourceIndex) < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(sourceIndex));
            }
            if ((y.Length - destinationIndex) < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(destinationIndex));
            }
            if (length > (x.Length - sourceIndex))
            {
                throw new ArgumentOutOfRangeException(nameof(length));
            }
            if (length > (y.Length - destinationIndex))
            {
                throw new ArgumentOutOfRangeException(nameof(length));
            }

            return(BytesOperations.Equals(x.AsSpan(sourceIndex, length), y.AsSpan(destinationIndex, length)));
        }

        var equalityComparer = EqualityComparer <T> .Default;

        return(Equals(source, sourceIndex, destination, destinationIndex, length, equalityComparer));
    }
示例#3
0
        public async Task Encode(ReadOnlyMemory<byte>[] sources, int[] index, Memory<byte>[] repairs, int packetLength, int concurrency = 1, CancellationToken token = default)
        {
            if (sources == null) throw new ArgumentNullException(nameof(sources));
            if (repairs == null) throw new ArgumentNullException(nameof(repairs));
            if (index == null) throw new ArgumentNullException(nameof(index));

            await Enumerable.Range(0, repairs.Length).ForEachAsync(row =>
            {
                return Task.Run(() =>
                {
                    token.ThrowIfCancellationRequested();

                    // *remember* indices start at 0, k starts at 1.
                    if (index[row] < _k)
                    {
                        // < k, systematic so direct copy.
                        BytesOperations.Copy(sources[index[row]].Span, repairs[row].Span, packetLength);
                    }
                    else
                    {
                        // index[row] >= k && index[row] < n
                        int pos = index[row] * _k;
                        BytesOperations.Zero(repairs[row].Span.Slice(packetLength));

                        for (int col = 0; col < _k; col++)
                        {
                            token.ThrowIfCancellationRequested();

                            ReadSolomonMath.AddMul(sources[col].Span, repairs[row].Span, _encMatrix[pos + col], packetLength);
                        }
                    }
                });
            }, concurrency, token, false);
        }
示例#4
0
        public static byte[] Encode(IEnumerable <ReadOnlyMemory <byte> > collection)
        {
            var pipe = new Pipe();

            {
                foreach (var value in collection)
                {
                    Varint.SetUInt64((uint)value.Length, pipe.Writer);
                    BytesOperations.Copy(value.Span, pipe.Writer.GetSpan(value.Length), value.Length);
                    pipe.Writer.Advance(value.Length);
                }

                pipe.Writer.Complete();
            }

            byte[] result;
            {
                pipe.Reader.TryRead(out var readResult);

                result = readResult.Buffer.ToArray();

                pipe.Reader.Complete();
            }

            return(result);
        }
示例#5
0
        public static int Compare <T>(IList <T> source, int sourceIndex, IList <T> destination, int destinationIndex, int length)
        {
            {
                var x = source as byte[];
                var y = destination as byte[];

                if (x != null && y != null)
                {
                    if (0 > (source.Count - sourceIndex))
                    {
                        throw new ArgumentOutOfRangeException(nameof(sourceIndex));
                    }
                    if (0 > (destination.Count - destinationIndex))
                    {
                        throw new ArgumentOutOfRangeException(nameof(destinationIndex));
                    }
                    if (length > (source.Count - sourceIndex))
                    {
                        throw new ArgumentOutOfRangeException(nameof(length));
                    }
                    if (length > (destination.Count - destinationIndex))
                    {
                        throw new ArgumentOutOfRangeException(nameof(length));
                    }

                    return(BytesOperations.Compare(x.AsSpan(sourceIndex, length), y.AsSpan(destinationIndex, length)));
                }
            }

            {
                var compare = Comparer <T> .Default;
                return(CollectionHelper.Compare(source, sourceIndex, destination, destinationIndex, length, compare));
            }
        }
示例#6
0
    public static IEnumerable <KademliaElement <T> > Search <T>(ReadOnlySpan <byte> baseId, ReadOnlySpan <byte> targetId, IEnumerable <KademliaElement <T> > elements, int count)
        where T : notnull
    {
        if (elements is null)
        {
            throw new ArgumentNullException(nameof(elements));
        }

        if (count == 0)
        {
            Array.Empty <KademliaElement <T> >();
        }

        var targetList = new List <SortEntry <T> >();

        if (baseId != null)
        {
            var xor = new byte[targetId.Length];
            BytesOperations.Xor(targetId, baseId, xor);
            targetList.Add(new SortEntry <T>(null, xor));
        }

        foreach (var node in elements)
        {
            var xor = new byte[targetId.Length];
            BytesOperations.Xor(targetId, node.Id.Span, xor);
            targetList.Add(new SortEntry <T>(node, xor));
        }

        for (int i = 1; i < targetList.Count; i++)
        {
            var temp = targetList[i];

            int left  = 0;
            int right = Math.Min(i, count);

            while (left < right)
            {
                int middle = (left + right) / 2;

                if (BytesOperations.Compare(targetList[middle].Xor, temp.Xor) <= 0)
                {
                    left = middle + 1;
                }
                else
                {
                    right = middle;
                }
            }

            for (int j = Math.Min(i, count); left < j; --j)
            {
                targetList[j] = targetList[j - 1];
            }

            targetList[left] = temp;
        }

        return(targetList.Take(count).TakeWhile(n => n.Node.HasValue).Select(n => n.Node !.Value).ToList());
    }
示例#7
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }
            if (offset < 0 || buffer.Length < offset)
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }
            if (count < 0 || (buffer.Length - offset) < count)
            {
                throw new ArgumentOutOfRangeException(nameof(count));
            }

            if (count == 0)
            {
                return(0);
            }

            if (_blockIsUpdated)
            {
                this.Flush();
            }

            count = (int)Math.Min(count, _length - _position);

            int readSumLength = 0;

            while (count > 0)
            {
                long p = (_position / SectorSize) * SectorSize;

                if (_blockPosition != p)
                {
                    _blockPosition = p;
                    _blockOffset   = 0;

                    _stream.Seek(_blockPosition, SeekOrigin.Begin);

                    int readLength = _stream.Read(_blockBuffer, 0, SectorSize);
                    readLength = (int)Math.Min(_length - _blockPosition, readLength);

                    _blockCount = readLength;
                }

                int blockReadPosition = (int)(_position - p);
                int length            = Math.Min(SectorSize - blockReadPosition, count);
                BytesOperations.Copy(_blockBuffer.AsSpan(blockReadPosition), buffer.AsSpan(offset), length);

                offset += length;
                count  -= length;

                _position += length;

                readSumLength += length;
            }

            return(readSumLength);
        }
示例#8
0
            private static Stream RemoveHash(Stream stream)
            {
                if (stream == null)
                {
                    throw new ArgumentNullException(nameof(stream));
                }

                int type = (int)Varint.GetUInt64(stream);

                if (type == (int)ConvertHashAlgorithm.Sha256)
                {
                    var value = new byte[32];
                    stream.Read(value, 0, value.Length);

                    var dataStream = new RangeStream(stream, true);
                    if (!BytesOperations.SequenceEqual(value, Sha256.Compute(new WrapperStream(dataStream))))
                    {
                        throw new ArgumentException("Hash");
                    }

                    dataStream.Seek(0, SeekOrigin.Begin);
                    return(dataStream);
                }
                else
                {
                    throw new NotSupportedException();
                }
            }
    public void WriteAndReadTest()
    {
        var random = new Random(0);

        var cases = new int[] {
            0,
            1,
            10,
            100,
            random.Next(0, 1024 * 64),
            random.Next(0, 1024 * 64),
            random.Next(0, 1024 * 64),
            random.Next(0, 1024 * 64),
        };

        foreach (var length in cases)
        {
            var inBody = new byte[length];
            random.NextBytes(inBody);

            Assert.True(TryEncode(inBody, out var message));
            Assert.True(TryDecode(message !, out var outBody));

            Assert.True(BytesOperations.Equals(inBody.AsSpan(), outBody.AsSpan()));
        }
    }
示例#10
0
        public static uint Verify_Simple_Sha2_256(ReadOnlySpan <byte> key, ReadOnlySpan <byte> value)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }
            if (key.Length != 32)
            {
                throw new ArgumentOutOfRangeException(nameof(key));
            }
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }
            if (value.Length != 32)
            {
                throw new ArgumentOutOfRangeException(nameof(value));
            }

            Span <byte> buffer = stackalloc byte[64];

            byte[] hash;
            {
                BytesOperations.Copy(key, buffer, key.Length);
                BytesOperations.Copy(value, buffer[key.Length..], value.Length);
示例#11
0
 public void CompareTest()
 {
     Assert.True(BytesOperations.Compare(new byte[] { 0, 1, 2, 3, 4 }, new byte[] { 0, 1, 2, 3, 4 }) == 0);
     Assert.True(BytesOperations.Compare(new byte[] { 0, 1, 2, 3, 4 }, new byte[] { 0, 1, 2, 3, 5 }) < 0);
     Assert.True(BytesOperations.Compare(new byte[] { 0, 1, 2, 3, 4 }, new byte[] { 0, 1, 2, 3, 3 }) > 0);
     Assert.True(BytesOperations.Compare(new byte[] { 0, 1, 2, 3, 4 }, new byte[] { 0, 1, 2, 5 }) > 0);
     Assert.True(BytesOperations.Compare(new byte[] { 0, 1, 2, 3, 4 }, new byte[] { 0, 1, 2, 0, 0, 0 }) < 0);
 }
示例#12
0
    public static bool IsGlobalIpAddress(IPAddress ipAddress)
    {
        if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
        {
            if (ipAddress == IPAddress.Any || ipAddress == IPAddress.Loopback || ipAddress == IPAddress.Broadcast)
            {
                return(false);
            }

            var bytes = ipAddress.GetAddressBytes();

            // Loopback Address
            if (BytesOperations.Compare(bytes, _ipAddress_127_0_0_0.Span) >= 0 &&
                BytesOperations.Compare(bytes, _ipAddress_127_255_255_255.Span) <= 0)
            {
                return(false);
            }

            // Class A
            if (BytesOperations.Compare(bytes, _ipAddress_10_0_0_0.Span) >= 0 &&
                BytesOperations.Compare(bytes, _ipAddress_10_255_255_255.Span) <= 0)
            {
                return(false);
            }

            // Class B
            if (BytesOperations.Compare(bytes, _ipAddress_172_16_0_0.Span) >= 0 &&
                BytesOperations.Compare(bytes, _ipAddress_172_31_255_255.Span) <= 0)
            {
                return(false);
            }

            // Class C
            if (BytesOperations.Compare(bytes, _ipAddress_192_168_0_0.Span) >= 0 &&
                BytesOperations.Compare(bytes, _ipAddress_192_168_255_255.Span) <= 0)
            {
                return(false);
            }

            // Link Local Address
            if (BytesOperations.Compare(bytes, _ipAddress_169_254_0_0.Span) >= 0 &&
                BytesOperations.Compare(bytes, _ipAddress_169_254_255_255.Span) <= 0)
            {
                return(false);
            }
        }

        if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
        {
            if (ipAddress == IPAddress.IPv6Any || ipAddress == IPAddress.IPv6Loopback || ipAddress == IPAddress.IPv6None ||
                ipAddress.IsIPv4MappedToIPv6 || ipAddress.IsIPv6LinkLocal || ipAddress.IsIPv6Multicast || ipAddress.IsIPv6SiteLocal || ipAddress.IsIPv6Teredo)
            {
                return(false);
            }
        }

        return(true);
    }
示例#13
0
        public void EqualsTest()
        {
            var random = new Random();

            Assert.True(BytesOperations.SequenceEqual(new byte[] { 0, 1, 2, 3, 4 }, new byte[] { 0, 1, 2, 3, 4 }));
            Assert.False(BytesOperations.SequenceEqual(new byte[] { 0, 1, 2, 3, 4 }, new byte[] { 0, 1, 2, 3, 4, 5 }));
            Assert.True(BytesOperations.SequenceEqual(new byte[] { 0, 1, 2, 3, 4 }.AsSpan().Slice(2), new byte[] { 0, 1, 2, 3, 4 }.AsSpan().Slice(2)));
            Assert.False(BytesOperations.SequenceEqual(new byte[] { 0, 1, 2, 3, 4 }.AsSpan().Slice(1), new byte[] { 0, 1, 2, 3, 4 }.AsSpan().Slice(2)));
        }
        public void RandomSendAndReceiveTest()
        {
            var random = new Random();

            var caseList = new List <int>();

            caseList.AddRange(Enumerable.Range(1, 32));
            caseList.AddRange(new int[] { 100, 1000, 10000, 1024 * 1024, 1024 * 1024 * 32 });

            var(socket1, socket2) = SocketHelpers.GetSockets();

            var options = new OmniNonblockingConnectionOptions()
            {
                MaxReceiveByteCount = 1024 * 1024 * 256,
                MaxSendByteCount    = 1024 * 1024 * 256,
                BufferPool          = BufferPool.Shared,
            };

            using (var connection1 = new OmniNonblockingConnection(new SocketCap(socket1, false), options))
                using (var connection2 = new OmniNonblockingConnection(new SocketCap(socket2, false), options))
                {
                    foreach (var bufferSize in caseList)
                    {
                        var buffer1 = new byte[bufferSize];
                        var buffer2 = new byte[bufferSize];

                        random.NextBytes(buffer1);

                        var valueTask1 = connection1.EnqueueAsync((bufferWriter) =>
                        {
                            bufferWriter.Write(buffer1);
                        });

                        var valueTask2 = connection2.DequeueAsync((sequence) =>
                        {
                            sequence.CopyTo(buffer2);
                        });

                        var stopwatch = Stopwatch.StartNew();

                        while (!valueTask1.IsCompleted || !valueTask2.IsCompleted)
                        {
                            if (stopwatch.Elapsed.TotalSeconds > 60)
                            {
                                throw new TimeoutException("SendAndReceive");
                            }

                            Thread.Sleep(100);

                            connection1.DoEvents();
                            connection2.DoEvents();
                        }

                        Assert.True(BytesOperations.SequenceEqual(buffer1, buffer2));
                    }
                }
        }
示例#15
0
        private static bool IsGlobalIpAddress(IPAddress ipAddress)
        {
            if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
            {
                if (ipAddress == IPAddress.Any || ipAddress == IPAddress.Loopback || ipAddress == IPAddress.Broadcast)
                {
                    return(false);
                }
                if (BytesOperations.Compare(ipAddress.GetAddressBytes(), IPAddress.Parse("10.0.0.0").GetAddressBytes()) >= 0 &&
                    BytesOperations.Compare(ipAddress.GetAddressBytes(), IPAddress.Parse("10.255.255.255").GetAddressBytes()) <= 0)
                {
                    return(false);
                }
                if (BytesOperations.Compare(ipAddress.GetAddressBytes(), IPAddress.Parse("172.16.0.0").GetAddressBytes()) >= 0 &&
                    BytesOperations.Compare(ipAddress.GetAddressBytes(), IPAddress.Parse("172.31.255.255").GetAddressBytes()) <= 0)
                {
                    return(false);
                }
                if (BytesOperations.Compare(ipAddress.GetAddressBytes(), IPAddress.Parse("127.0.0.0").GetAddressBytes()) >= 0 &&
                    BytesOperations.Compare(ipAddress.GetAddressBytes(), IPAddress.Parse("127.255.255.255").GetAddressBytes()) <= 0)
                {
                    return(false);
                }
                if (BytesOperations.Compare(ipAddress.GetAddressBytes(), IPAddress.Parse("192.168.0.0").GetAddressBytes()) >= 0 &&
                    BytesOperations.Compare(ipAddress.GetAddressBytes(), IPAddress.Parse("192.168.255.255").GetAddressBytes()) <= 0)
                {
                    return(false);
                }
                if (BytesOperations.Compare(ipAddress.GetAddressBytes(), IPAddress.Parse("169.254.0.0").GetAddressBytes()) >= 0 &&
                    BytesOperations.Compare(ipAddress.GetAddressBytes(), IPAddress.Parse("169.254.255.255").GetAddressBytes()) <= 0)
                {
                    return(false);
                }
            }
            if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
            {
                if (ipAddress == IPAddress.IPv6Any || ipAddress == IPAddress.IPv6Loopback || ipAddress == IPAddress.IPv6None)
                {
                    return(false);
                }
                if (ipAddress.ToString().StartsWith("fe80:"))
                {
                    return(false);
                }
                if (ipAddress.ToString().StartsWith("2001:"))
                {
                    return(false);
                }
                if (ipAddress.ToString().StartsWith("2002:"))
                {
                    return(false);
                }
            }

            return(true);
        }
示例#16
0
    public static bool Equals <T>(IList <T> source, IList <T> destination)
    {
        if (source is byte[] x && destination is byte[] y)
        {
            return(BytesOperations.Equals(x.AsSpan(), y.AsSpan()));
        }

        var equalityComparer = EqualityComparer <T> .Default;

        return(CollectionHelper.Equals(source, destination, equalityComparer));
    }
示例#17
0
    public static int Compare <T>(IList <T> source, IList <T> destination)
    {
        if (source is byte[] x && destination is byte[] y)
        {
            return(BytesOperations.Compare(x, y));
        }

        var compare = Comparer <T> .Default;

        return(CollectionHelper.Compare(source, destination, compare));
    }
示例#18
0
            public static byte[] CreateDecodeMatrix(byte[] encMatrix, int[] index, int k, int n)
            {
                var matrix = CreateGFMatrix(k, k);

                for (int i = 0, pos = 0; i < k; i++, pos += k)
                {
                    BytesOperations.Copy(encMatrix.AsSpan(index[i] * k), matrix.AsSpan(pos), k);
                }

                InvertMatrix(matrix, k);

                return matrix;
            }
示例#19
0
        public override void Write(byte[] buffer, int offset, int count)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }
            if (offset < 0 || buffer.Length < offset)
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }
            if (count < 0 || (buffer.Length - offset) < count)
            {
                throw new ArgumentOutOfRangeException(nameof(count));
            }

            if (count == 0)
            {
                return;
            }

            while (count > 0)
            {
                long p = (_position / SectorSize) * SectorSize;

                if (_blockPosition != p)
                {
                    this.Flush();
                }

                _stream.Seek(p, SeekOrigin.Begin);

                int blockWritePosition = (int)(_position - p);
                int length             = Math.Min(SectorSize - blockWritePosition, count);

                _blockPosition = p;
                BytesOperations.Copy(buffer.AsSpan(offset), _blockBuffer.AsSpan(blockWritePosition), length);
                if (_blockCount == 0)
                {
                    _blockOffset = blockWritePosition;
                }

                _blockCount = (length + blockWritePosition) - _blockOffset;

                _blockIsUpdated = true;

                offset += length;
                count  -= length;

                _position += length;
            }
        }
示例#20
0
        public override void Write(ReadOnlySpan <byte> buffer)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            if (buffer.Length == 0)
            {
                return;
            }

            this.Search();

            int offset         = 0;
            int count          = buffer.Length;
            int writeSumLength = 0;

            while (count > 0)
            {
                if (_currentBufferIndex >= _buffers.Count)
                {
                    var tempBuffer = _bytesPool.Array.Rent(_bufferSize);
                    if (_bufferSize < 1024 * 32)
                    {
                        _bufferSize *= 2;
                    }

                    _buffers.Add(tempBuffer);
                }

                int length = Math.Min(_buffers[_currentBufferIndex].Length - _currentBufferPosition, count);

                BytesOperations.Copy(buffer.Slice(offset), _buffers[_currentBufferIndex].AsSpan(_currentBufferPosition), length);
                _currentBufferPosition += length;

                offset         += length;
                count          -= length;
                writeSumLength += length;

                if (_currentBufferPosition == _buffers[_currentBufferIndex].Length)
                {
                    _currentBufferIndex++;
                    _currentBufferPosition = 0;
                }
            }

            _position += writeSumLength;
            _length    = Math.Max(_length, _position);
        }
示例#21
0
            public static byte[] CreateEncodeMatrix(int k, int n)
            {
                if (k > _gfSize + 1 || n > _gfSize + 1 || k > n)
                {
                    throw new ArgumentException("Invalid parameters n=" + n + ",k=" + k + ",gfSize=" + _gfSize);
                }

                var encMatrix = CreateGFMatrix(n, k);

                /*
                 * The encoding matrix is computed starting with a Vandermonde matrix,
                 * and then transforming it into a systematic matrix.
                 *
                 * fill the matrix with powers of field elements, starting from 0.
                 * The first row is special, cannot be computed with exp. table.
                 */
                var tmpMatrix = CreateGFMatrix(n, k);

                tmpMatrix[0] = (byte)1;

                // first row should be 0's, fill in the rest.
                for (int pos = k, row = 0; row < n - 1; row++, pos += k)
                {
                    for (int col = 0; col < k; col++)
                    {
                        tmpMatrix[pos + col] = _gf_exp[Modnn(row * col)];
                    }
                }

                /*
                 * quick code to build systematic matrix: invert the top
                 * k*k vandermonde matrix, multiply right the bottom n-k rows
                 * by the inverse, and construct the identity matrix at the top.
                 */
                // much faster than invertMatrix
                InvertVandermonde(tmpMatrix, k);
                MatMul(tmpMatrix, k * k, tmpMatrix, 0, encMatrix, k * k, n - k, k, k);

                /*
                 * the upper matrix is I so do not bother with a slow multiply
                 */
                BytesOperations.Zero(encMatrix.AsSpan(0, k * k));

                for (int i = 0, col = 0; col < k; col++, i += k + 1)
                {
                    encMatrix[i] = (byte)1;
                }

                return encMatrix;
            }
示例#22
0
            public static uint Verify_Simple_Sha2_256(ReadOnlySpan <byte> key, ReadOnlySpan <byte> value)
            {
                if (key == null)
                {
                    throw new ArgumentNullException(nameof(key));
                }
                if (key.Length != 32)
                {
                    throw new ArgumentOutOfRangeException(nameof(key));
                }
                if (value == null)
                {
                    throw new ArgumentNullException(nameof(value));
                }
                if (value.Length != 32)
                {
                    throw new ArgumentOutOfRangeException(nameof(value));
                }

                Span <byte> buffer = stackalloc byte[64];

                byte[] hash;
                {
                    BytesOperations.Copy(key, buffer, key.Length);
                    BytesOperations.Copy(value, buffer.Slice(key.Length), value.Length);
                    hash = Sha2_256.ComputeHash(buffer);
                }

                uint count = 0;

                for (int i = 0; i < 32; i++)
                {
                    for (int j = 0; j < 8; j++)
                    {
                        if (((hash[i] << j) & 0x80) == 0)
                        {
                            count++;
                        }
                        else
                        {
                            goto End;
                        }
                    }
                }

End:
                return(count);
            }
示例#23
0
    public static bool TryComputeHash(ReadOnlySequence <byte> sequence, ReadOnlySpan <byte> key, Span <byte> destination)
    {
        if (destination.Length < 32)
        {
            throw new ArgumentOutOfRangeException(nameof(destination));
        }

        Span <byte> extendedKey = stackalloc byte[_blockLength];

        if (key.Length > _blockLength)
        {
            Sha2_256.TryComputeHash(key, extendedKey);
        }
        else
        {
            BytesOperations.Copy(key, extendedKey, Math.Min(key.Length, extendedKey.Length));
        }

        Span <byte> ixor = stackalloc byte[_blockLength];

        BytesOperations.Xor(_ipad, extendedKey, ixor);

        Span <byte> oxor = stackalloc byte[_blockLength];

        BytesOperations.Xor(_opad, extendedKey, oxor);

        Span <byte> ihash = stackalloc byte[32];

        using (var incrementalHash = IncrementalHash.CreateHash(HashAlgorithmName.SHA256))
        {
            incrementalHash.AppendData(ixor);

            foreach (var segment in sequence)
            {
                incrementalHash.AppendData(segment.Span);
            }

            incrementalHash.TryGetHashAndReset(ihash, out _);
        }

        using (var incrementalHash = IncrementalHash.CreateHash(HashAlgorithmName.SHA256))
        {
            incrementalHash.AppendData(oxor);
            incrementalHash.AppendData(ihash);

            return(incrementalHash.TryGetHashAndReset(destination, out _));
        }
    }
示例#24
0
        public async Task Decode(Memory<byte>[] packets, int[] index, int packetLength, int concurrency = 1, CancellationToken token = default)
        {
            if (packets == null) throw new ArgumentNullException(nameof(packets));
            if (index == null) throw new ArgumentNullException(nameof(index));

            Shuffle(packets, index, _k);

            var decMatrix = ReadSolomonMath.CreateDecodeMatrix(_encMatrix, index, _k, _n);

            // do the actual decoding..
            var tempPackets = new byte[_k][];

            await Enumerable.Range(0, _k).ForEachAsync(row =>
            {
                return Task.Run(() =>
                {
                    token.ThrowIfCancellationRequested();

                    if (index[row] >= _k)
                    {
                        tempPackets[row] = _bufferPool.GetArrayPool().Rent(packetLength);
                        BytesOperations.Zero(tempPackets[row].AsSpan(0, packetLength));

                        for (int col = 0; col < _k; col++)
                        {
                            token.ThrowIfCancellationRequested();

                            ReadSolomonMath.AddMul(packets[col].Span, tempPackets[row], decMatrix[row * _k + col], packetLength);
                        }
                    }
                });
            }, concurrency, token, false);

            token.ThrowIfCancellationRequested();

            // move pkts to their final destination
            for (int row = 0; row < _k; row++)
            {
                if (index[row] >= _k)
                {
                    // only copy those actually decoded.
                    BytesOperations.Copy(tempPackets[row], packets[row].Span, packetLength);
                    index[row] = row;
                    _bufferPool.GetArrayPool().Return(tempPackets[row]);
                }
            }
        }
示例#25
0
        public void RandomReadAndWriteTest(int seed, int loopCount, int bufferLength)
        {
            var random    = new Random(seed);
            var bytesPool = BytesPool.Shared;

            using var buffer1 = bytesPool.Memory.Rent(bufferLength);
            using var buffer2 = bytesPool.Memory.Rent(bufferLength);

            using var recyclableMemoryStream = new RecyclableMemoryStream(BytesPool.Shared);
            using var memoryStream           = new MemoryStream();

            // Write
            foreach (var i in Enumerable.Range(0, loopCount))
            {
                random.NextBytes(buffer1.Memory.Span);
                var s = buffer1.Memory.Span.Slice(0, random.Next(0, buffer1.Memory.Length));
                recyclableMemoryStream.Write(s);
                memoryStream.Write(s);
            }

            Assert.Equal(recyclableMemoryStream.Position, memoryStream.Position);
            Assert.Equal(recyclableMemoryStream.Length, memoryStream.Length);

            recyclableMemoryStream.Seek(0, SeekOrigin.Begin);
            memoryStream.Seek(0, SeekOrigin.Begin);

            // Read
            while (memoryStream.Position < memoryStream.Length)
            {
                var length = random.Next(0, buffer1.Memory.Length);
                var s1     = buffer1.Memory.Span.Slice(0, length);
                var s2     = buffer1.Memory.Span.Slice(0, length);

                recyclableMemoryStream.Read(s1);
                memoryStream.Read(s2);

                Assert.Equal(recyclableMemoryStream.Position, memoryStream.Position);
                Assert.Equal(recyclableMemoryStream.Length, memoryStream.Length);
                Assert.True(BytesOperations.Equals(s1, s2));
            }

            using var m1 = recyclableMemoryStream.ToMemoryOwner();
            var m2 = memoryStream.ToArray();

            Assert.True(BytesOperations.Equals(m1.Memory.Span, m2));
        }
示例#26
0
        public void SetLength(ulong length)
        {
            ulong byteCount   = MathHelper.Roundup(length, 8);
            ulong sectorCount = MathHelper.Roundup(byteCount, SectorSize);

            _bufferList = new byte[sectorCount][];

            for (int i = 0; i < _bufferList.Length; i++)
            {
                var buffer = _bufferPool.GetArrayPool().Rent((int)SectorSize);
                BytesOperations.Zero(buffer);

                _bufferList[i] = buffer;
            }

            _length = length;
        }
示例#27
0
        public static bool Equals <T>(IList <T> source, IList <T> destination)
        {
            {
                var x = source as byte[];
                var y = destination as byte[];

                if (x != null && y != null)
                {
                    return(BytesOperations.SequenceEqual(x.AsSpan(), y.AsSpan()));
                }
            }

            {
                var equalityComparer = EqualityComparer <T> .Default;
                return(CollectionHelper.Equals(source, destination, equalityComparer));
            }
        }
示例#28
0
        public static int Compare <T>(IList <T> source, IList <T> destination)
        {
            {
                var x = source as byte[];
                var y = destination as byte[];

                if (x != null && y != null)
                {
                    return(BytesOperations.Compare(x, y));
                }
            }

            {
                var compare = Comparer <T> .Default;
                return(CollectionHelper.Compare(source, destination, compare));
            }
        }
示例#29
0
        public static bool TryComputeHash(ReadOnlySpan <byte> password, ReadOnlySpan <byte> salt, int iterationCount, Span <byte> destination)
        {
            const int hashLength = 32;

            int keyLength = destination.Length / hashLength;

            if (destination.Length % hashLength != 0)
            {
                keyLength++;
            }

            var         extendedkeyLength = (salt.Length + 4);
            Span <byte> extendedkey       = extendedkeyLength <= 128 ? stackalloc byte[extendedkeyLength] : new byte[extendedkeyLength];

            BytesOperations.Copy(salt, extendedkey, salt.Length);

            Span <byte> f = stackalloc byte[hashLength];
            Span <byte> u = stackalloc byte[hashLength];

            for (int i = 0; i < keyLength; i++)
            {
                extendedkey[salt.Length]     = (byte)(((i + 1) >> 24) & 0xFF);
                extendedkey[salt.Length + 1] = (byte)(((i + 1) >> 16) & 0xFF);
                extendedkey[salt.Length + 2] = (byte)(((i + 1) >> 8) & 0xFF);
                extendedkey[salt.Length + 3] = (byte)(((i + 1)) & 0xFF);

                Hmac_Sha2_256.TryComputeHash(extendedkey, password, u);
                BytesOperations.Zero(extendedkey.Slice(salt.Length, 4));

                BytesOperations.Copy(u, f, hashLength);

                for (int j = 1; j < iterationCount; j++)
                {
                    Hmac_Sha2_256.TryComputeHash(u, password, u);
                    BytesOperations.Xor(f, u, f);
                }

                int position = i * hashLength;
                int remain   = Math.Min(hashLength, destination.Length - position);

                BytesOperations.Copy(f, destination.Slice(position), remain);
            }

            return(true);
        }
示例#30
0
        public bool Equals(NodeInfo <T> other)
        {
            if ((this.Id == null) != (other.Id == null) ||
                !this.Value.Equals(other.Value))
            {
                return(false);
            }

            if (this.Id != null && other.Id != null)
            {
                if (!BytesOperations.SequenceEqual(this.Id, other.Id))
                {
                    return(false);
                }
            }

            return(true);
        }