Open In App

Node.js tlsSocket.getSharedSigalgs() Method

The tlsSocket.getSharedSigalgs() is an inbuilt application programming interface of class TLSSocket within tls module which is used to return the array of signature algorithms shared between the server and the client.

Syntax:



const tlsSocket.getSharedSigalgs()

Parameters: This method does not accept any parameter.

Return Value: This method returns the array of signature algorithms shared between the server and the client.



How to generate Private key and Public certificate?

Example 1: Filename: index.js




// Node.js program to demonstrate the
// tlsSocket.getSharedSigalgs() method
var tls = require('tls'),
    fs = require('fs'),
  
// Port and host address for server    
PORT = 1337,
HOST = '127.0.0.1',
value = null;
   
// Private key and public certificate for access
var options = {
  key: fs.readFileSync('private-key.pem'),
  cert: fs.readFileSync('public-cert.pem'),
  rejectUnauthorized: false
};
  
// Creating and initializing server
var server = tls.createServer(options, function(socket) {
  
  // Print the data that we received
  socket.on('data', function(data) {
    console.log('\nReceived: %s ',
    data.toString().replace(/(\n)/gm, ""));
  });
  
  // Stopping the server
  // by using the close() method
  server.close(() => {
    console.log("Server closed successfully");
  });
});
  
// Start listening on a specific port and address
// by using listen() method
server.listen(PORT, HOST, function() {
  console.log("I'm listening at %s, on port %s", HOST, PORT);
});
  
// Creating and initializing client
var client = tls.connect(PORT, HOST, options, function() {
    
  // Getting array of signature
  // by using tlsSocket.getSharedSigalgs() method
  value = client.getSharedSigalgs();
  
  client.write("List of Signature Algorithm : " + value);
    
  client.end(() => {
    console.log("Client closed successfully");
  });
});

Run the index.js file using the following command:

node index.js

Output:

I'm listening at 127.0.0.1, on port 1337
Client closed successfully

Received: List of Signature Algorithm :
Server closed successfully

Example 2: Filename: index.js




// Node.js program to demonstrate the
// tlsSocket.getSharedSigalgs() method
var tls = require('tls'),
    fs = require('fs'),
  
// Port and host address for server    
PORT = 1337,
HOST = '127.0.0.1';
   
// Private key and public certificate for access
var options = {
  key: fs.readFileSync('private-key.pem'),
  cert: fs.readFileSync('public-cert.pem'),
  rejectUnauthorized: false
};
  
// Creating and initializing server
var server = tls.createServer(options, function(socket) {
  
  // Getting array of signature
  // by using tlsSocket.getSharedSigalgs() method
  value = socket.getSharedSigalgs();
  
  socket.write("Signature Algorithm : " + value[3]);
  
  // Stopping the server
  // by using the close() method
  server.close(() => {
    console.log("Server closed successfully")
  });
});
  
// Start listening on a specific port and address
// by using listen() method
server.listen(PORT, HOST, function() {
  console.log("I'm listening at %s, on port %s", HOST, PORT);
});
  
// Creating and initializing client
var client = tls.connect(PORT, HOST, options, function() {
   console.log("client is connected");
});
  
client.on("data", function(data) {
  
  console.log('Received: %s',
  data.toString().replace(/(\n)/gm, ""));
  
  // Close the connection after receiving the message
  client.end(() => {
     console.log("Client closed successfully")
  });
});

Run the index.js file using the following command:

node index.js

Output:

I'm listening at 127.0.0.1, on port 1337
client is connected
Received: Signature Algorithm : Ed25519+UNDEF
client closed successfully
Server closed successfully

Reference: https://nodejs.org/dist/latest-v12.x/docs/api/tls.html#tls_tlssocket_getsharedsigalgs


Article Tags :