예제 #1
0
        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);
            }
        }
예제 #2
0
        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);
            }
        }
예제 #3
0
        /// <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());
                }
            }
        }
예제 #4
0
 public Task <bool> EqualsAsync(IReadOnlySafeBytes other)
 {
     return(Task.FromResult(other != null &&
                            _bytes.AsEnumerable().SequenceEqual(TaskContext.RunSync(other.RevealDecryptedBytesAsync))));
 }
예제 #5
0
 /// <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;