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>()); } Span <byte> input = inputBuffer.AsSpan(inputOffset, inputCount); int inputBlocks = Math.DivRem(inputCount, InputBlockSize, out int inputRemainder); int outputBlocks = inputBlocks + (inputRemainder != 0 ? 1 : 0); byte[] output = new byte[outputBlocks * OutputBlockSize]; OperationStatus status = Base64.EncodeToUtf8(input, output, out int consumed, out int written, isFinalBlock: true); Debug.Assert(written == output.Length); Debug.Assert(status == OperationStatus.Done); Debug.Assert(consumed == inputCount); return(output); }
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); }
public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) { // inputCount < InputBlockSize is not allowed ThrowHelper.ValidateTransformBlock(inputBuffer, inputOffset, inputCount, InputBlockSize); if (outputBuffer == null) { ThrowHelper.ThrowArgumentNull(ThrowHelper.ExceptionArgument.outputBuffer); } // For now, only convert 3 bytes to 4 Span <byte> input = inputBuffer.AsSpan(inputOffset, InputBlockSize); Span <byte> output = outputBuffer.AsSpan(outputOffset, OutputBlockSize); OperationStatus status = Base64.EncodeToUtf8(input, output, out int consumed, out int written, isFinalBlock: false); if (written != OutputBlockSize) { ThrowHelper.ThrowCryptographicException(); } Debug.Assert(status == OperationStatus.NeedMoreData); Debug.Assert(consumed == InputBlockSize); return(written); }
public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount) { // inputCount != InputBlockSize is allowed ThrowHelper.ValidateTransformBlock(inputBuffer, inputOffset, inputCount); if (_inputBuffer == null) { ThrowHelper.ThrowObjectDisposed(); } if (inputCount == 0) { return(Array.Empty <byte>()); } // The common case is inputCount <= Base64InputBlockSize byte[]? tmpBufferArray = null; Span <byte> tmpBuffer = stackalloc byte[StackAllocSize]; if (inputCount > StackAllocSize) { tmpBuffer = tmpBufferArray = CryptoPool.Rent(inputCount); } tmpBuffer = GetTempBuffer(inputBuffer.AsSpan(inputOffset, inputCount), tmpBuffer); int bytesToTransform = _inputIndex + tmpBuffer.Length; // Too little data to decode if (bytesToTransform < InputBlockSize) { // reinitialize the transform Reset(); ReturnToCryptoPool(tmpBufferArray, tmpBuffer.Length); return(Array.Empty <byte>()); } int outputSize = GetOutputSize(bytesToTransform, tmpBuffer); byte[] output = new byte[outputSize]; ConvertFromBase64(tmpBuffer, output, out int consumed, out int written); Debug.Assert(written == outputSize); ReturnToCryptoPool(tmpBufferArray, tmpBuffer.Length); // reinitialize the transform Reset(); return(output); }
public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) { // inputCount != InputBlockSize is allowed ThrowHelper.ValidateTransformBlock(inputBuffer, inputOffset, inputCount); if (_inputBuffer == null) { ThrowHelper.ThrowObjectDisposed(); } if (outputBuffer == null) { ThrowHelper.ThrowArgumentNull(ThrowHelper.ExceptionArgument.outputBuffer); } // The common case is inputCount = InputBlockSize byte[]? tmpBufferArray = null; Span <byte> tmpBuffer = stackalloc byte[StackAllocSize]; if (inputCount > StackAllocSize) { tmpBuffer = tmpBufferArray = CryptoPool.Rent(inputCount); } tmpBuffer = GetTempBuffer(inputBuffer.AsSpan(inputOffset, inputCount), tmpBuffer); int bytesToTransform = _inputIndex + tmpBuffer.Length; // Too little data to decode: save data to _inputBuffer, so it can be transformed later if (bytesToTransform < InputBlockSize) { tmpBuffer.CopyTo(_inputBuffer.AsSpan(_inputIndex)); _inputIndex = bytesToTransform; ReturnToCryptoPool(tmpBufferArray, tmpBuffer.Length); return(0); } ConvertFromBase64(tmpBuffer, outputBuffer.AsSpan(outputOffset), out _, out int written); ReturnToCryptoPool(tmpBufferArray, tmpBuffer.Length); return(written); }
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); }