private static string SecureInput(Dictionary <string, object> header, Dictionary <string, object> payload) { var encodeHeader = UrlBase64.Encode(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(header))); var encodePayload = UrlBase64.Encode(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(payload))); return(string.Format("{0}.{1}", encodeHeader, encodePayload)); }
/// <summary> /// Generates a Jws Signature. /// </summary> /// <param name="header"></param> /// <param name="payload"></param> /// <returns></returns> public string GenerateSignature(Dictionary <string, object> header, Dictionary <string, object> payload) { var securedInput = SecureInput(header, payload); var message = Encoding.UTF8.GetBytes(securedInput); byte[] hashedMessage; using (var sha256Hasher = SHA256.Create()) { hashedMessage = sha256Hasher.ComputeHash(message); } var signer = new ECDsaSigner(); signer.Init(true, _privateKey); var results = signer.GenerateSignature(hashedMessage); // Concated to create signature var a = results[0].ToByteArrayUnsigned(); var b = results[1].ToByteArrayUnsigned(); // a,b are required to be exactly the same length of bytes if (a.Length != b.Length) { var largestLength = Math.Max(a.Length, b.Length); a = ByteArrayPadLeft(a, largestLength); b = ByteArrayPadLeft(b, largestLength); } var signature = UrlBase64.Encode(a.Concat(b).ToArray()); return(string.Format("{0}.{1}", securedInput, signature)); }