Skip to content

Instantly share code, notes, and snippets.

@alexzautke
Last active January 1, 2023 15:59
Show Gist options
  • Save alexzautke/5aafda0cb1da8f17d0a8973512a066e9 to your computer and use it in GitHub Desktop.
Save alexzautke/5aafda0cb1da8f17d0a8973512a066e9 to your computer and use it in GitHub Desktop.
CreativeCode.JWS HOW-TO
using System;
using System.Text;
using CreativeCode.JWS;
using CreativeCode.JWK;
using CreativeCode.JWK.KeyParts;
namespace JWS_Run
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Implementation of RFC7515 (JSON Web Signature)");
// A signature key (RSA/EC/OCT) is needed. This implementation always uses JWKs (RFC7517) to supply a key.
var keyUse = PublicKeyUse.Signature;
var keyOperations = new HashSet<KeyOperation>(new[] {KeyOperation.ComputeDigitalSignature, KeyOperation.VerifyDigitalSignature});
var algorithm = Algorithm.ES256;
var jwk = new JWK(algorithm, keyUse, keyOperations);
// Provide custom Content-Type and content. "application/fhir+json" is only choosen as an example.
// Create header based on supplied information. Exceptions may be thrown if required content is not proivided by the JWKProvider
var joseHeader = new ProtectedJoseHeader(jwk, "application/fhir+json", SerializationOption.JwsCompactSerialization);
var payload = Encoding.UTF8.GetBytes("payload");
// Initialize JWS
var jws = new JWS(new []{joseHeader}, payload);
// Create digital signature
jws.CalculateSignature();
var jwsSignature = jws.Export();
Console.WriteLine("Created JSON Web Signature: " + jwsSignature);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment