Open In App

Node.js x509.raw Property

Improve
Improve
Like Article
Like
Save
Share
Report

The x509.raw is an inbuilt application programming interface of class X509Certificate within the crypto module which is used to get a Buffer containing the DER encoding of this certificate.

Syntax:

const x509.raw

Parameters: This property does not accept any arguments as a parameter.

Return Value: This property returns a Buffer containing the DER encoding of this 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 :

index.js




// Node.js program to demonstrate the  
// x509.raw APi
  
// Importing crypto module
const {X509Certificate} = require('crypto')
  
// Importing fs module
const fs = require('fs')
  
// Getting object of a PEM encoded X509 Certificate. 
const x509 = new X509Certificate(fs.readFileSync('public-cert.pem'));
  
// Getting raw included in this certificate.
// by using x509.raw api
const value = x509.raw
  
// Display the result
console.log(value)


Run the index.js file using the following command:

node index.js

Output:

<Buffer 30 82 02 7f 30 82 01 e8 02 09 00 f1 c5 e5
f0 f7 5e 18 d8 30 0d 06 09 2a 86 48 86 f7 0d 01 01 0b 05 
00 30 81 83 31 0b 30 09 06 03 55 04 06 13 02 49 4e ...
 593 more bytes>

Example 2:

index.js




// Node.js program to demonstrate the  
// x509.raw APi
  
// Importing crypto module
const {X509Certificate} = require('crypto')
  
// Importing fs module
const fs = require('fs')
  
// Display the result
console.log((new X509Certificate(
  fs.readFileSync('public-cert.pem'))).raw)


Run the index.js file using the following command:

node index.js

Output:

<Buffer 30 82 02 7f 30 82 01 e8 02 09 00 f1 c5
e5 f0 f7 5e 18 d8 30 0d 06 09 2a 86 48 86 f7 0d 01 01 
0b 05 00 30 81 83 31 0b 30 09 06 03 55 04 06 13 02 49 4e
 ... 593 more bytes>

Reference: https://nodejs.org/dist/latest-v15.x/docs/api/crypto.html#crypto_x509_raw



Last Updated : 06 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads