Пример #1
0
        public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
        {
            // inputCount <= InputBlockSize is allowed
            ThrowHelper.ValidateTransformBlock(inputBuffer, inputOffset, inputCount);

            // Convert.ToBase64CharArray already does padding, so all we have to check is that
            // the inputCount wasn't 0
            if (inputCount == 0)
            {
                return(Array.Empty <byte>());
            }
            else if (inputCount > InputBlockSize)
            {
                ThrowHelper.ThrowArgumentOutOfRange(ThrowHelper.ExceptionArgument.inputCount);
            }

            // Again, for now only a block at a time
            Span <byte> input = inputBuffer.AsSpan(inputOffset, inputCount);

            byte[] output = new byte[OutputBlockSize];

            OperationStatus status = Base64.EncodeToUtf8(input, output, out int consumed, out int written, isFinalBlock: true);

            if (written != OutputBlockSize)
            {
                ThrowHelper.ThrowCryptographicException();
            }

            Debug.Assert(status == OperationStatus.Done);
            Debug.Assert(consumed == inputCount);

            return(output);
        }
Пример #2
0
        public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
        {
            ThrowHelper.ValidateTransformBlock(inputBuffer, inputOffset, inputCount);

            int inputBlocks = Math.DivRem(inputCount, InputBlockSize, out int inputRemainder);

            if (inputBlocks == 0)
            {
                ThrowHelper.ThrowArgumentOutOfRange(ThrowHelper.ExceptionArgument.inputCount);
            }

            if (outputBuffer == null)
            {
                ThrowHelper.ThrowArgumentNull(ThrowHelper.ExceptionArgument.outputBuffer);
            }

            if (inputRemainder != 0)
            {
                ThrowHelper.ThrowArgumentOutOfRange(ThrowHelper.ExceptionArgument.inputCount);
            }

            int requiredOutputLength = checked (inputBlocks * OutputBlockSize);

            if (requiredOutputLength > outputBuffer.Length - outputOffset)
            {
                ThrowHelper.ThrowArgumentOutOfRange(ThrowHelper.ExceptionArgument.outputBuffer);
            }

            Span <byte> input  = inputBuffer.AsSpan(inputOffset, inputCount);
            Span <byte> output = outputBuffer.AsSpan(outputOffset, requiredOutputLength);

            OperationStatus status = Base64.EncodeToUtf8(input, output, out int consumed, out int written, isFinalBlock: false);

            Debug.Assert(status == OperationStatus.Done);
            Debug.Assert(consumed == input.Length);
            Debug.Assert(written == output.Length);

            return(written);
        }