private async Task <char> TransformSafeBytesToCharAsync(IReadOnlySafeBytes safeBytes, Encoding encoding) { var byteBuffer = await safeBytes.RevealDecryptedBytesAsync().ConfigureAwait(false); try { return(_textService.GetChars(byteBuffer, encoding).First()); } finally { Array.Clear(byteBuffer, 0, byteBuffer.Length); } }
private async Task <ISafeBytes> ConvertEncodingAsync(IReadOnlySafeBytes character, Encoding sourceEncoding, Encoding destinationEncoding) { var buffer = await character.RevealDecryptedBytesAsync().ConfigureAwait(false); try { buffer = _textService.Convert(sourceEncoding, destinationEncoding, buffer); var safeBytes = _safeBytesFactory.Create(); var stream = new SafeMemoryStream(buffer); await safeBytes.AppendManyAsync(stream) .ConfigureAwait(false); return(safeBytes); } finally { Array.Clear(buffer, 0, buffer.Length); } }
/// <inheritdoc cref="DisposableBase.ThrowIfDisposed"/> public async Task <bool> EqualsAsync(IReadOnlySafeBytes other) { ThrowIfDisposed(); if (other == null || other.Length == 0) { return(Length == 0); } if (this.GetHashCode() != other.GetHashCode()) { return(false); } var otherBytes = GetOtherBytesAsync(); var ownBytes = _safeByteCollection.GetAllAsync(); await Task.WhenAll(otherBytes, ownBytes) .ConfigureAwait(false); return(AreEqual(otherBytes.Result, ownBytes.Result)); async Task <ISafeByte[]> GetOtherBytesAsync() { if (other is SafeBytes safeBytes) { // If it's the known SafeBytes then it reveals nothing in the memory var bytes = await safeBytes._safeByteCollection.GetAllAsync() .ConfigureAwait(false); return(bytes); } else { // If it's not, then reveals each byte in memory. var bytes = await other.RevealDecryptedBytesAsync().ConfigureAwait(false); var stream = new SafeMemoryStream(bytes); var safe = await _safeByteFactory.GetByBytesAsync(stream) .ConfigureAwait(false); return(safe.ToArray()); } } }
public Task <bool> EqualsAsync(IReadOnlySafeBytes other) { return(Task.FromResult(other != null && _bytes.AsEnumerable().SequenceEqual(TaskContext.RunSync(other.RevealDecryptedBytesAsync)))); }
/// <summary> /// Indicates whether the specified <see cref="ISafeBytes"/> is null or holds zero bytes. /// </summary> /// <param name="safeBytes">The string to test.</param> /// <returns><see langword="true" /> if the value parameter is <see langword="null" /> or holds zero bytes; otherwise, <see langword="false" />.</returns> public static bool IsNullOrEmpty(IReadOnlySafeBytes safeBytes) => safeBytes == null || safeBytes.Length == 0;