| 
									
										
										
										
											2018-11-22 00:39:43 -07:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var PEM = module.exports; | 
					
						
							|  |  |  | var Enc = require('./encoding.js'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PEM.parseBlock = function pemToDer(pem) { | 
					
						
							| 
									
										
										
										
											2018-12-01 16:28:44 -07:00
										 |  |  |   var lines = pem.trim().split(/\n/); | 
					
						
							|  |  |  |   var end = lines.length - 1; | 
					
						
							|  |  |  |   var head = lines[0].match(/-----BEGIN (.*)-----/); | 
					
						
							|  |  |  |   var foot = lines[end].match(/-----END (.*)-----/); | 
					
						
							| 
									
										
										
										
											2018-11-22 00:39:43 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-01 16:28:44 -07:00
										 |  |  |   if (head) { | 
					
						
							|  |  |  |     lines = lines.slice(1, end); | 
					
						
							|  |  |  |     head = head[1]; | 
					
						
							|  |  |  |     if (head !== foot[1]) { | 
					
						
							|  |  |  |       throw new Error("headers and footers do not match"); | 
					
						
							| 
									
										
										
										
											2018-11-22 00:39:43 -07:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-01 16:28:44 -07:00
										 |  |  |   return { type: head, bytes: Enc.base64ToBuf(lines.join('')) }; | 
					
						
							| 
									
										
										
										
											2018-11-22 00:39:43 -07:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2018-11-22 14:31:53 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | PEM.packBlock = function (opts) { | 
					
						
							|  |  |  |   return '-----BEGIN ' + opts.type + '-----\n' | 
					
						
							|  |  |  |     + Enc.bufToBase64(opts.bytes).match(/.{1,64}/g).join('\n') + '\n' | 
					
						
							|  |  |  |     + '-----END ' + opts.type + '-----' | 
					
						
							|  |  |  |   ; | 
					
						
							|  |  |  | }; |