📅  最后修改于: 2023-12-03 14:44:41.305000             🧑  作者: Mango
In Node.js, the x509.fingerprint256
property is used to retrieve the SHA-256 fingerprint of an X.509 certificate. This property is available in the built-in https
and tls
modules, as well as in third-party modules that use these modules.
const fingerprint = socket.getPeerCertificate().fingerprint256;
The getPeerCertificate()
method is used to retrieve the X.509 certificate of the remote peer connected over a TLS socket. The fingerprint256
property of the certificate object is used to retrieve the SHA-256 fingerprint of the certificate.
The SHA-256 fingerprint is a unique identifier that is calculated by hashing the entire certificate using the SHA-256 algorithm. This makes it easier to verify the authenticity of the certificate compared to other methods such as comparing the certificate's issuer and subject information.
The following example demonstrates how to retrieve the SHA-256 fingerprint of a remote peer's X.509 certificate using the x509.fingerprint256
property of the tls
module:
const tls = require('tls');
const options = {
host: 'www.google.com',
port: 443,
};
const socket = tls.connect(options, () => {
const fingerprint = socket.getPeerCertificate().fingerprint256;
console.log(`SHA-256 fingerprint of the certificate: ${fingerprint}`);
});
In this example, a TLS socket is established with the remote host www.google.com
on port 443
. After the socket is connected, the SHA-256 fingerprint of the remote peer's X.509 certificate is retrieved using the fingerprint256
property of the certificate object. Finally, the fingerprint is printed to the console.