@@ -163,8 +163,11 @@ use rand_core::CryptoRng;
163163#[ cfg( feature = "std" ) ]
164164use std:: { fs, path:: Path } ;
165165
166+ #[ cfg( feature = "std" ) ]
167+ use std:: io:: { self , Read , Write } ;
168+
166169#[ cfg( all( unix, feature = "std" ) ) ]
167- use std:: { io :: Write , os:: unix:: fs:: OpenOptionsExt } ;
170+ use std:: os:: unix:: fs:: OpenOptionsExt ;
168171
169172/// Error message for infallible conversions (used by `expect`)
170173const CONVERSION_ERROR_MSG : & str = "SSH private key conversion error" ;
@@ -343,15 +346,34 @@ impl PrivateKey {
343346
344347 /// Read private key from an OpenSSH-formatted PEM file.
345348 #[ cfg( feature = "std" ) ]
346- pub fn read_openssh_file ( path : & Path ) -> Result < Self > {
349+ pub fn read_openssh < R : Read > ( reader : & mut R ) -> Result < Self > {
350+ let pem = Zeroizing :: new ( io:: read_to_string ( reader) ?) ;
351+ Self :: from_openssh ( & * pem)
352+ }
353+
354+ /// Read private key from an OpenSSH-formatted PEM file.
355+ #[ cfg( feature = "std" ) ]
356+ pub fn read_openssh_file < P : AsRef < Path > > ( path : P ) -> Result < Self > {
347357 // TODO(tarcieri): verify file permissions match `UNIX_FILE_PERMISSIONS`
348358 let pem = Zeroizing :: new ( fs:: read_to_string ( path) ?) ;
349359 Self :: from_openssh ( & * pem)
350360 }
351361
352362 /// Write private key as an OpenSSH-formatted PEM file.
353363 #[ cfg( feature = "std" ) ]
354- pub fn write_openssh_file ( & self , path : & Path , line_ending : LineEnding ) -> Result < ( ) > {
364+ pub fn write_openssh < W : Write > ( & self , writer : & mut W , line_ending : LineEnding ) -> Result < ( ) > {
365+ let pem = self . to_openssh ( line_ending) ?;
366+ writer. write_all ( pem. as_bytes ( ) ) ?;
367+ Ok ( ( ) )
368+ }
369+
370+ /// Write private key as an OpenSSH-formatted PEM file.
371+ #[ cfg( feature = "std" ) ]
372+ pub fn write_openssh_file < P : AsRef < Path > > (
373+ & self ,
374+ path : P ,
375+ line_ending : LineEnding ,
376+ ) -> Result < ( ) > {
355377 let pem = self . to_openssh ( line_ending) ?;
356378
357379 #[ cfg( not( unix) ) ]
0 commit comments