Git Product home page Git Product logo

keypair's Introduction

keypair

Generate a RSA PEM key pair from pure JS

Build Status downloads

browser support

Usage

var keypair = require('keypair');

var pair = keypair();
console.log(pair);

outputs

$ node example.js
{ public: '-----BEGIN RSA PUBLIC KEY-----\r\nMIGJAoGBAM3CosR73CBNcJsLv5E90NsFt6qN1uziQ484gbOoule8leXHFbyIzPQRozgEpSpi\r\nwhr6d2/c0CfZHEJ3m5tV0klxfjfM7oqjRMURnH/rmBjcETQ7qzIISZQ/iptJ3p7Gi78X5ZMh\r\nLNtDkUFU9WaGdiEb+SnC39wjErmJSfmGb7i1AgMBAAE=\r\n-----END RSA PUBLIC KEY-----\n',
  private: '-----BEGIN RSA PRIVATE KEY-----\r\nMIICXAIBAAKBgQDNwqLEe9wgTXCbC7+RPdDbBbeqjdbs4kOPOIGzqLpXvJXlxxW8iMz0EaM4\r\nBKUqYsIa+ndv3NAn2RxCd5ubVdJJcX43zO6Ko0TFEZx/65gY3BE0O6syCEmUP4qbSd6exou/\r\nF+WTISzbQ5FBVPVmhnYhG/kpwt/cIxK5iUn5hm+4tQIDAQABAoGBAI+8xiPoOrA+KMnG/T4j\r\nJsG6TsHQcDHvJi7o1IKC/hnIXha0atTX5AUkRRce95qSfvKFweXdJXSQ0JMGJyfuXgU6dI0T\r\ncseFRfewXAa/ssxAC+iUVR6KUMh1PE2wXLitfeI6JLvVtrBYswm2I7CtY0q8n5AGimHWVXJP\r\nLfGV7m0BAkEA+fqFt2LXbLtyg6wZyxMA/cnmt5Nt3U2dAu77MzFJvibANUNHE4HPLZxjGNXN\r\n+a6m0K6TD4kDdh5HfUYLWWRBYQJBANK3carmulBwqzcDBjsJ0YrIONBpCAsXxk8idXb8jL9a\r\nNIg15Wumm2enqqObahDHB5jnGOLmbasizvSVqypfM9UCQCQl8xIqy+YgURXzXCN+kwUgHinr\r\nutZms87Jyi+D8Br8NY0+Nlf+zHvXAomD2W5CsEK7C+8SLBr3k/TsnRWHJuECQHFE9RA2OP8W\r\noaLPuGCyFXaxzICThSRZYluVnWkZtxsBhW2W8z1b8PvWUE7kMy7TnkzeJS2LSnaNHoyxi7Ia\r\nPQUCQCwWU4U+v4lD7uYBw00Ga/xt+7+UqFPlPVdz1yyr4q24Zxaw0LgmuEvgU5dycq8N7Jxj\r\nTubX0MIRR+G9fmDBBl8=\r\n-----END RSA PRIVATE KEY-----\n' }

Performance

Performance greatly depends on the bit size of the generated private key. With 1024 bits you get a key in 0.5s-2s, with 2048 bits it takes 8s-20s, on the same machine. As this will block the event loop while generating the key, make sure that's ok or to spawn a child process or run it inside a webworker.

Pro Tip: authorized_keys

@maxogden found out how to use this module to create entries for the authorized_keys file:

var keypair = require('keypair');
var forge = require('node-forge');

var pair = keypair();
var publicKey = forge.pki.publicKeyFromPem(pair.public);
var ssh = forge.ssh.publicKeyToOpenSSH(publicKey, '[email protected]');
console.log(ssh);

API

keypair([opts])

Get an RSA PEM key pair.

opts can be

  • bits: the size for the private key in bits. Default: 2048.
  • e: the public exponent to use. Default: 65537.

Installation

With npm do

$ npm install keypair

Kudos

To digitalbazaar for their forge project, this library is merely a wrapper around some of forge's functions.

License

BSD / GPL

keypair's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

keypair's Issues

RNG by WebAssembly?

I like to leave it as an idea here, just to think about it or to research: how about porting a good and performant RNG as a WebAssembly library which provides API for JavaScript instead of using JavaScript's Math.random(), and using the latter as a fallback with a fair warning for developers?

