You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					79 lines
				
				1.5 KiB
			
		
		
			
		
	
	
					79 lines
				
				1.5 KiB
			| 
								 
											3 years ago
										 
									 | 
							
								'use strict'
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								const crypto = require('crypto')
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								function generateKeyPair () {
							 | 
						||
| 
								 | 
							
								  const options = {
							 | 
						||
| 
								 | 
							
								    modulusLength: 2048,
							 | 
						||
| 
								 | 
							
								    publicExponent: 0x10001,
							 | 
						||
| 
								 | 
							
								    publicKeyEncoding: {
							 | 
						||
| 
								 | 
							
								      type: 'spki',
							 | 
						||
| 
								 | 
							
								      format: 'pem'
							 | 
						||
| 
								 | 
							
								    },
							 | 
						||
| 
								 | 
							
								    privateKeyEncoding: {
							 | 
						||
| 
								 | 
							
								      type: 'pkcs8',
							 | 
						||
| 
								 | 
							
								      format: 'pem'
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								  return crypto.generateKeyPairSync('rsa', options)
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								function generateKeyPairProtected (passphrase) {
							 | 
						||
| 
								 | 
							
								  const options = {
							 | 
						||
| 
								 | 
							
								    modulusLength: 2048,
							 | 
						||
| 
								 | 
							
								    publicExponent: 0x10001,
							 | 
						||
| 
								 | 
							
								    publicKeyEncoding: {
							 | 
						||
| 
								 | 
							
								      type: 'spki',
							 | 
						||
| 
								 | 
							
								      format: 'pem'
							 | 
						||
| 
								 | 
							
								    },
							 | 
						||
| 
								 | 
							
								    privateKeyEncoding: {
							 | 
						||
| 
								 | 
							
								      type: 'pkcs8',
							 | 
						||
| 
								 | 
							
								      format: 'pem',
							 | 
						||
| 
								 | 
							
								      cipher: 'aes-256-cbc',
							 | 
						||
| 
								 | 
							
								      passphrase
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								  return crypto.generateKeyPairSync('rsa', options)
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								function generateKeyPairECDSA () {
							 | 
						||
| 
								 | 
							
								  const options = {
							 | 
						||
| 
								 | 
							
								    modulusLength: 2048,
							 | 
						||
| 
								 | 
							
								    namedCurve: 'prime256v1',
							 | 
						||
| 
								 | 
							
								    publicKeyEncoding: {
							 | 
						||
| 
								 | 
							
								      type: 'spki',
							 | 
						||
| 
								 | 
							
								      format: 'pem'
							 | 
						||
| 
								 | 
							
								    },
							 | 
						||
| 
								 | 
							
								    privateKeyEncoding: {
							 | 
						||
| 
								 | 
							
								      type: 'pkcs8',
							 | 
						||
| 
								 | 
							
								      format: 'pem'
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								  return crypto.generateKeyPairSync('ec', options)
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								function generateKeyPairECDSAProtected (passphrase) {
							 | 
						||
| 
								 | 
							
								  const options = {
							 | 
						||
| 
								 | 
							
								    modulusLength: 2048,
							 | 
						||
| 
								 | 
							
								    namedCurve: 'prime256v1',
							 | 
						||
| 
								 | 
							
								    publicKeyEncoding: {
							 | 
						||
| 
								 | 
							
								      type: 'spki',
							 | 
						||
| 
								 | 
							
								      format: 'pem'
							 | 
						||
| 
								 | 
							
								    },
							 | 
						||
| 
								 | 
							
								    privateKeyEncoding: {
							 | 
						||
| 
								 | 
							
								      type: 'pkcs8',
							 | 
						||
| 
								 | 
							
								      format: 'pem',
							 | 
						||
| 
								 | 
							
								      cipher: 'aes-256-cbc',
							 | 
						||
| 
								 | 
							
								      passphrase
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								  return crypto.generateKeyPairSync('ec', options)
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								module.exports = {
							 | 
						||
| 
								 | 
							
								  generateKeyPair,
							 | 
						||
| 
								 | 
							
								  generateKeyPairProtected,
							 | 
						||
| 
								 | 
							
								  generateKeyPairECDSA,
							 | 
						||
| 
								 | 
							
								  generateKeyPairECDSAProtected
							 | 
						||
| 
								 | 
							
								}
							 |