示例#1
0
        public async ValueTask <ActionResult <ApiEncryptionModel> > GetEncrypted([FromBody] ApiEncryptionModel rawData)
        {
            string logHeader = $"[{DateTime.Now}] {GetGdprFriendlyAddress(Request.HttpContext.Connection.RemoteIpAddress)}:{Request.HttpContext.Connection.RemotePort} ->";

            using (_logger.BeginScope($"{logHeader} EncryptionController::GetEncrypted")) {
                if (string.IsNullOrWhiteSpace(rawData.Decrypted))
                {
                    _logger.LogInformation($"{logHeader} No decrypted data received.");
                    return(StatusCode(StatusCodes.Status422UnprocessableEntity));
                }

                try {
                    _logger.LogInformation($"{logHeader} Received {rawData.Decrypted.Length} bytes of data.");
                    string data = PadBase64String(rawData.Decrypted);
                    return(await Encrypt(data));
                } catch (Exception e) {
                    _logger.LogError(e, $"{logHeader} Encryption threw exception.");
#if DEBUG
                    // Keep this constrained to local debug builds to avoid having to setup a privacy policy
                    _logger.LogDebug($"{logHeader} Decrypted data was: {rawData.Decrypted}.");
#endif
                    return(StatusCode(StatusCodes.Status500InternalServerError));
                }
            }
        }
        private static async ValueTask <ApiEncryptionModel> CallEndpoint(ApiEncryptionModel input, string endpoint)
        {
            using HttpContent content             = new StringContent(JsonSerializer.Serialize(input));
            content.Headers.ContentType.MediaType = "application/json";
            HttpResponseMessage response = await Client.PostAsync(endpoint, content);

            return(JsonSerializer.Deserialize <ApiEncryptionModel>(await response.Content.ReadAsStringAsync(), SerializerOptions));
        }
        /// <inheritdoc />
        public async ValueTask <Stream> EncryptAsync(Stream decrypted)
        {
            ApiEncryptionModel model = new ApiEncryptionModel {
                Decrypted = await GetBase64Data(decrypted)
            };
            ApiEncryptionModel response = await CallEndpoint(model, "api/encryption/encrypt");

            MemoryStream output = new MemoryStream(Convert.FromBase64String(PadBase64String(response.Encrypted !)));

            return(output);
        }