-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(curve25519.js): Introduce Curve25519 key pairs
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |