dlech.SshAgentLib.KeyFormatter.GetFormatter C# (CSharp) Метод

GetFormatter() публичный статический Метод

Attempts to return a Formatter that can deserialize data given the specified first line
/// The file format was not recognized ///
public static GetFormatter ( string firstLine ) : KeyFormatter
firstLine string first line of data to be deserialized
Результат KeyFormatter
        public static KeyFormatter GetFormatter(string firstLine)
        {
            // PuTTY Private key format
              var ppkRegex = new Regex ("PuTTY-User-Key-File-[12]");
              // OpenSSH private key format
              var pemPrivateKeyRegex = new Regex ("-----BEGIN .* PRIVATE KEY-----");

              if (!string.IsNullOrWhiteSpace (firstLine)) {
            if (ppkRegex.IsMatch (firstLine)) {
              return new PpkFormatter ();
            } else if (OpensshKeyFormatter.MARK_BEGIN == firstLine) {
              return new OpensshKeyFormatter();
            } else if (pemPrivateKeyRegex.IsMatch(firstLine)) {
              return new PemKeyFormatter ();
            } else if (Ssh1KeyFormatter.FILE_HEADER_LINE == firstLine) {
              return new Ssh1KeyFormatter ();
            }
              }
              throw new KeyFormatterException ("Unknown file format");
        }

Usage Example

Пример #1
0
        /// <summary>
        /// Reads file and loads key into agent
        /// </summary>
        /// <param name="aAgent">the agent</param>
        /// <param name="aFileName">pathname of file to read</param>
        /// <param name="aGetPassPhraseCallback">method that returns passphrase</param>
        /// <param name="aConstraints">additional constraints</param>
        /// <exception cref="AgentFailureException">
        /// Agent returned SSH_AGENT_FAILURE
        /// </exception>
        /// <exception cref="KeyFormatterException">
        /// File format was not recognized
        /// </exception>
        /// <exception cref="UnauthorizedAccessException"></exception>
        /// <exception cref="ArgumentException"></exception>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="PathTooLongException"></exception>
        /// <exception cref="DirectoryNotFoundException"></exception>
        /// <exception cref="FileNotFoundException"></exception>
        /// <exception cref="NotSupportedException"></exception>
        /// <returns>The ssh key that was read from the file</returns>
        public static ISshKey AddKeyFromFile(this IAgent aAgent, string aFileName,
                                             KeyFormatter.GetPassphraseCallback aGetPassPhraseCallback,
                                             ICollection <Agent.KeyConstraint> aConstraints = null)
        {
            string firstLine;

            using (var fileReader = File.OpenText(aFileName)) {
                firstLine = fileReader.ReadLine();
            }
            var formatter = KeyFormatter.GetFormatter(firstLine);

            formatter.GetPassphraseCallbackMethod = aGetPassPhraseCallback;
            var key = formatter.DeserializeFile(aFileName);

            if (aConstraints != null)
            {
                foreach (var constraint in aConstraints)
                {
                    key.AddConstraint(constraint);
                }
            }
            // prevent error in Pageant by attempting to remove key before adding it
            // this makes behavior more consistent with OpenSSH
            if (aAgent is PageantClient)
            {
                try {
                    aAgent.RemoveKey(key);
                } catch (Exception) { /* error will occur if key is not loaded */ }
            }
            aAgent.AddKey(key);
            return(key);
        }
All Usage Examples Of dlech.SshAgentLib.KeyFormatter::GetFormatter