📜  Node.js x509.raw 属性

📅  最后修改于: 2022-05-13 01:56:22.195000             🧑  作者: Mango

Node.js x509.raw 属性

x509.raw是加密模块中 X509Certificate 类的内置应用程序编程接口,用于获取包含此证书的 DER 编码的缓冲区。

句法:

const x509.raw

参数:此属性不接受任何参数作为参数。

返回值:此属性返回包含此证书的 DER 编码的 Buffer。

如何生成公共证书?

公共证书:打开记事本并复制粘贴以下密钥并将文件另存为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-----

例子 :

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)


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)


使用以下命令运行index.js文件:

node index.js

输出:

示例 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)

使用以下命令运行index.js文件:

node index.js

输出:

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