private async Task SaveDbKeysToRepositoryAsync()
        {
            var dbKeys = await DataProtectionKeys.GetFromDbAsync(_settings.ConnectionString);

            var newKeys = dbKeys.Except(Keys);

            foreach (var key in newKeys)
            {
                _keyRepository.StoreElement(key.Element, key.FriendlyName);
            }
        }
예제 #2
0
        private IKey CreateNewKey(Guid keyId, DateTimeOffset creationDate, DateTimeOffset activationDate, DateTimeOffset expirationDate)
        {
            // <key id="{GUID}" version="1" xmlns="{XMLNS}">
            //   <creationDate>...</creationDate>
            //   <activationDate>...</activationDate>
            //   <expirationDate>...</expirationDate>
            //   <authenticatedEncryptor>
            //     <... parser="{TYPE}" />
            //   </authenticatedEncryptor>
            // </xxx:key>

            // Create the <xxx:authenticatedEncryptor /> element and make sure it's well-formed.
            var encryptorConfiguration = _authenticatedEncryptorConfigurationFactory.CreateNewConfiguration();
            var encryptorElementAsXml  = encryptorConfiguration.ToXml(_xmlEncryptor);

            CryptoUtil.Assert(!String.IsNullOrEmpty((string)encryptorElementAsXml.Attribute(ReaderAttributeName)), "!String.IsNullOrEmpty((string)encryptorElementAsXml.Attribute(ParserAttributeName))");

            // Create the <xxx:key /> element.
            var keyElement = new XElement(KeyElementName,
                                          new XAttribute(IdAttributeName, keyId),
                                          new XAttribute(VersionAttributeName, 1),
                                          new XElement(CreationDateElementName, creationDate),
                                          new XElement(ActivationDateElementName, activationDate),
                                          new XElement(ExpirationDateElementName, expirationDate),
                                          new XElement(AuthenticatedEncryptorElementName,
                                                       encryptorElementAsXml));

            // Persist it to the underlying repository
            string friendlyName = String.Format(CultureInfo.InvariantCulture, "key-{0:D}", keyId);

            _xmlRepository.StoreElement(keyElement, friendlyName);

            // And we're done!
            return(new Key(
                       keyId: keyId,
                       creationDate: creationDate,
                       activationDate: activationDate,
                       expirationDate: expirationDate,
                       encryptorConfiguration: encryptorConfiguration));
        }