Skip to content

Commit

Permalink
Merge pull request ONDC-Official#64 from poojapaul-ondc/fix/KeyGenera…
Browse files Browse the repository at this point in the history
…tion

fix: signing key generation using nodejs
  • Loading branch information
92shreyansh authored Mar 11, 2024
2 parents 516fe64 + b2e8b33 commit 7fa25f2
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions utilities/signing_and_verification/node/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
const nacl = require("tweetnacl");
const { randomBytes } = require("crypto");
const crypto = require("crypto");

function generateKeyPairs() {
// Generate signing key pair
const signingKeyPair = nacl.sign.keyPair();

// Generate X25519 key pair for encryption
const encryptionKeyPair = nacl.box.keyPair.fromSecretKey(
randomBytes(nacl.box.secretKeyLength)
);
const { privateKey, publicKey } = crypto.generateKeyPairSync('x25519', {
modulusLength: 2048,
publicKeyEncoding: {
type: 'spki',
format: 'pem',
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
},
});

return {
Signing_private_key: Buffer.from(signingKeyPair.secretKey).toString(
Expand All @@ -17,15 +22,16 @@ function generateKeyPairs() {
Signing_public_key: Buffer.from(signingKeyPair.publicKey).toString(
"base64"
),
Encryption_Privatekey: Buffer.from(encryptionKeyPair.secretKey).toString(
"base64"
),
Encryption_Publickey: Buffer.from(encryptionKeyPair.publicKey).toString(
"base64"
),
Encryption_Privatekey: privateKey.toString('utf-8')
.replace(/-----BEGIN PRIVATE KEY-----/, '')
.replace(/-----END PRIVATE KEY-----/, '')
.replace(/\s/g, ''),
Encryption_Publickey: publicKey.toString('utf-8')
.replace(/-----BEGIN PUBLIC KEY-----/, '')
.replace(/-----END PUBLIC KEY-----/, '')
.replace(/\s/g, ''),
};
}

// Example usage:
const keyPairs = generateKeyPairs();
console.log(keyPairs);

0 comments on commit 7fa25f2

Please sign in to comment.