Skip to content

Instantly share code, notes, and snippets.

@lfalck
Created June 27, 2019 09:53
Show Gist options
  • Save lfalck/ce6e775d9e171ba3096af5cbff269024 to your computer and use it in GitHub Desktop.
Save lfalck/ce6e775d9e171ba3096af5cbff269024 to your computer and use it in GitHub Desktop.
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using PgpCore;
using System.Threading.Tasks;
using System.Net.Http;
using System;
using System.Text;
using System.Collections.Concurrent;
using Microsoft.Extensions.Logging;
namespace AzureFunctionsPGPDecrypt
{
public static class PGPDecrypt
{
private static HttpClient client = new HttpClient();
private static ConcurrentDictionary<string, string> secrects = new ConcurrentDictionary<string, string>();
[FunctionName(nameof(PGPDecrypt))]
public static async Task<IActionResult> RunAsync(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]
HttpRequest req, ILogger log)
{
log.LogInformation($"C# HTTP trigger function {nameof(PGPDecrypt)} processed a request.");
string privateKeyBase64 = req.Query["privateKeyBase64"];
byte[] data = Convert.FromBase64String(privateKeyBase64);
string privateKey = Encoding.UTF8.GetString(data);
Stream decryptedData = await DecryptAsync(req.Body, privateKey);
return new OkObjectResult(decryptedData);
}
private static async Task<Stream> DecryptAsync(Stream inputStream, string privateKey)
{
using (PGP pgp = new PGP())
{
Stream outputStream = new MemoryStream();
using (inputStream)
using (Stream privateKeyStream = GenerateStreamFromString(privateKey))
{
await pgp.DecryptStreamAsync(inputStream, outputStream, privateKeyStream, null);
outputStream.Seek(0, SeekOrigin.Begin);
return outputStream;
}
}
}
private static Stream GenerateStreamFromString(string s)
{
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment