| 
									
										
										
										
											2018-12-16 02:54:57 -07:00
										 |  |  | // Copyright 2016-2018 AJ ONeal. All rights reserved
 | 
					
						
							|  |  |  | /* This Source Code Form is subject to the terms of the Mozilla Public | 
					
						
							|  |  |  |  * License, v. 2.0. If a copy of the MPL was not distributed with this | 
					
						
							|  |  |  |  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 | 
					
						
							| 
									
										
										
										
											2018-12-15 22:31:17 -07:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-16 02:54:57 -07:00
										 |  |  | var Rasha = require('./rasha'); | 
					
						
							| 
									
										
										
										
											2018-12-15 22:31:17 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | module.exports = function (bitlen, exp) { | 
					
						
							|  |  |  |   var k = require('node-forge').pki.rsa | 
					
						
							|  |  |  |     .generateKeyPair({ bits: bitlen || 2048, e: exp || 0x10001 }).privateKey; | 
					
						
							| 
									
										
										
										
											2018-12-15 23:16:27 -07:00
										 |  |  |   var jwk = { | 
					
						
							|  |  |  |     kty: "RSA" | 
					
						
							|  |  |  |   , n: _toUrlBase64(k.n) | 
					
						
							|  |  |  |   , e: _toUrlBase64(k.e) | 
					
						
							|  |  |  |   , d: _toUrlBase64(k.d) | 
					
						
							|  |  |  |   , p: _toUrlBase64(k.p) | 
					
						
							|  |  |  |   , q: _toUrlBase64(k.q) | 
					
						
							|  |  |  |   , dp: _toUrlBase64(k.dP) | 
					
						
							|  |  |  |   , dq: _toUrlBase64(k.dQ) | 
					
						
							|  |  |  |   , qi: _toUrlBase64(k.qInv) | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     publicKeyPem: Rasha.exportSync({ jwk: jwk, public: true }) | 
					
						
							|  |  |  |   , privateKeyPem: Rasha.exportSync({ jwk: jwk }) | 
					
						
							|  |  |  |   , privateKeyJwk: jwk | 
					
						
							|  |  |  |   , publicKeyJwk: { | 
					
						
							|  |  |  |       kty: jwk.kty | 
					
						
							|  |  |  |     , n: jwk.n | 
					
						
							|  |  |  |     , e: jwk.e | 
					
						
							| 
									
										
										
										
											2018-12-15 22:31:17 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-12-15 23:16:27 -07:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2018-12-15 22:31:17 -07:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function _toUrlBase64(fbn) { | 
					
						
							|  |  |  |   var hex = fbn.toRadix(16); | 
					
						
							|  |  |  |   if (hex.length % 2) { | 
					
						
							|  |  |  |     // Invalid hex string
 | 
					
						
							|  |  |  |     hex = '0' + hex; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   while ('00' === hex.slice(0, 2)) { | 
					
						
							|  |  |  |     hex = hex.slice(2); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return Buffer.from(hex, 'hex').toString('base64') | 
					
						
							|  |  |  |     .replace(/\+/g, "-") | 
					
						
							|  |  |  |     .replace(/\//g, "_") | 
					
						
							|  |  |  |     .replace(/=/g,"") | 
					
						
							|  |  |  |   ; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if (require.main === module) { | 
					
						
							| 
									
										
										
										
											2018-12-15 23:16:27 -07:00
										 |  |  |   var keypair = module.exports(2048, 0x10001); | 
					
						
							|  |  |  |   console.info(keypair.privateKeyPem); | 
					
						
							|  |  |  |   console.warn(keypair.publicKeyPem); | 
					
						
							|  |  |  |   //console.info(keypair.privateKeyJwk);
 | 
					
						
							|  |  |  |   //console.warn(keypair.publicKeyJwk);
 | 
					
						
							| 
									
										
										
										
											2018-12-15 22:31:17 -07:00
										 |  |  | } |