Skip to content

Commit

Permalink
feat(curve25519.js): Introduce Curve25519 key pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
jwerle committed Jun 19, 2018
1 parent 4313155 commit 5d0e731
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions curve25519.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const alloc = require('buffer-alloc-unsafe')
const kp = require('./key-pair')

const {
crypto_sign_ed25519_pk_to_curve25519,
crypto_sign_ed25519_sk_to_curve25519,
crypto_scalarmult_BYTES,
} = require('sodium-universal')

/**
* Generate a Curve25519 public and secret key pair from an optional
* seed buffer. This function calls crypto_sign_seed_keypair and
* crypto_sign_keypair internally and converts to Curve25519 key pair
* calling crypto_sign_ed25519_pk_to_curve25519 and
* crypto_sign_ed25519_sk_to_curve25519.
* @public
* @param {(Buffer)} [seed]
* @return {Object}
* @throws TypeError
*/
function keyPair(seed) {
const { publicKey, secretKey } = kp.keyPair(seed)
const out = {
publicKey: alloc(crypto_scalarmult_BYTES),
secretKey: alloc(crypto_scalarmult_BYTES),
}

crypto_sign_ed25519_pk_to_curve25519(out.publicKey, publicKey)
crypto_sign_ed25519_sk_to_curve25519(out.secretKey, secretKey)

return out
}

module.exports = {
keyPair
}

0 comments on commit 5d0e731

Please sign in to comment.