Ejemplo n.º 1
0
        public static string EncodeBytes(byte[] payload, object key, JwsAlgorithm algorithm, IDictionary <string, object> extraHeaders = null, JwtSettings settings = null)
        {
            if (payload == null)
            {
                throw new ArgumentNullException(nameof(payload));
            }

            if (extraHeaders == null)             //allow overload, but keep backward compatible defaults
            {
                extraHeaders = new Dictionary <string, object> {
                    { "typ", "JWT" }
                };
            }

            var jwtHeader = new Dictionary <string, object> {
                { "alg", algorithm.ToString() }
            };

            Dictionaries.Append(jwtHeader, extraHeaders);
            byte[] headerBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(jwtHeader));

            var bytesToSign = Encoding.UTF8.GetBytes(Serialize(headerBytes, payload));

            var signer = SignerUtilities.GetSigner("SHA-384withECDSA");

            signer.Init(true, (ECPrivateKeyParameters)key);

            signer.BlockUpdate(bytesToSign, 0, bytesToSign.Length);
            byte[] signature = signer.GenerateSignature();

            return(Serialize(headerBytes, payload, transcodeSignatureToConcat(signature, 96)));
        }