Rust wraps providers of operating system's RNGs and it also discloses that some browsers support Crypto.getRandomValues() as a better RNG for cryptographic operations.

ERROR TypeError: crypto.randomBytes is not a function

问题:

keypire使用报错

测试步骤:

import keypair from 'keypair';
const pair = keypair();

现状:

core.mjs:11754 ERROR TypeError: crypto.randomBytes is not a function
at ctx.seedFileSync (index.js:1032:21)
at _reseedSync (index.js:921:21)
at ctx.generateSync (index.js:874:9)
at ctx.generate (index.js:801:18)
at forge.random.getBytes (index.js:1831:23)
at Object.nextBytes (index.js:1858:28)
at BigInteger.bnpFromNumber [as fromNumber] (index.js:2552:4)
at new BigInteger (index.js:1898:35)
at pki.rsa.stepKeyPairGenerationState (index.js:3972:21)
at pki.rsa.generateKeyPair (index.js:4322:13)

image

版本信息:

v1.0.4

期望:

成功

Existence of '\n' characters within the key values

The '\n' printing in the key values makes the key values useless. When doing a POST method with these keys as input, the string treats '\n' as characters rather than new line. Hence when calculating the fingerprints it always give an error.

Kindly look into this issue.

Thanks

A huge thanks for this awesome script, it saved me weeks of senseless solution finding
:)

How to use it client-side?

I'd like to use keypair client-side. I tried creating index.html with:

<script type="text/javascript" src="keypair.js"></script>
<script type="text/javascript">
var pair = keypair();
console.log(pair);
</script>

but it doesn't work:

Uncaught ReferenceError: module is not defined
Uncaught ReferenceError: keypair is not defined

How to make it available from browser?

Can't use BigInteger outside of keypair

var keypair=require('keypair');
console.log(new keypair.BigInteger(16));

/home/engine/private_js/secure.js:1
(function (exports, require, module, __filename, __dirname) { /*
^
TypeError: const 'a' has already been declared
at Object. (/home/engine/private_js/secure.js:1:11)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
[root@server node_modules]#

I have also tried installing the npm module https://www.npmjs.org/package/jsbn

I get the same error when trying to get jsbn.BigInteger because of keypair.

How can I use BigInteger outside of your module?

license?

package.json says MIT, README says BSD, LICENSE file says BSD or GPL.

How can I generate SSH2 public key?

Can I use the generate keys for ssh2? I am guessing.. I can use the private key just fine, but I need to convert the public key into ssh2 (with ---- BEGIN SSH2 PUBLIC KEY ----) format so that I can put them under ~/.ssh/authorized_keys before I can connect using ssh2 npm module?

only one set of private/public keys generated

Hello,
I am using the keypair add-on on my server as follows: let pair = keypair();
Each time the server routine is called, it returns the same character sets for the public key. I see that the private key varies (as desired) but the public key is always the same. Starts with MIIB and ends DAQAB
Am I doing something wrong?

Hardening

Type checking

See:

keypair/index.js

Line 1020 in 9596418

if(crypto) {

Please change from:

if(crypto) {

to

if(null !== crypto) { # Yoda-Condition not needed

For such critical lib there are too less type checkings in this lib, normally such critical libs should only work in strict mode! :-)

Fallbacks

Currently you're using too many fallbacks to prevent errors. Each fallback/weakening) should be optional. Examples in your code:

  1. crypto-lib is optional
  2. weak entropy (see
    /* Mozilla claims getRandomValues can throw QuotaExceededError, so
    ).

Recommendation:
In future versions such fallbacks can be added, but just as option. E.g. if crypto is not available you could output an error with a security warning, how to disable required usage of crypto. Same with weaker entropy...

slow

not sure if there is anything that can be done about this but generating keys is damn slow:

var keypair = require('keypair');
console.time();
keypair();
console.timeEnd();

Output of multiple runs on Macbook, 3GHz Intel Core i7:

22324ms
6746ms
1522ms
10037ms

It seems to be more slow on the first run, I guess that's v8 getting it's optimisation on?

If this isn't isolated to me, it might be worthwhile mentioning performance in the readme, and recommend this be run in a child process or something to keep the blocking out of the main thread.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.