/// <inheritdoc /> /// <inheritdoc cref="DisposableBase.ThrowIfDisposed"/> /// <exception cref="ArgumentException">Throws if the argument is empty</exception> public async Task AppendAsync(ISafeBytes safeBytes) { ThrowIfDisposed(); if (safeBytes.Length == 0) { throw new ArgumentException($"{nameof(safeBytes)} is empty."); } // If it's the known SafeBytes then it reveals nothing in the memory if (safeBytes is SafeBytes asSafeBytes) { var allBytes = await asSafeBytes._safeByteCollection.GetAllAsync() .ConfigureAwait(false); await _safeByteCollection.AppendManyAsync(allBytes) .ConfigureAwait(false); foreach (var safeByte in allBytes) { UpdateHashCode(safeByte); } } // If it's not, then reveals each byte in memory. else { var plainBytes = await safeBytes.RevealDecryptedBytesAsync() .ConfigureAwait(false); var stream = new SafeMemoryStream(plainBytes); await AppendManyAsync(stream).ConfigureAwait(false); } }
public bool Equals(ISafeBytes other) { if (Length != other?.Length) { return(false); } var asSafeBytes = other as SafeBytes; if (asSafeBytes != null) { for (var i = 0; i < Length; i++) { if (!GetAsSafeByte(i).Equals(asSafeBytes.GetAsSafeByte(i))) { return(false); } } } else { for (var i = 0; i < Length; i++) { if (!GetAsSafeByte(i).Equals(other.GetByte(i))) { return(false); } } } return(true); }
private char TransformSafeBytesToChar(ISafeBytes safeBytes, Encoding encoding) { var byteBuffer = safeBytes.ToByteArray(); try { return(_textService.GetChars(byteBuffer, InnerEncoding).First()); } finally { Array.Clear(byteBuffer, 0, byteBuffer.Length); } }
/// <inheritdoc /> /// <inheritdoc cref="DisposableBase.ThrowIfDisposed"/> /// <exception cref="ArgumentNullException"> <paramref name="character" /> is <see langword="null" />. </exception> /// <exception cref="ArgumentOutOfRangeException"> <paramref name="character" /> is null or empty. </exception> public async Task InsertAsync(int position, ISafeBytes character, Encoding encoding = Encoding.Utf16LittleEndian) { ThrowIfDisposed(); if (position < 0 || position > Length) { throw new ArgumentOutOfRangeException(nameof(position)); } if (SafeBytes.IsNullOrEmpty(character)) { throw new ArgumentNullException(nameof(character)); } if (encoding != InnerEncoding) { character = await ConvertEncodingAsync(character, encoding, InnerEncoding) .ConfigureAwait(false); } _charBytesList.Insert(position, character); }
/// <summary> /// Clones and appends the each byte from given SafeBytes object to the end. /// </summary> /// <exception cref="ArgumentNullException">Throws if the argument is null</exception> /// <exception cref="ObjectDisposedException">Throws if the SafeBytes instance is disposed</exception> /// <exception cref="ArgumentException">Throws if the argument is empty</exception> public void Append(ISafeBytes safeBytes) { EnsureNotDisposed(); if (safeBytes == null) { throw new ArgumentNullException(nameof(safeBytes)); } if (safeBytes.IsDisposed) { throw new ObjectDisposedException(nameof(safeBytes)); } if (safeBytes.Length == 0) { throw new ArgumentException($"{nameof(safeBytes)} is empty."); } //If it's the known SafeBytes then it reveals nothing in the memory var asSafeBytes = safeBytes as SafeBytes; if (asSafeBytes != null) { for (var i = 0; i < safeBytes.Length; i++) { var safeByte = asSafeBytes.GetAsSafeByte(i); _safeByteCollection.Append(safeByte); } } //If it's not, then reveals each byte in memory. else { for (var i = 0; i < safeBytes.Length; i++) { var safeByte = _safeByteFactory.GetByByte(safeBytes.GetByte(i)); _safeByteCollection.Append(safeByte); } } //Add the arbitrary byte }
/// <exception cref="ArgumentNullException"> /// <paramref name="character" /> is <see langword="null" />. /// </exception> /// <exception cref="ObjectDisposedException"> /// Throws if the SafeString instance is disposed. /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// Throws if <paramref name="character" /> is less than zero or higher than the length. /// </exception> public void Insert(int position, ISafeBytes character, Encoding encoding = Encoding.Utf16LittleEndian) { EnsureNotDisposed(); if ((position < 0) || (position > Length)) { throw new ArgumentOutOfRangeException(nameof(position)); } if (SafeBytes.IsNullOrEmpty(character)) { throw new ArgumentNullException(nameof(character)); } if (encoding == InnerEncoding) { _charBytesList.Insert(position, character); return; } //Convert encoding var buffer = character.ToByteArray(); try { buffer = _textService.Convert(encoding, InnerEncoding, buffer); var safeBytes = _safeBytesFactory.Create(); for (var i = 0; i < buffer.Length; i++) { //Append safeBytes.Append(buffer[i]); buffer[i] = 0x0; } _charBytesList.Insert(position, safeBytes); } finally { Array.Clear(buffer, 0, buffer.Length); } }
/// <exception cref="ArgumentNullException"> /// <paramref name="character" /> is <see langword="null" />. /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// If position is less than zero or higher than the length. /// </exception> /// <exception cref="ObjectDisposedException"> /// Throws if the SafeString instance is disposed. /// </exception> public void Append(ISafeBytes character, Encoding encoding = Encoding.Utf16LittleEndian) { Insert(Length, character, encoding); }
public async Task AppendAsync(ISafeBytes safeBytes) { _bytes.AddRange(await safeBytes.RevealDecryptedBytesAsync()); }
/// <summary> /// Indicates whether the specified SafeBytes object is null or holds zero bytes. /// </summary> public static bool IsNullOrEmpty(ISafeBytes safebytes) => (safebytes == null) || (safebytes.Length == 0);
/// <summary> /// Class SafeBytesHelper. /// </summary> /// <summary> /// Appends the and return deep clone. /// </summary> /// <param name="safeBytes">The safe bytes.</param> /// <param name="byte">The byte to add.</param> /// <returns>DeepClone of appended <see cref="@byte"/>.</returns> public static ISafeBytes AppendAndReturnDeepClone(this ISafeBytes safeBytes, byte @byte) { safeBytes.Append(@byte); return(safeBytes.DeepClone()); }
/// <inheritdoc /> /// <exception cref="ArgumentNullException"><paramref name="character" /> is <see langword="null" />. </exception> /// <exception cref="ArgumentOutOfRangeException">If position is less than zero or higher than the length. </exception> /// <inheritdoc cref="DisposableBase.ThrowIfDisposed"/> public Task AppendAsync(ISafeBytes character, Encoding encoding = Encoding.Utf16LittleEndian) { return(InsertAsync(Length, character, encoding)); }
public bool Equals(ISafeBytes other) { return(other != null && _bytes.ToArray().SequenceEqual(other.ToByteArray())); }
public void Append(ISafeBytes safeBytes) { _bytes.AddRange(safeBytes.ToByteArray()); }