@@ -94,62 +94,23 @@ const getPemFileContent = (filePath: string, filePassword: string): string => {
9494 }
9595} ;
9696
97- const getCertificate = ( pemFileContent : string ) : string | undefined => {
98- let certificate ;
99- const beginMark = '-----BEGIN CERTIFICATE-----' ;
100- const beginIndex = pemFileContent . indexOf ( beginMark ) + beginMark . length ;
101- if ( beginIndex > - 1 ) {
102- const endMark = '-----END CERTIFICATE-----' ;
103- const endIndex = pemFileContent . indexOf ( endMark , beginIndex ) ;
104- if ( endIndex > - 1 ) {
105- certificate = pemFileContent . slice ( beginIndex , endIndex ) . replace ( / \s / g, '' ) ;
106- return beginMark + os . EOL + certificate + os . EOL + endMark ;
107- }
108- }
109- return certificate ;
97+ const extractPemBlock = ( pemFileContent : string , beginMark : string , endMark : string ) : string | undefined => {
98+ const rawIndex = pemFileContent . indexOf ( beginMark ) ;
99+ if ( rawIndex === - 1 ) return undefined ;
100+ const beginIndex = rawIndex + beginMark . length ;
101+ const endIndex = pemFileContent . indexOf ( endMark , beginIndex ) ;
102+ if ( endIndex === - 1 ) return undefined ;
103+ const content = pemFileContent . slice ( beginIndex , endIndex ) . replace ( / \s / g, '' ) ;
104+ return beginMark + os . EOL + content + os . EOL + endMark ;
110105} ;
111106
112- const getPrivateKey = ( pemFileContent : string ) : string | undefined => {
113- let privateKey ;
114- const beginMark = '-----BEGIN PRIVATE KEY-----' ;
115- const beginIndex = pemFileContent . indexOf ( beginMark ) + beginMark . length ;
116- if ( beginIndex > - 1 ) {
117- const endMark = '-----END PRIVATE KEY-----' ;
118- const endIndex = pemFileContent . indexOf ( endMark , beginIndex ) ;
119- if ( endIndex > - 1 ) {
120- privateKey = pemFileContent . slice ( beginIndex , endIndex ) . replace ( / \s / g, '' ) ;
121- return beginMark + os . EOL + privateKey + os . EOL + endMark ;
122- }
123- }
124- return privateKey ;
125- } ;
107+ const getCertificate = ( pem : string ) : string | undefined =>
108+ extractPemBlock ( pem , '-----BEGIN CERTIFICATE-----' , '-----END CERTIFICATE-----' ) ;
126109
127- const getRSAPrivateKey = ( pemFileContent : string ) : string | undefined => {
128- let privateKey ;
129- const beginMark = '-----BEGIN RSA PRIVATE KEY-----' ;
130- const beginIndex = pemFileContent . indexOf ( beginMark ) + beginMark . length ;
131- if ( beginIndex > - 1 ) {
132- const endMark = '-----END RSA PRIVATE KEY-----' ;
133- const endIndex = pemFileContent . indexOf ( endMark , beginIndex ) ;
134- if ( endIndex > - 1 ) {
135- privateKey = pemFileContent . slice ( beginIndex , endIndex ) . replace ( / \s / g, '' ) ;
136- return beginMark + os . EOL + privateKey + os . EOL + endMark ;
137- }
138- }
139- return privateKey ;
140- } ;
110+ const getPrivateKey = ( pem : string ) : string | undefined => extractPemBlock ( pem , '-----BEGIN PRIVATE KEY-----' , '-----END PRIVATE KEY-----' ) ;
141111
142- const getEncryptedPrivateKey = ( pemFileContent : string ) : string | undefined => {
143- let privateKey ;
144- const beginMark = '-----BEGIN ENCRYPTED PRIVATE KEY-----' ;
145- const beginIndex = pemFileContent . indexOf ( beginMark ) + beginMark . length ;
146- if ( beginIndex > - 1 ) {
147- const endMark = '-----END ENCRYPTED PRIVATE KEY-----' ;
148- const endIndex = pemFileContent . indexOf ( endMark , beginIndex ) ;
149- if ( endIndex > - 1 ) {
150- privateKey = pemFileContent . slice ( beginIndex , endIndex ) . replace ( / \s / g, '' ) ;
151- return beginMark + os . EOL + privateKey + os . EOL + endMark ;
152- }
153- }
154- return privateKey ;
155- } ;
112+ const getRSAPrivateKey = ( pem : string ) : string | undefined =>
113+ extractPemBlock ( pem , '-----BEGIN RSA PRIVATE KEY-----' , '-----END RSA PRIVATE KEY-----' ) ;
114+
115+ const getEncryptedPrivateKey = ( pem : string ) : string | undefined =>
116+ extractPemBlock ( pem , '-----BEGIN ENCRYPTED PRIVATE KEY-----' , '-----END ENCRYPTED PRIVATE KEY-----' ) ;
0 commit comments