Example #1
0
        /// <summary>
        /// A wrapper around Convert.FromBase64String, with extra validation and relevant exception messages.
        /// </summary>
        private static byte[] GetKeyFromBase64(string base64, int defaultEncryptionKeySize)
        {
            if (string.IsNullOrWhiteSpace(base64))
            {
                throw new ConfigurationErrorsException("The " + Constants.EncryptionKeySetting + " setting must be set to an encryption key. "
                                                       + "The key should be in base 64, and should be at least " + Constants.MinimumAcceptableEncryptionKeyLength
                                                       + " bytes long. You may use EncryptionSettings.GenerateRandomEncryptionKey() to generate a key.\n"
                                                       + "If you'd like, here's a key that was randomly generated:\n"
                                                       + "<add key=\"Raven/Encryption/Key\" value=\""
                                                       + Convert.ToBase64String(EncryptionSettings.GenerateRandomEncryptionKey(defaultEncryptionKeySize))
                                                       + "\" />");
            }

            try
            {
                var result = Convert.FromBase64String(base64);
                if (result.Length < Constants.MinimumAcceptableEncryptionKeyLength)
                {
                    throw new ConfigurationErrorsException("The " + Constants.EncryptionKeySetting + " setting must be at least "
                                                           + Constants.MinimumAcceptableEncryptionKeyLength + " bytes long.");
                }

                return(result);
            }
            catch (FormatException e)
            {
                throw new ConfigurationErrorsException("The " + Constants.EncryptionKeySetting + " setting has an invalid base 64 value.", e);
            }
        }