Beispiel #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);
        }
Beispiel #2
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;
            }
        }