コード例 #1
0
ファイル: Program.cs プロジェクト: tuongntk/LitJWT
        public DecryptionTypeEncode()
        {
            var rsaParams = RSA.Create().ExportParameters(true);

            payload = new Payload {
                hoge = "hugahuga", hage = "nanonano"
            };

            var ehs256 = new LitJWT.JwtEncoder(new LitJWT.Algorithms.HS256Algorithm(LitJWT.Algorithms.HS256Algorithm.GenerateRandomRecommendedKey()));
            var ehs384 = new LitJWT.JwtEncoder(new LitJWT.Algorithms.HS384Algorithm(LitJWT.Algorithms.HS384Algorithm.GenerateRandomRecommendedKey()));
            var ehs512 = new LitJWT.JwtEncoder(new LitJWT.Algorithms.HS512Algorithm(LitJWT.Algorithms.HS512Algorithm.GenerateRandomRecommendedKey()));
            var ers256 = new LitJWT.JwtEncoder(new LitJWT.Algorithms.RS256Algorithm(() => RSA.Create(rsaParams), () => RSA.Create(rsaParams)));
            var ers384 = new LitJWT.JwtEncoder(new LitJWT.Algorithms.RS384Algorithm(() => RSA.Create(rsaParams), () => RSA.Create(rsaParams)));
            var ers512 = new LitJWT.JwtEncoder(new LitJWT.Algorithms.RS512Algorithm(() => RSA.Create(rsaParams), () => RSA.Create(rsaParams)));

            hs256 = new LitJWT.JwtDecoder(ehs256.SignAlgorithm);
            hs384 = new LitJWT.JwtDecoder(ehs384.SignAlgorithm);
            hs512 = new LitJWT.JwtDecoder(ehs512.SignAlgorithm);
            rs256 = new LitJWT.JwtDecoder(ers256.SignAlgorithm);
            rs384 = new LitJWT.JwtDecoder(ers384.SignAlgorithm);
            rs512 = new LitJWT.JwtDecoder(ers512.SignAlgorithm);

            b1 = ehs256.EncodeAsUtf8Bytes(payload, null, (x, writer) => writer.Write(Utf8Json.JsonSerializer.SerializeUnsafe(x)));
            b2 = ehs384.EncodeAsUtf8Bytes(payload, null, (x, writer) => writer.Write(Utf8Json.JsonSerializer.SerializeUnsafe(x)));
            b3 = ehs512.EncodeAsUtf8Bytes(payload, null, (x, writer) => writer.Write(Utf8Json.JsonSerializer.SerializeUnsafe(x)));
            b4 = ers256.EncodeAsUtf8Bytes(payload, null, (x, writer) => writer.Write(Utf8Json.JsonSerializer.SerializeUnsafe(x)));
            b5 = ers384.EncodeAsUtf8Bytes(payload, null, (x, writer) => writer.Write(Utf8Json.JsonSerializer.SerializeUnsafe(x)));
            b6 = ers512.EncodeAsUtf8Bytes(payload, null, (x, writer) => writer.Write(Utf8Json.JsonSerializer.SerializeUnsafe(x)));
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: tuongntk/LitJWT
        public JwtEncode()
        {
            key           = Encoding.UTF8.GetBytes("hogehogehogehoge");
            litJwtEncoder = new LitJWT.JwtEncoder(new LitJWT.Algorithms.HS256Algorithm(key));
            jwtEncoder    = new JWT.JwtEncoder(new JWT.Algorithms.HMACSHA256Algorithm(), new JWT.Serializers.JsonNetSerializer(), new JWT.JwtBase64UrlEncoder());

            jwtHandler = new JwtSecurityTokenHandler()
            {
                SetDefaultTimesOnTokenCreation = false
            };
            handlerKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(key);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: tuongntk/LitJWT
        public EncryptionTypeEncode()
        {
            payload = new Payload {
                hoge = "hugahuga", hage = "nanonano"
            };
            var rsaParams = RSA.Create().ExportParameters(true);

            hs256 = new LitJWT.JwtEncoder(new LitJWT.Algorithms.HS256Algorithm(LitJWT.Algorithms.HS256Algorithm.GenerateRandomRecommendedKey()));
            hs384 = new LitJWT.JwtEncoder(new LitJWT.Algorithms.HS384Algorithm(LitJWT.Algorithms.HS384Algorithm.GenerateRandomRecommendedKey()));
            hs512 = new LitJWT.JwtEncoder(new LitJWT.Algorithms.HS512Algorithm(LitJWT.Algorithms.HS512Algorithm.GenerateRandomRecommendedKey()));
            rs256 = new LitJWT.JwtEncoder(new LitJWT.Algorithms.RS256Algorithm(() => RSA.Create(rsaParams), () => RSA.Create(rsaParams)));
            rs384 = new LitJWT.JwtEncoder(new LitJWT.Algorithms.RS384Algorithm(() => RSA.Create(rsaParams), () => RSA.Create(rsaParams)));
            rs512 = new LitJWT.JwtEncoder(new LitJWT.Algorithms.RS512Algorithm(() => RSA.Create(rsaParams), () => RSA.Create(rsaParams)));
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: tuongntk/LitJWT
        static void Main(string[] args)
        {
            //// Get recommended-size random key.
            //var key = HS256Algorithm.GenerateRandomRecommendedKey();

            //// Create encoder, JwtEncoder is thread-safe and recommend to store static/singleton.
            //var encoder = new JwtEncoder(new HS256Algorithm(key));

            //// Encode with payload, expire, and use specify payload serializer.
            //var token = encoder.Encode(new PayloadSample { foo = "pay", bar = "load" }, TimeSpan.FromMinutes(30),
            //    (x, writer) => writer.Write(Utf8Json.JsonSerializer.SerializeUnsafe(x)));

            //var token2 = encoder.Encode(new PayloadSample { foo = "pay", bar = "load" }, TimeSpan.FromMinutes(30),
            //    (x, writer) => writer.Write(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(x))));


            ////byte[] input;
            ////Convert.ToBase64String(input).TrimEnd('=').Replace('+', '-').Replace('/', '_');



            //// Create decoder, JwtDecoder is also thread-safe so recommend to store static/singleton.
            //var decoder = new JwtDecoder(encoder.SignAlgorithm);
            ////var result = decoder.TryDecode(token, x => Utf8Json.JsonSerializer.Deserialize<PayloadSample>(x.ToArray()), out var payload);

            //var result = decoder.TryDecode(token, x => JsonConvert.DeserializeObject<PayloadSample>(Encoding.UTF8.GetString(x)), out var payload);

            //if (result == DecodeResult.Success)
            //{
            //    Console.WriteLine((payload.foo, payload.bar));
            //}

            //Console.WriteLine(token);

            var payload   = new  { hoge = "hugahuga", hage = "nanonano" };
            var rsaParams = RSA.Create().ExportParameters(true);
            var rs256     = new LitJWT.JwtEncoder(new LitJWT.Algorithms.RS256Algorithm(() => RSA.Create(rsaParams), () => RSA.Create(rsaParams)));
            var foo       = rs256.Encode(payload, null, (x, writer) => writer.Write(Utf8Json.JsonSerializer.SerializeUnsafe(x)));
        }