| 
									
										
										
										
											2019-10-15 04:12:46 -06:00
										 |  |  | #!/usr/bin/env node
 | 
					
						
							|  |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var fs = require('fs'); | 
					
						
							|  |  |  | var Eckles = require('../ecdsa'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var infile = process.argv[2]; | 
					
						
							|  |  |  | var format = process.argv[3]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if (!infile) { | 
					
						
							|  |  |  | 	infile = 'jwk'; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if ( | 
					
						
							|  |  |  | 	-1 !== | 
					
						
							|  |  |  | 	['jwk', 'pem', 'json', 'der', 'sec1', 'pkcs8', 'spki', 'ssh'].indexOf( | 
					
						
							|  |  |  | 		infile | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | ) { | 
					
						
							|  |  |  | 	console.log('Generating new key...'); | 
					
						
							|  |  |  | 	Eckles.generate({ | 
					
						
							|  |  |  | 		format: infile, | 
					
						
							|  |  |  | 		namedCurve: format === 'P-384' ? 'P-384' : 'P-256', | 
					
						
							|  |  |  | 		encoding: format === 'der' ? 'der' : 'pem' | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2020-07-28 15:42:32 -06:00
										 |  |  | 		.then(function (key) { | 
					
						
							| 
									
										
										
										
											2019-10-15 04:12:46 -06:00
										 |  |  | 			if ('der' === infile || 'der' === format) { | 
					
						
							|  |  |  | 				key.private = key.private.toString('binary'); | 
					
						
							|  |  |  | 				key.public = key.public.toString('binary'); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			console.log(key.private); | 
					
						
							|  |  |  | 			console.log(key.public); | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2020-07-28 15:42:32 -06:00
										 |  |  | 		.catch(function (err) { | 
					
						
							| 
									
										
										
										
											2019-10-15 04:12:46 -06:00
										 |  |  | 			console.error(err); | 
					
						
							|  |  |  | 			process.exit(1); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	return; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var key = fs.readFileSync(infile, 'ascii'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | try { | 
					
						
							|  |  |  | 	key = JSON.parse(key); | 
					
						
							|  |  |  | } catch (e) { | 
					
						
							|  |  |  | 	// ignore
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var thumbprint = 'thumbprint' === format; | 
					
						
							|  |  |  | if (thumbprint) { | 
					
						
							|  |  |  | 	format = 'public'; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if ('string' === typeof key) { | 
					
						
							|  |  |  | 	if (thumbprint) { | 
					
						
							|  |  |  | 		Eckles.thumbprint({ pem: key }).then(console.log); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	var pub = -1 !== ['public', 'spki', 'pkix'].indexOf(format); | 
					
						
							|  |  |  | 	Eckles.import({ pem: key, public: pub || format }) | 
					
						
							| 
									
										
										
										
											2020-07-28 15:42:32 -06:00
										 |  |  | 		.then(function (jwk) { | 
					
						
							| 
									
										
										
										
											2019-10-15 04:12:46 -06:00
										 |  |  | 			console.log(JSON.stringify(jwk, null, 2)); | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2020-07-28 15:42:32 -06:00
										 |  |  | 		.catch(function (err) { | 
					
						
							| 
									
										
										
										
											2019-10-15 04:12:46 -06:00
										 |  |  | 			console.error(err); | 
					
						
							|  |  |  | 			process.exit(1); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | } else { | 
					
						
							|  |  |  | 	if (thumbprint) { | 
					
						
							|  |  |  | 		Eckles.thumbprint({ jwk: key }).then(console.log); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	Eckles.export({ jwk: key, format: format }) | 
					
						
							| 
									
										
										
										
											2020-07-28 15:42:32 -06:00
										 |  |  | 		.then(function (pem) { | 
					
						
							| 
									
										
										
										
											2019-10-15 04:12:46 -06:00
										 |  |  | 			console.log(pem); | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2020-07-28 15:42:32 -06:00
										 |  |  | 		.catch(function (err) { | 
					
						
							| 
									
										
										
										
											2019-10-15 04:12:46 -06:00
										 |  |  | 			console.error(err); | 
					
						
							|  |  |  | 			process.exit(2); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | } |