예제 #1
0
        public JsonWebKey Rotate(int expirationTimeInSeconds)
        {
            var result = new JsonWebKey
            {
                Kid             = Guid.NewGuid().ToString(),
                Alg             = Alg,
                KeyOperationLst = KeyOperationLst.Select(k => new JsonWebKeyKeyOperation
                {
                    Operation = k.Operation
                }).ToList(),
                Kty     = Kty,
                Use     = Use,
                Content = new Dictionary <string, string>()
            };

            switch (result.Kty)
            {
            case KeyTypes.RSA:
                using (var rsa = RSA.Create())
                {
                    foreach (var kvp in rsa.ExtractPublicKey())
                    {
                        result.Content.Add(kvp.Key, kvp.Value);
                    }

                    foreach (var kvp in rsa.ExtractPrivateKey())
                    {
                        result.Content.Add(kvp.Key, kvp.Value);
                    }
                }
                break;

            case KeyTypes.EC:
                using (var ec = new ECDsaCng())
                {
                    foreach (var kvp in ec.ExtractPublicKey())
                    {
                        result.Content.Add(kvp.Key, kvp.Value);
                    }

                    foreach (var kvp in ec.ExtractPrivateKey())
                    {
                        result.Content.Add(kvp.Key, kvp.Value);
                    }
                }
                break;

            case KeyTypes.OCT:
                using (var ec = new HMACSHA256())
                {
                    result.Content = ec.ExportKey();
                }
                break;
            }

            RotationJWKId      = result.Kid;
            ExpirationDateTime = DateTime.UtcNow.AddSeconds(expirationTimeInSeconds);
            return(result);
        }
예제 #2
0
        public JsonWebKeySignBuilder SetAlg(ECDsaCng ec, string algName)
        {
            _jsonWebKey.Alg = algName;
            _jsonWebKey.Kty = KeyTypes.EC;
            foreach (var kvp in ec.ExtractPublicKey())
            {
                _jsonWebKey.Content.Add(kvp.Key, kvp.Value);
            }

            foreach (var kvp in ec.ExtractPrivateKey())
            {
                _jsonWebKey.Content.Add(kvp.Key, kvp.Value);
            }

            return(this);
        }
예제 #3
0
        public void Renew()
        {
            switch (Kty)
            {
            case KeyTypes.RSA:
                using (var rsa = RSA.Create())
                {
                    foreach (var kvp in rsa.ExtractPublicKey())
                    {
                        Content.Add(kvp.Key, kvp.Value);
                    }

                    foreach (var kvp in rsa.ExtractPrivateKey())
                    {
                        Content.Add(kvp.Key, kvp.Value);
                    }
                }
                break;

            case KeyTypes.EC:
                using (var ec = new ECDsaCng())
                {
                    foreach (var kvp in ec.ExtractPublicKey())
                    {
                        Content.Add(kvp.Key, kvp.Value);
                    }

                    foreach (var kvp in ec.ExtractPrivateKey())
                    {
                        Content.Add(kvp.Key, kvp.Value);
                    }
                }
                break;

            case KeyTypes.OCT:
                using (var ec = new HMACSHA256())
                {
                    Content = ec.ExportKey();
                }
                break;
            }
        }