| 
									
										
										
										
											2016-07-30 16:00:08 -04:00
										 |  |  | /*! | 
					
						
							|  |  |  |  * rsa-compat | 
					
						
							|  |  |  |  * Copyright(c) 2016 AJ ONeal <aj@daplie.com> https://daplie.com
 | 
					
						
							|  |  |  |  * Apache-2.0 OR MIT (and hence also MPL 2.0) | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 'use strict'; | 
					
						
							|  |  |  | var RSA = {}; | 
					
						
							|  |  |  | var NOBJ = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function create(deps) { | 
					
						
							|  |  |  |   var crypto = require('crypto'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   deps = deps || {}; | 
					
						
							|  |  |  |   deps.NOBJ = {}; | 
					
						
							|  |  |  |   deps.RSA = RSA; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   RSA.utils = require('./lib/key-utils.js'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   RSA.utils._bytesToBuffer = function (bytes) { | 
					
						
							|  |  |  |     var forge = require("node-forge"); | 
					
						
							|  |  |  |     return new Buffer(forge.util.bytesToHex(bytes), "hex"); | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2016-07-30 19:09:37 -04:00
										 |  |  |   RSA._internal = require('./lib/node');//.create(deps);
 | 
					
						
							| 
									
										
										
										
											2016-07-30 16:00:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   RSA.thumbprint = function (jwk) { | 
					
						
							|  |  |  |     jwk = jwk.privateKeyJwk || jwk.publicKeyJwk || jwk; | 
					
						
							|  |  |  |     if (!jwk.e || !jwk.n) { | 
					
						
							|  |  |  |       throw new Error("You must provide an RSA jwk with 'e' and 'n' (the public components)"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     var input = RSA.utils._bytesToBuffer('{"e":"'+ jwk.e + '","kty":"RSA","n":"'+ jwk.n +'"}'); | 
					
						
							|  |  |  |     return RSA.util.b64enc(crypto.createHash('sha256').update(input).digest()); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   RSA.generateKeypair = function (length, exponent, options, cb) { | 
					
						
							|  |  |  |     var keypair = { | 
					
						
							|  |  |  |       privateKeyPem: undefined | 
					
						
							|  |  |  |     , publicKeyPem: undefined | 
					
						
							|  |  |  |     , privateKeyJwk: undefined | 
					
						
							|  |  |  |     , publicKeyJwk: undefined | 
					
						
							|  |  |  |     , _ursa: undefined | 
					
						
							|  |  |  |     , _forge: undefined | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     options = options || NOBJ; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-30 23:47:52 -04:00
										 |  |  |     RSA._internal.generateKeypair(length, exponent, options, function (err, keys) { | 
					
						
							| 
									
										
										
										
											2016-07-30 16:00:08 -04:00
										 |  |  |       if (false !== options.jwk || options.thumbprint) { | 
					
						
							|  |  |  |         keypair.privateKeyJwk = RSA._internal.exportPrivateJwk(keys); | 
					
						
							|  |  |  |         if (options.public) { | 
					
						
							|  |  |  |           keypair.publicKeyJwk = RSA._internal.exportPublicJwk(keys); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (options.pem) { | 
					
						
							|  |  |  |         keypair.privateKeyPem = RSA._internal.exportPrivatePem(keys); | 
					
						
							|  |  |  |         if (options.public) { | 
					
						
							|  |  |  |           keypair.publicKeyPem = RSA._internal.exportPublicPem(keys); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (options.thumprint) { | 
					
						
							|  |  |  |         keypair.thumbprint = RSA.thumbprint(keypair.privateKeyJwk /*|| keypair.publicKeyJwk*/); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (options.internal) { | 
					
						
							|  |  |  |         //keypair._ursa = undefined;
 | 
					
						
							|  |  |  |         //keypair._forge = undefined;
 | 
					
						
							|  |  |  |         keypair._ursa = keys._ursa; | 
					
						
							|  |  |  |         keypair._forge = keys._forge; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       cb(null, keypair); | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-30 23:47:52 -04:00
										 |  |  |   RSA.exportPrivateKey = RSA._internal.exportPrivatePem; | 
					
						
							|  |  |  |   RSA.exportPublicKey = RSA._internal.exportPublicPem; | 
					
						
							| 
									
										
										
										
											2016-07-31 00:48:54 -04:00
										 |  |  |   RSA.exportPrivatePem = RSA._internal.exportPrivatePem; | 
					
						
							|  |  |  |   RSA.exportPublicPem = RSA._internal.exportPublicPem; | 
					
						
							| 
									
										
										
										
											2016-07-30 23:47:52 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   RSA.exportPrivateJwk = RSA._internal.exportPrivateJwk; | 
					
						
							|  |  |  |   RSA.exportPublicJwk = RSA._internal.exportPublicJwk; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-30 16:00:08 -04:00
										 |  |  |   return RSA; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports.RSA = create(/*require('./lib/node')*/); | 
					
						
							|  |  |  | //module.exports.RSA.create = create;
 |