Node.js new X509Certificate() Function

06 Apr, 2021
The new X509Certificate(buffer) is  an inbuilt constructor of class X509Certificate within crypto module which is used to provide a PEM encoded X509 Certificate.
Syntax:
new X509Certificate(buffer)
Parameters: This function takes the buffer of strings representing public certificate.
Return Value: This function returns the object of a PEM encoded X509 Certificate.
How to generate a Public certificate?
Public certificate: Open notepad and copy-paste the following key and save the file as public-cert.pem
-----BEGIN CERTIFICATE-----
MIICfzCCAegCCQDxxeXw914Y2DANBgkqhkiG9w0BAQsFADCBgzELMAkGA1UEBhMC
SU4xEzARBgNVBAgMCldlc3RiZW5nYWwxEDAOBgNVBAcMB0tvbGthdGExFDASBgNV
BAoMC1BhbmNvLCBJbmMuMRUwEwYDVQQDDAxSb2hpdCBQcmFzYWQxIDAeBgkqhkiG
9w0BCQEWEXJvZm9mb2ZAZ21haWwuY29tMB4XDTIwMDkwOTA1NTExN1oXDTIwMTAw
OTA1NTExN1owgYMxCzAJBgNVBAYTAklOMRMwEQYDVQQIDApXZXN0YmVuZ2FsMRAw
DgYDVQQHDAdLb2xrYXRhMRQwEgYDVQQKDAtQYW5jbywgSW5jLjEVMBMGA1UEAwwM
Um9oaXQgUHJhc2FkMSAwHgYJKoZIhvcNAQkBFhFyb2ZvZm9mQGdtYWlsLmNvbTCB
nzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAt/EfcF3FG4TneOBWr4JhOUdyuCXm
Dhy5yO3VKtQfPxr+5d0joCSnn/5vYDNSr1MfedZmqVxrXFoMAdPCd71BNmDmeLVi
QK61WREtASP0ZhQMoUBT+R3Fpdy0jPS0YoT/fBd96CJCmgsQOS8Tq5IKVeB61MyC
kwAQ2Goe0T3sdVkCAwEAATANBgkqhkiG9w0BAQsFAAOBgQATe6ixdAjoV7BSHgRX
bXM2+IZLq8kq3s7ck0EZrRVhsivutcaZwDXRCCinB+OlPedbzXwNZGvVX0nwPYHG
BfiXwdiuZeVJ88ni6Fm6RhoPtu2QF1UExfBvSXuMBgR+evp+e3QadNpGx6Ppl1aC
hWF6W2H9+MAlU7yvtmCQQuZmfQ==
-----END CERTIFICATE-----
Example 1:
index.js
// Node.js program to demonstrate the  // new X509Certificate(buffer) constructor function  // Importing crypto moduleconst {X509Certificate} = require('crypto')  // Importing fs moduleconst fs = require('fs')  // getting object of a PEM encoded X509 Certificate. const x509 = new X509Certificate(fs.readFileSync('public-cert.pem'));  // getting subject included in this certificate.// by using x509.subject functionconst value = x509.subject  // display the resultconsole.log("subject :- " + value)
Run the index.js file using the following command:
node index.js
Output:
subject :- C=IN
ST=Westbengal
L=Kolkata
O=Panco, Inc.
CN=Rohit Prasad
emailAddress=rofofof@gmail.com
Example 2:
index.js
// Node.js program to demonstrate the  // new X509Certificate(buffer) constructor function  // Importing crypto moduleconst {X509Certificate} = require('crypto')  // Importing fs moduleconst fs = require('fs')  // display the resultconsole.log((new X509Certificate(             fs.readFileSync('public-cert.pem'))).subject)
Run the index.js file using the following command:
node index.js
Output:
C=IN
ST=Westbengal
L=Kolkata
O=Panco, Inc.
CN=Rohit Prasad
emailAddress=rofofof@gmail.com
Reference: https://nodejs.org/dist/latest-v15.x/docs/api/crypto.html#crypto_new_x509certificate_buffer
Node.js-crypto-module
NodeJS-function
Picked
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Installation of Node.js on Windows
JWT Authentication with Node.js
Difference between dependencies, devDependencies and peerDependencies
Mongoose Populate() Method
Mongoose find() Function
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?
Differences between Functional Components and Class Components in React