Exemple #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);
        }
Exemple #2
0
 public object Clone()
 {
     return(new JsonWebKey
     {
         Alg = Alg,
         KeyOperationLst = KeyOperationLst.Select(s => (JsonWebKeyKeyOperation)s.Clone()).ToList(),
         Use = Use,
         Kid = Kid,
         Kty = Kty,
         Content = Content == null ? new Dictionary <string, string>() : Content.ToDictionary(s => s.Key, s => s.Value),
         RotationJWKId = RotationJWKId,
         ExpirationDateTime = ExpirationDateTime
     });
 }