Hi Guest 1 HomePage 1 Jobs 1 WalkIn 1Articles  

 :) Latest Topic
Node.js Tutorial
Parts of Node.js
Features of Node.js
Install Node.js Windows
Install Node.js Linux
Node.js First Example
Node.js Console
Node.js REPL
Node.js NPM
Node.js CL Options1
Node.js CL Option2
Node.js Globals
Node.js OS
Node.js Timer
Node.js Errors
Node.js DNS
Node.js Net
Node.js Crypto
Node.js TLS/SSL
Node.js Debugger
Node.js Process
Node.js Process Example
Node.js Process Example2
Node.js Child Process
Child Process EXEC
Child Process SPAWN
Child Process Fork
Node.js Buffers
Node.js Streams
Node.js Reading Stream
Node.js Writing Stream
Node.js Piping Stream
Node.js Chaining Streams
Node.js File System Part1
Node.js File System Part2
Node.js File System Part3
Node.js Path
Node.js StringDecoder
Node.js Query String
Node.js ZLIB
Node.js Assertion
Node.js V8
Node.js Callbacks
Node.js Events
Node.js Punycode
Node.js TTY
Node.js Web Module
Node.js Upload Files
Node.js Email Send
Node.js BuiltIn Modules
Node.js Assert Code
Node.js Assert Function
Node.js console.time()
 


  

 
Node.js TLS/SSL
Question Posted on 08 Feb 2024

Home >> Tutorial >> Node.js Tutorial >> Node.js TLS/SSL


Node.js TLS SSL

Node.js TLS/SSL




What is TLS/SSL


First questions comes in mind, what TLS and SSL is. TLS mainly stand for Transport Layer Secuirty. And this is the successor to SSL(Secure Sockets Layer). TLS along with SSL is used for cryptographic protocols to secure communication over the web.

Well TLS uses pubic key cryptography to encrypt messages. It encrypts communication generally on the TCP layer.


What is public key cryptography


In public key cryptography, each client and each server has tow keys mainly:-
(1)Public Key
(2)Private Key
Public key is shared with everyone and private key is secured. And to encrypt a message, a computer requires its private key and the recipient?s public key. While on the other hand, to decrypt the message, the recipient requires its own.

And to get access the module you need to use require('tls')

syntax:


var tls=require('tls');





The tls module uses OpenSSL to attain Transport Layer Security and Secure Socket Layer. Well TLS/SSL is a public/private key infrastruture. Each client and each server must have a private key.

And a private key can be created like this:-


openssl genrsa -out ryans-key.pem 1024




All servers and some clients need to have a certificate. Certificates are public keys signed by a Certificate Authority or self-signed. And to get certificate, you have to create a "Certificate Signing Request" CSR file.

And a certificate can be created like below


openssl req -new -key ryans-key.pem -out ryans-csr.pem




And to create a self-signed certificate with the CSR:-


openssl x509 -req -in ryans-csr.pem -signkey ryans-key.pem -out ryans-cert.pem




Node.js TLS client example


File:tlsclient.js


tls = require('tls');
function connected(stream) {
if (stream) {
// socket connected
stream.write("GET / HTTP/1.0\n\rHost: encrypted.google.com:443\n\r\n\r");
} else {
console.log("Connection failed");
}
}
// needed to keep socket variable in scope
var dummy = this;
// try to connect to the server
dummy.socket = tls.connect(443, 'encrypted.google.com', function() {
// callback called only after successful socket connection
dummy.connected = true;
if (dummy.socket.authorized) {
// authorization successful
dummy.socket.setEncoding('utf-8');
connected(dummy.socket);
} else {
// authorization failed
console.log(dummy.socket.authorizationError);
connected(null);
}
});
dummy.socket.addListener('data', function(data) {
// received data
console.log(data);
});
dummy.socket.addListener('error', function(error) {
if (!dummy.connected) {
// socket was not connected, notify callback
connected(null);
}
console.log("FAIL");
console.log(error);
});
dummy.socket.addListener('close', function() {
// do something
});




Now we open th Node.js command prompt and execuet the file


node tlsclient.js







Node.js TLS





Other Important Questions
Parts of Node.js
Install Node.js Linux
Node.js First Example
Node.js Globals
Node.js DNS







 
Top Searches:asp net questions vb net questions sql query uddl questions class javascript Questions sharepoint interview questions and concept silverlight questions and concept wcf questions beans general knowledge ajax questions
PHP | Biztalk | Testing | SAP | HR |