Azure

In this tutorial, we are going to describe how to use and deploy DidiSoft OpenPGP Library for .NET on Microsoft Azure Cloud.

The steps below describe the deployment of the library in a Function App that reads and writes data to Azure Blob storage. A video and steps by step instructions follow below:

Transcript and Step by Step instructions

Using OpenPGP Library for .NET on Azure in a few words is no different from any other type of .NET project. We just reference the library assemblies and invoke the PGP cryptography methods.

But when using on Azure there are some DevOps steps that we have to perform beforehand in order to prepare the deployment environment.

We start by creating a Function App

The default settings are enough for our demo application. The “The runtime stack will be .NET Core. We will reuse an existing storage account and an exiting resource group. Application Insights are useful for tracing errors and exceptions.

Once the Function App has been created we can open Visual Studio and create a Function App from a template.

For this example, we will be using a Blob trigger, but developing another type of Function will be very much similar. Our code will take as input data uploaded to Blob storage (storage1) and will put this data OpenPGP encrypted into another Blob storage (storage2).

namespace MyPgpFunctions
{
    public static class FunctionPgp
    {
        [FunctionName("FunctionPgp")]
        public static void Run([BlobTrigger("storage1/{name}", Connection = "AzureWebJobsStorage")]Stream myBlob,
                               [Blob("storage2/{name}.pgp", FileAccess.Write, Connection = "AzureWebJobsStorage")]Stream outBlob,
                               string name, 
	                       ILogger log)
        {
        }
    }
}

Referencing DidiSoft.Pgp

In order to use the library we have to add reference to it. We can do that by getting it from NuGet. For a demo project we can use the trial version from nuget.org and for production our private NuGet repository must be used:

Install-Package DidiSoft.Pgp.Trial

Then we can modify the Blob trigger code by invoking the encryption method:

using DidiSoft.Pgp;
namespace MyPgpFunctions
{
    public static class FunctionPgp
    {
        [FunctionName("FunctionPgp")]
        public static void Run([BlobTrigger("storage1/{name}", Connection = "AzureWebJobsStorage")]Stream myBlob,
                               [Blob("storage2/{name}.pgp", FileAccess.Write, Connection = "AzureWebJobsStorage")]Stream outBlob,
                               string name, 
	                       ILogger log)
        {
            PGPLib pgp = new PGPLib();
            bool asciiArmor = true;
 
	    inlinePublicKey = "";
            pgp.EncryptStream(myBlob, name, inlinePublicKey, outBlob, asciiArmor);
        }
    }
}

The key can be hard coded for a test project, but for a real world scenario it shall be obtained from somewhere else.
Now we are ready to deploy the solution to Azure into the Function App that we have created at the beginning (mypgpfunc).

Once the application has been deployed we can upload a file in Azure Blob storage “storage1

And it will automatically become encrypted by the Blob trigger in storage2