Skip to content

Instantly share code, notes, and snippets.

@Clancey
Created June 8, 2017 19:23
Show Gist options
  • Save Clancey/dafac69f49c2e00dfe91ec57642499d8 to your computer and use it in GitHub Desktop.
Save Clancey/dafac69f49c2e00dfe91ec57642499d8 to your computer and use it in GitHub Desktop.
public static string SignES256(string privateKey, string header, string payload)
{
CngKey key = CngKey.Import(
Convert.FromBase64String(privateKey),
CngKeyBlobFormat.Pkcs8PrivateBlob);
using (ECDsaCng dsa = new ECDsaCng(key))
{
dsa.HashAlgorithm = CngAlgorithm.Sha256;
var unsignedJwtData =
Encode(Encoding.UTF8.GetBytes(header)) + "." + Encode(Encoding.UTF8.GetBytes(payload));
var signature =
dsa.SignData(Encoding.UTF8.GetBytes(unsignedJwtData));
return unsignedJwtData + "." + Encode(signature);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment