Exemple #1
0
        public static string EncodeBytes(byte[] payload, object key, JwsAlgorithm algorithm, IDictionary <string, object> extraHeaders = null)
        {
            if (payload == null)
            {
                throw new ArgumentNullException("payload");
            }
            if (extraHeaders == null)
            {
                extraHeaders = new Dictionary <string, object>()
                {
                    { "typ", "JWT" }
                };
            }
            Dictionary <string, object> strs = new Dictionary <string, object>()
            {
                { "alg", JWT.JwsAlgorithms[algorithm] }
            };

            Dictionaries.Append <string, object>(strs, extraHeaders);
            byte[] bytes    = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(strs));
            byte[] numArray = Encoding.UTF8.GetBytes(Compact.Serialize(new byte[][] { bytes, payload }));
            //byte[] numArray1 = JWT.HashAlgorithms[algorithm].Sign(numArray, key);
            SHA512 sha1 = SHA512.Create();

            byte[] numArray1 = sha1.ComputeHash(numArray);
            //System.Security.Cryptography.SHA512Cng  --
            return(Compact.Serialize(new byte[][] { bytes, payload, numArray1 }));
        }
Exemple #2
0
        public static string EncodeBytes(byte[] payload, object key, JweAlgorithm alg, JweEncryption enc, JweCompression?compression = null, IDictionary <string, object> extraHeaders = null)
        {
            if (payload == null)
            {
                throw new ArgumentNullException("payload");
            }
            IKeyManagement item               = JWT.KeyAlgorithms[alg];
            IJweAlgorithm  jweAlgorithm       = JWT.EncAlgorithms[enc];
            IDictionary <string, object> strs = new Dictionary <string, object>()
            {
                { "alg", JWT.JweAlgorithms[alg] },
                { "enc", JWT.JweEncryptionMethods[enc] }
            };

            Dictionaries.Append <string, object>(strs, extraHeaders);
            byte[][] numArray  = item.WrapNewKey(jweAlgorithm.KeySize, key, strs);
            byte[]   numArray1 = numArray[0];
            byte[]   numArray2 = numArray[1];
            if (compression.HasValue)
            {
                strs["zip"] = JWT.JweCompressionMethods[compression.Value];
                payload     = JWT.CompressionAlgorithms[compression.Value].Compress(payload);
            }
            byte[]   bytes     = Encoding.UTF8.GetBytes(JWT.jsMapper.Serialize(strs));
            byte[]   bytes1    = Encoding.UTF8.GetBytes(Compact.Serialize(new byte[][] { bytes }));
            byte[][] numArray3 = jweAlgorithm.Encrypt(bytes1, payload, numArray1);
            return(Compact.Serialize(new byte[][] { bytes, numArray2, numArray3[0], numArray3[1], numArray3[2] }));
        }
Exemple #3
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)));
        }
        public void AppendNull()
        {
            //given
            var src = new Dictionary <string, string> {
                { "one", "1" }, { "two", "2" }
            };

            //when
            Dictionaries.Append(src, null);

            //then
            Assert.Equal(src.Count, 2);
            Assert.Equal(src["one"], "1");
            Assert.Equal(src["two"], "2");
        }
Exemple #5
0
        public void AppendNull()
        {
            //given
            var src = new Dictionary <string, string> {
                { "one", "1" }, { "two", "2" }
            };

            //when
            Dictionaries.Append(src, null);

            //then
            Assert.That(src, Has.Count.EqualTo(2));
            Assert.That(src["one"], Is.EqualTo("1"));
            Assert.That(src["two"], Is.EqualTo("2"));
        }
        public void Append()
        {
            //given
            var src = new Dictionary <string, string> {
                { "one", "1" }, { "two", "2" }
            };
            var other = new Dictionary <string, string> {
                { "three", "3" }, { "two", "3" }
            };

            //when
            Dictionaries.Append(src, other);

            //then
            Assert.Equal(src.Count, 3);
            Assert.Equal(src["one"], "1");
            Assert.Equal(src["two"], "2");
            Assert.Equal(src["three"], "3");
        }
Exemple #7
0
        public void Append()
        {
            //given
            var src = new Dictionary <string, string> {
                { "one", "1" }, { "two", "2" }
            };
            var other = new Dictionary <string, string> {
                { "three", "3" }, { "two", "3" }
            };

            //when
            Dictionaries.Append(src, other);

            //then
            Assert.That(src, Has.Count.EqualTo(3));
            Assert.That(src["one"], Is.EqualTo("1"));
            Assert.That(src["two"], Is.EqualTo("2"));
            Assert.That(src["three"], Is.EqualTo("3"));
        }