node-gpg icon indicating copy to clipboard operation
node-gpg copied to clipboard

How can I generate key pair using this?

Open rohitkhatri opened this issue 7 years ago • 1 comments

I'm trying to find documentation to create key pair, but there's no information regarding that.

I would like to generate key pair and store that in some database and use them later to encrypt, decrypt files.

Please guide me.

rohitkhatri avatar Sep 26 '18 05:09 rohitkhatri

Looking at the lib/gpg.js source there are no methods currently dedicated to generating new key pairs, however, the Usage portion of the ReadMe.md file states that the call method may be used for calling the command line utility that this project wraps...

  // ... Other stuff

  /**
   * Raw call to gpg.
   *
   * @param  {String}   stdin  String to send to stdin.
   * @param  {Array}    [args] Array of arguments.
   * @param  {Function} [fn]   Callback.
   * @api public
   */
  call: function(stdin, args, fn) {
    spawnGPG(stdin, args, fn);
  },

... checking with GitHub's GPG docs states that either --full-generate-key or --default-new-key-algo rsa4096 --gen-key, however, this would be ill-advised if you're after non-interactive key pair generation, which if that's your aim then glancing at one of my older projects source files (S0AndS0/Perinoid_Pipes - Script_Helpers/GnuPG_Gen_Key.sh), shows that the following command line options may be of use...

#!/usr/bin/env bash

## ... other stuff

Func_gen_gnupg_keys(){
	_pass_phrase=( "$@" )
	if [ "${#Var_gnupg_comment}" != "0" ]; then
		gpg --batch --gen-key <<EOF
Key-Type: ${Var_gnupg_key_type}
Key-Length: ${Var_gnupg_key_length}
Subkey-Type: ${Var_gnupg_sub_key_type}
Subkey-Length: ${Var_gnupg_sub_key_length}
Name-Real: ${Var_gnupg_name}
Name-Comment: ${Var_gnupg_comment}
name-Email: ${Var_gnupg_email}
Expire-Date: ${Var_gnupg_expire_date}
Passphrase: ${_pass_phrase[*]}
## Uncomment the next line to not generate keys
#%dry-run
%commit
%echo finished
EOF
	else
		gpg --batch --gen-key <<EOF
Key-Type: ${Var_gnupg_key_type}
Key-Length: ${Var_gnupg_key_length}
Subkey-Type: ${Var_gnupg_sub_key_type}
Subkey-Length: ${Var_gnupg_sub_key_length}
Name-Real: ${Var_gnupg_name}
name-Email: ${Var_gnupg_email}
Expire-Date: ${Var_gnupg_expire_date}
Passphrase: ${_pass_phrase[*]}
%commit
%echo finished
EOF
	fi

	unset _pass_phrase
}

## ... more _pruning_ of non-essential code for this answer

Note, I do not know if this project can handle EOF (End Of File) redirection of arguments and it's likely not to work so well on non-Unix inspired OSs, but if memory serves from the last time I did a info --node='Unattended Usage of GPG' gpg, some of the above where required at the time.

Doing a search for call( within this project shows test/index.js demonstrates the use of the call method...

// ... Other tests trimmed

    it('should decrypt files', function(done){
      gpg.call('', [ '--skip-verify', '--passphrase-fd', '0', '--decrypt', './test/hello.gpg' ], function(err, decrypted){
        assert.ifError(err);
        assert.ok(decrypted.length);
        assert.equal(decrypted.toString('utf8'), 'Hello World\n');
        done();
      });
    });

// ... and a bit more pruning to keep things relatively concise

Putting some of this together one might be able to...

function keyGen({key_type, length, sub_type, name, email, pass, expires}) {
  let gpg_configs = '--batch --gen-key';
  // ... Do stuff that checks and defaults things like `type`
  // if (key_type) {
  //   gpg_configs += `\nKey-Type: ${key_type}`
  // } else {
  //   gpg_configs += `\nKey-Type: DSA`
  // }
  // ... Then _feed_ the `call` method
  GPG.call(gpg_configs, [], (err, results) => {
    if (err) throw err;
    // ... do stuff with results?
    console.log(results);
  });
}

... though this last bit is untested an incomplete, hopefully as a whole this has prepared ya to code something better.

S0AndS0 avatar Jul 18 '19 19:07 S0AndS0