// Returns Success public async Task <bool> DecompressAsync(Letter letter) { if (letter.LetterMetadata.Encrypted) { return(false); } // Don't decompress before decryption. if (letter.LetterMetadata.Compressed) { try { letter.Body = await Gzip.DecompressAsync(letter.Body).ConfigureAwait(false); letter.LetterMetadata.Compressed = false; if (letter.LetterMetadata.CustomFields.ContainsKey(Constants.HeaderForCompressed)) { letter.LetterMetadata.CustomFields.Remove(Constants.HeaderForCompressed); } if (letter.LetterMetadata.CustomFields.ContainsKey(Constants.HeaderForCompression)) { letter.LetterMetadata.CustomFields.Remove(Constants.HeaderForCompression); } } catch { return(false); } } return(true); }
public async Task ComcryptDecomcryptTest() { var message = new Message { StringMessage = $"Sensitive ReceivedLetter 0", MessageId = 0 }; var data = JsonSerializer.SerializeToUtf8Bytes(message); var hashKey = await ArgonHash .GetHashKeyAsync(Passphrase, Salt, HouseofCat.Encryption.Constants.Aes256.KeySize) .ConfigureAwait(false); _output.WriteLine(Encoding.UTF8.GetString(hashKey)); _output.WriteLine($"HashKey: {Encoding.UTF8.GetString(hashKey)}"); // Comcrypt var payload = await Gzip.CompressAsync(data); var encryptedPayload = AesEncrypt.Aes256Encrypt(payload, hashKey); // Decomcrypt var decryptedData = AesEncrypt.Aes256Decrypt(encryptedPayload, hashKey); Assert.NotNull(decryptedData); var decompressed = await Gzip.DecompressAsync(decryptedData); JsonSerializer.SerializeToUtf8Bytes(decompressed); _output.WriteLine($"Data: {Encoding.UTF8.GetString(data)}"); _output.WriteLine($"Decrypted: {Encoding.UTF8.GetString(decryptedData)}"); Assert.Equal(data, decompressed); }
public async Task DecomcryptAsync(Letter letter) { var decryptFailed = false; if (letter.LetterMetadata.Encrypted && (_hashKey?.Length > 0)) { try { letter.Body = AesEncrypt.Decrypt(letter.Body, _hashKey); letter.LetterMetadata.Encrypted = false; } catch { decryptFailed = true; } } if (!decryptFailed && letter.LetterMetadata.Compressed) { try { letter.Body = await Gzip.DecompressAsync(letter.Body).ConfigureAwait(false); letter.LetterMetadata.Compressed = false; } catch { } } }
/// <summary> /// Sends a POST request to an API function using a <paramref name="token"/>, and returns /// the response as a JSON string. /// </summary> /// <param name="function"> /// The name of the function. /// </param> /// <param name="args"> /// A set of parameters to pass along with the request. /// </param> /// <param name="token"> /// The auth token. /// </param> /// <returns> /// Upon success, the JSON string; otherwise, <c>null</c>. /// </returns> public async Task <string> PostToJsonAsync(string function, RequestParameters args, string token) { Contract.Requires <ArgumentNullException>(function != null); Contract.Requires <ArgumentNullException>(token != null); var response = await PostAsync(function, args, token); if (!response.IsSuccessStatusCode) { return(null); } // Obtain the JSON data (and decompress it if it is gzipped). string jsonData; if (response.Content.Headers.ContentEncoding.Contains(HttpContentCodingHeaderValue.Parse("gzip"))) { var compressedData = (await response.Content.ReadAsBufferAsync()).ToArray(); jsonData = await Gzip.DecompressAsync(compressedData, Encoding.UTF8); } else { jsonData = await response.Content.ReadAsStringAsync(); } Debug.WriteLine("[EndpointManager] Received JSON data: {0}", jsonData); // Return the JSON data. return(jsonData); }
public static async Task <byte[]> DecompressAsync(ReadOnlyMemory <byte> data, CompressionMethod method) { return(method switch { CompressionMethod.LZ4 => await LZ4.DecompressAsync(data).ConfigureAwait(false), CompressionMethod.Deflate => await Deflate.DecompressAsync(data).ConfigureAwait(false), CompressionMethod.Brotli => await Brotli.DecompressAsync(data).ConfigureAwait(false), CompressionMethod.Gzip => await Gzip.DecompressAsync(data).ConfigureAwait(false), _ => await Gzip.DecompressAsync(data).ConfigureAwait(false) });
public async Task DecomcryptDataAsync(bool decrypt = false, bool decompress = false) { if (!_decrypted && decrypt && _hashKey.Length > 0) { Data = AesEncrypt.Decrypt(Data, _hashKey); _decrypted = true; } if (!_decompressed && decompress) { Data = await Gzip .DecompressAsync(Data) .ConfigureAwait(false); _decompressed = true; } }
public async Task <bool> DecompressAsync(Letter letter) { if (letter.LetterMetadata.Encrypted) { return(false); } if (letter.LetterMetadata.Compressed) { try { letter.Body = await Gzip.DecompressAsync(letter.Body).ConfigureAwait(false); letter.LetterMetadata.Compressed = false; } catch { } } return(!letter.LetterMetadata.Compressed); }
/// <summary> /// Sends a GET request to an API function, and receives binary data from the server. /// </summary> /// <param name="function"> /// The name of the function. /// </param> /// <returns> /// Upon success, a byte array containing the data received from the server; otherwise, /// <c>null</c>. /// </returns> public async Task <byte[]> GetAsync(string function) { Contract.Requires <ArgumentNullException>(function != null); var response = await GetAsync(function, null); if (!response.IsSuccessStatusCode) { return(null); } // Obtain the binary data (and decompress it if it is gzipped). var data = (await response.Content.ReadAsBufferAsync()).ToArray(); if (response.Content.Headers.ContentEncoding.Contains(HttpContentCodingHeaderValue.Parse("gzip"))) { data = await Gzip.DecompressAsync(data); } Debug.WriteLine("[EndpointManager] Received binary data ({0} bytes)", data.Length); return(data); }
public async Task CreateLetterFromDataAsync() { if (Letter == null) { Letter = JsonSerializer.Deserialize <Letter>(Data); } if (!_decrypted && Letter.LetterMetadata.Encrypted && _hashKey.Length > 0) { Letter.Body = AesEncrypt.Decrypt(Letter.Body, _hashKey); Letter.LetterMetadata.Encrypted = false; _decrypted = true; } if (!_decompressed && Letter.LetterMetadata.Compressed) { Letter.Body = await Gzip .DecompressAsync(Letter.Body) .ConfigureAwait(false); Letter.LetterMetadata.Compressed = false; _decompressed = true; } }