コード例 #1
0
ファイル: BitString.cs プロジェクト: a-luna/dotnetcore-crypto
        public BitString(byte[] data, int length = -1)
        {
            data = data ?? throw new ArgumentNullException(nameof(data));
            var count    = data.Length;
            var bitCount = count << Shift;

            _length = length < 0
                                ? bitCount
                                : length;

            if (_length > bitCount)
            {
                throw new ArgumentOutOfRangeException(nameof(length));
            }

            // If the full range of bits is to be considered, whole process is a lot simpler.
            if (_length != bitCount)
            {
                // How many blocks will we need?
                count = (int)Math.Ceiling((double)_length / BlockBitSize);
                Array.Resize(ref data, count);

                // If the last block is not full, zero the trailing bits which do not belong to the bitString.
                var remaining = _length % BlockBitSize;
                if (remaining > 0)
                {
                    data[count - 1] &= BinaryFunctions.LowerMask(remaining);
                }
            }
            _data = data;
        }
コード例 #2
0
ファイル: BitString.cs プロジェクト: a-luna/dotnetcore-crypto
        public static BitString Random(Random random, int length)
        {
            var count = (int)Math.Ceiling((double)length / BlockBitSize);
            var data  = new byte[count];

            random.NextBytes(data);

            var left = length % BlockBitSize;

            if (left != 0)
            {
                data[count - 1] &= BinaryFunctions.LowerMask(left);
            }

            return(new BitString(data, length));
        }
コード例 #3
0
ファイル: BitString.cs プロジェクト: a-luna/dotnetcore-crypto
        public BitString Truncate(int length)
        {
            length = Math.Min(_length, Math.Max(0, length));
            var count = (int)Math.Ceiling((double)length / BlockBitSize);
            var data  = new byte[count];

            Array.Copy(_data, 0, data, 0, count);

            var left = length % BlockBitSize;

            if (left != 0)
            {
                data[count - 1] &= BinaryFunctions.LowerMask(left);
            }

            return(new BitString(data, length));
        }