예제 #1
0
        /// <summary>
        /// Get the content from an encrypted password file.
        /// </summary>
        /// <param name="file">A <see cref="PasswordFile"/> specifying the password file to be decrypted.</param>
        /// <param name="passwordOnFirstLine">Should be true if the first line of the file contains the password.
        /// Any content in the remaining lines will be considered metadata.
        /// If set to false, the contents of the entire file are considered to be the password.</param>
        public KeyedPasswordFile DecryptPassword(PasswordFile file, bool passwordOnFirstLine)
        {
            if (!file.FileInfo.Exists)
            {
                throw new ArgumentException($"The password file \"{file.FullPath}\" does not exist.");
            }

            var content = cryptoService.Decrypt(file.FullPath);

            return(passwordFileParser.Parse(file, content, !passwordOnFirstLine));
        }
예제 #2
0
        public string DecryptText(PasswordFile file)
        {
            if (!File.Exists(file.FullPath))
            {
                throw new ArgumentException($"The password file \"{file.FullPath}\" does not exist.");
            }

            if (PinentryFixEnabled)
            {
                pinentryWatcher.BumpPinentryWindow();
            }
            return(Crypto.Decrypt(file.FullPath));
        }
        public string[] FindRecipients(PasswordFile file)
        {
            var current = file.Directory;

            // Walk up from the innermost directory, and keep moving up until an existing directory
            // containing a gpg-id file is found.
            while (!current.Exists || !current.ContainsFile(GpgIdFileName))
            {
                if (current.Parent == null || current.PathEquals(passwordStore))
                {
                    return(Array.Empty <string>());
                }
                current = current.Parent;
            }

            return(fileSystem.File.ReadAllLines(fileSystem.Path.Combine(current.FullName, GpgIdFileName)));
        }
        /// <summary>
        /// Extracts the username and any possible metadata from a password file
        /// by auto-detecting the correct line-endings.
        /// </summary>
        /// <param name="file">A <see cref="PasswordFile"/> specifying the file to be decrypted.</param>
        /// <param name="content">Content of the password file</param>
        /// <param name="entireFile">If set to true, any line endings are considered to be part of the password.</param>
        /// <returns>A <see cref="KeyedPasswordFile"/> structure containing the password and metadata</returns>
        public KeyedPasswordFile Parse(PasswordFile file, string content, bool entireFile)
        {
            if (entireFile)
            {
                // If the password contains any line endings, there is no additional metadata available.
                return(new KeyedPasswordFile(file, content, null, null));
            }
            else
            {
                // The first line contains the password, any other lines contain additional (contextual) content.
                var match    = Regex.Match(content, @"([^\n\r]*)(?:(?:\r\n|\n|\r)(.*))?", RegexOptions.Singleline);
                var password = match.Groups[1].Value;
                var metadata = match.Groups[2].Value;

                var keys = ExtractKeys(metadata);

                return(new KeyedPasswordFile(file, password, metadata, keys.ToList()));
            }
        }
예제 #5
0
 public PasswordFile(PasswordFile original)
 {
     FileInfo      = original.FileInfo;
     PasswordStore = original.PasswordStore;
 }
예제 #6
0
        /// <summary>
        /// Get the content from an encrypted password file.
        /// </summary>
        /// <param name="file">A <see cref="PasswordFile"/> specifying the password file to be decrypted.</param>
        /// <param name="passwordOnFirstLine">Should be true if the first line of the file contains the password.
        /// Any content in the remaining lines will be considered metadata.
        /// If set to false, the contents of the entire file are considered to be the password.</param>
        /// <returns></returns>
        public KeyedPasswordFile DecryptPassword(PasswordFile file, bool passwordOnFirstLine)
        {
            var content = DecryptText(file);

            return(new PasswordFileParser().Parse(file, content, !passwordOnFirstLine));
        }
예제 #7
0
 public KeyedPasswordFile(PasswordFile original, string password, string metadata, List <KeyValuePair <string, string> > keys)
     : base(original, password, metadata)
 {
     // The keys list should always be initialised.
     Keys = keys ?? new List <KeyValuePair <string, string> >();
 }