/// <summary> /// Computes a JSON Web Signature (JWS) according to the rules of RFC 7515 Section 5. /// </summary> /// <param name="sigFunc"></param> /// <param name="payload"></param> /// <param name="protectedHeaders"></param> /// <param name="unprotectedHeaders"></param> /// <returns></returns> public static JwsSigned SignFlatJsonAsObject(Func <byte[], byte[]> sigFunc, string payload, object protectedHeaders = null, object unprotectedHeaders = null) { if (protectedHeaders == null && unprotectedHeaders == null) { throw new ArgumentException("at least one of protected or unprotected headers must be specified"); } string protectedHeadersSer = ""; if (protectedHeaders != null) { protectedHeadersSer = JsonConvert.SerializeObject( protectedHeaders, Formatting.None); } string payloadB64u = Base64UrlEncode(Encoding.UTF8.GetBytes(payload)); string protectedB64u = Base64UrlEncode(Encoding.UTF8.GetBytes(protectedHeadersSer)); string signingInput = $"{protectedB64u}.{payloadB64u}"; byte[] signingBytes = Encoding.ASCII.GetBytes(signingInput); byte[] sigBytes = sigFunc(signingBytes); string sigB64u = Base64UrlEncode(sigBytes); var jwsFlatJS = new JwsSigned { header = unprotectedHeaders, @protected = protectedB64u, payload = payloadB64u, signature = sigB64u }; return(jwsFlatJS); }
/// <summary> /// Computes a JSON Web Signature (JWS) according to the rules of RFC 7515 Section 5. /// </summary> /// <param name="sigFunc"></param> /// <param name="payload"></param> /// <param name="protectedHeaders"></param> /// <param name="unprotectedHeaders"></param> /// <returns></returns> public static JwsSigned SignFlatJsonAsObject(Func<byte[], byte[]> sigFunc, string payload, object protectedHeaders = null, object unprotectedHeaders = null) { if (protectedHeaders == null && unprotectedHeaders == null) throw new ArgumentException("at least one of protected or unprotected headers must be specified"); string protectedHeadersSer = ""; if (protectedHeaders != null) { protectedHeadersSer = JsonConvert.SerializeObject( protectedHeaders, Formatting.None); } string payloadB64u = Base64UrlEncode(Encoding.UTF8.GetBytes(payload)); string protectedB64u = Base64UrlEncode(Encoding.UTF8.GetBytes(protectedHeadersSer)); string signingInput = $"{protectedB64u}.{payloadB64u}"; byte[] signingBytes = Encoding.ASCII.GetBytes(signingInput); byte[] sigBytes = sigFunc(signingBytes); string sigB64u = Base64UrlEncode(sigBytes); var jwsFlatJS = new JwsSigned { header = unprotectedHeaders, @protected = protectedB64u, payload = payloadB64u, signature = sigB64u }; return jwsFlatJS; }