@@ -95,7 +95,43 @@ module.exports = {
9595 } ,
9696
9797 publicImport : function ( key , data , options ) {
98- throw Error ( 'Not implemented yet.' ) ;
98+ options = options || { } ;
99+ var buffer ;
100+
101+ if ( options . type !== 'der' ) {
102+ if ( Buffer . isBuffer ( data ) ) {
103+ data = data . toString ( 'utf8' ) ;
104+ }
105+
106+ if ( _ . isString ( data ) ) {
107+ if ( data . substring ( 0 , 8 ) !== 'ssh-rsa ' )
108+ throw Error ( 'Unsupported key format' ) ;
109+ var pem = data . substring ( 8 , data . indexOf ( ' ' , 8 ) )
110+ . replace ( / \s + | \n \r | \n | \r $ / gm, '' ) ;
111+ buffer = Buffer . from ( pem , 'base64' ) ;
112+ } else {
113+ throw Error ( 'Unsupported key format' ) ;
114+ }
115+ } else if ( Buffer . isBuffer ( data ) ) {
116+ buffer = data ;
117+ } else {
118+ throw Error ( 'Unsupported key format' ) ;
119+ }
120+
121+ const reader = { buf :buffer , off :0 } ;
122+
123+ const type = readOpenSSHKeyString ( reader ) . toString ( 'ascii' ) ;
124+
125+ if ( type !== 'ssh-rsa' )
126+ throw Error ( 'Invalid key type' ) ;
127+
128+ const e = readOpenSSHKeyString ( reader ) ;
129+ const n = readOpenSSHKeyString ( reader ) ;
130+
131+ key . setPublic (
132+ n ,
133+ e
134+ ) ;
99135 } ,
100136
101137 /**
@@ -110,6 +146,11 @@ module.exports = {
110146 return true ;
111147 }
112148
149+ if ( / ^ [ \S \s ] * s s h - r s a \s * (? = ( ( [ A - Z a - z 0 - 9 + / = ] + \s * ) + ) ) \1[ \S \s ] * $ / g. test ( data ) ) {
150+ module . exports . publicImport ( key , data ) ;
151+ return true ;
152+ }
153+
113154 return false ;
114155 }
115156} ;
0 commit comments