home PYTHONJAVA
 

Node.js TLS/SSL


What is TLS/SSL

TLS represents Transport Layer Security. It is the successor to Secure Sockets Layer (SSL). TLS alongside SSL is utilized for cryptographic conventions to verify correspondence over the web.

TLS utilizes public key cryptography to encode messages. It scrambles correspondence for the most part on the TCP layer.

What is public key cryptography

In public key cryptography, every customer and every server has two keys: public key and private key. public key is imparted to everybody and private key is verified. To scramble a message, a PC requires its private key and the recipient?s public key. Then again, to unscramble the message, the beneficiary requires its very own


You need to utilize require('tls') to get to this module.

Syntax:

  1. var tls = require('tls');  

The tls module utilizes OpenSSL to achieve Transport Layer Security and Secure Socket Layer. TLS/SSL is an open/private key foundation. Every customer and every server must have a private key.

Node.js TLS client example

File: tls_client.js

  1. tls = require('tls');  
  2. function connected(stream) {  
  3.     if (stream) {  
  4.        // socket connected  
  5.       stream.write("GET / HTTP/1.0\n\rHost: encrypted.google.com:443\n\r\n\r");    
  6.     } else {  
  7.       console.log("Connection failed");  
  8.     }  
  9. }  
  10.  // needed to keep socket variable in scope  
  11. var dummy = this;  
  12.  // try to connect to the server  
  13. dummy.socket = tls.connect(443'encrypted.google.com', function() {  
  14.    // callback called only after successful socket connection  
  15.    dummy.connected = true;  
  16.    if (dummy.socket.authorized) {  
  17.       // authorization successful  
  18.       dummy.socket.setEncoding('utf-8');  
  19.       connected(dummy.socket);  
  20.    } else {  
  21.       // authorization failed  
  22.      console.log(dummy.socket.authorizationError);  
  23.      connected(null);  
  24.    }  
  25. });  
  26.  dummy.socket.addListener('data', function(data) {  
  27.    // received data  
  28.    console.log(data);  
  29. });  
  30.  dummy.socket.addListener('error', function(error) {  
  31.    if (!dummy.connected) {  
  32.      // socket was not connected, notify callback  
  33.      connected(null);  
  34.    }  
  35.    console.log("FAIL");  
  36.    console.log(error);  
  37. });  
  38.  dummy.socket.addListener('close', function() {  
  39.  // do something  
  40. });  





welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.