/// <summary> /// Encrypts the buffer with the specified length. /// </summary> /// <param name="buffer">The buffer to encrypt.</param> /// <param name="bufferLength">The buffer length which must be a multiple of 8.</param> /// /// <exception cref="ArgumentNullException"> /// <paramref name="buffer"/> is null. /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="bufferLength"/> is less than zero or greater than the length of <paramref name="buffer"/>. /// </exception> /// <exception cref="ArgumentException"> /// <paramref name="bufferLength"/> is not a multiple of 8. /// </exception> public void Encrypt(byte[] buffer, int bufferLength) { if (buffer == null) { throw new ArgumentNullException(nameof(buffer)); } if (bufferLength < 0 || bufferLength > buffer.Length) { throw ArgumentOutOfRangeUtils.OutsideRange(nameof(bufferLength), bufferLength, 0, $"{nameof(buffer)}.{nameof(buffer.Length)}", buffer.Length, true, true); } fixed(byte *pBuffer = buffer) Encrypt(pBuffer, bufferLength); }
/// <summary> /// Advances the position in the stream to conform to a padding amount with the supplied additional offset. /// </summary> /// <param name="stream">The stream to advance.</param> /// <param name="padding">The padding to conform to.</param> /// <param name="offset">The additional offset before the padding is calculated.</param> /// <returns>The amount of bytes skipped.</returns> /// /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="padding"/> is less than or equal to zero.-or- <paramref name="offset"/> is less than zero /// or greater than or equal to <paramref name="padding"/>. /// </exception> /// <exception cref="IOException"> /// An I/O error occurred. /// </exception> /// <exception cref="NotSupportedException"> /// The stream does not support seeking or reading. /// </exception> /// <exception cref="ObjectDisposedException"> /// The stream is closed. /// </exception> public static int SkipPadding(this Stream stream, int padding, int offset) { if (padding <= 0) { throw ArgumentOutOfRangeUtils.OutsideMin(nameof(padding), padding, 1, false); } if (offset < 0 || offset >= padding) { throw ArgumentOutOfRangeUtils.OutsideRange(nameof(offset), padding, 0, nameof(padding), padding, true, false); } int count = (int)((stream.Position + (padding - offset)) % padding); if (count != 0) { count = (padding - count); stream.Skip(count); } return(0); }
/// <summary> /// Encrypts the buffer with the specified length. /// </summary> /// <param name="buffer">The buffer to encrypt.</param> /// <param name="index">The index to start encrypting the buffer at.</param> /// <param name="bufferLength">The buffer length which must be a multiple of 8.</param> /// /// <exception cref="ArgumentNullException"> /// <paramref name="buffer"/> is null. /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="index"/> or <paramref name="bufferLength"/> is less than zero.-or- /// <paramref name="index"/> + <paramref name="bufferLength"/> is greater than the length of /// <paramref name="buffer"/>. /// </exception> /// <exception cref="ArgumentException"> /// <paramref name="bufferLength"/> is not a multiple of 8. /// </exception> public void Encrypt(byte[] buffer, int index, int bufferLength) { if (buffer == null) { throw new ArgumentNullException(nameof(buffer)); } if (index < 0) { throw ArgumentOutOfRangeUtils.OutsideMin(nameof(index), index, 0, true); } if (bufferLength < 0) { throw ArgumentOutOfRangeUtils.OutsideMin(nameof(bufferLength), bufferLength, 0, true); } if (index + bufferLength > buffer.Length) { throw ArgumentOutOfRangeUtils.OutsideRange($"{nameof(index)} + {nameof(bufferLength)}", index + bufferLength, 0, $"{nameof(buffer)}.{nameof(Array.Length)}", buffer.Length, true, false); } fixed(byte *pBuffer = buffer) Encrypt(pBuffer + index, bufferLength); }