GetFormatter() public static method

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
return KeyFormatter
Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Auto-detect data format, read data and create key object
 /// </summary>
 /// <param name="aStream"></param>
 /// <returns></returns>
 public static ISshKey ReadSshKey(this Stream aStream,
                                  KeyFormatter.GetPassphraseCallback aGetPassphraseCallback = null)
 {
     using (var reader = new StreamReader(aStream)) {
         var firstLine = reader.ReadLine();
         var formatter = KeyFormatter.GetFormatter(firstLine);
         formatter.GetPassphraseCallbackMethod = aGetPassphraseCallback;
         aStream.Position = 0;
         return(formatter.Deserialize(aStream) as ISshKey);
     }
 }