@@ -51,6 +51,18 @@ void ManagedX509::MemoryInfo(MemoryTracker* tracker) const {
51
51
tracker->TrackFieldWithSize (" cert" , size);
52
52
}
53
53
54
+ namespace {
55
+ template <const EVP_MD* (*algo)()>
56
+ void Fingerprint (const FunctionCallbackInfo<Value>& args) {
57
+ Environment* env = Environment::GetCurrent (args);
58
+ X509Certificate* cert;
59
+ ASSIGN_OR_RETURN_UNWRAP (&cert, args.Holder ());
60
+ Local<Value> ret;
61
+ if (GetFingerprintDigest (env, algo (), cert->get ()).ToLocal (&ret))
62
+ args.GetReturnValue ().Set (ret);
63
+ }
64
+ } // namespace
65
+
54
66
Local<FunctionTemplate> X509Certificate::GetConstructorTemplate (
55
67
Environment* env) {
56
68
Local<FunctionTemplate> tmpl = env->x509_constructor_template ();
@@ -68,9 +80,9 @@ Local<FunctionTemplate> X509Certificate::GetConstructorTemplate(
68
80
SetProtoMethod (isolate, tmpl, " issuer" , Issuer);
69
81
SetProtoMethod (isolate, tmpl, " validTo" , ValidTo);
70
82
SetProtoMethod (isolate, tmpl, " validFrom" , ValidFrom);
71
- SetProtoMethod (isolate, tmpl, " fingerprint" , Fingerprint);
72
- SetProtoMethod (isolate, tmpl, " fingerprint256" , Fingerprint256 );
73
- SetProtoMethod (isolate, tmpl, " fingerprint512" , Fingerprint512 );
83
+ SetProtoMethod (isolate, tmpl, " fingerprint" , Fingerprint<EVP_sha1> );
84
+ SetProtoMethod (isolate, tmpl, " fingerprint256" , Fingerprint<EVP_sha256> );
85
+ SetProtoMethod (isolate, tmpl, " fingerprint512" , Fingerprint<EVP_sha512> );
74
86
SetProtoMethod (isolate, tmpl, " keyUsage" , KeyUsage);
75
87
SetProtoMethod (isolate, tmpl, " serialNumber" , SerialNumber);
76
88
SetProtoMethod (isolate, tmpl, " pem" , Pem);
@@ -258,33 +270,6 @@ void X509Certificate::ValidTo(const FunctionCallbackInfo<Value>& args) {
258
270
args.GetReturnValue ().Set (ret);
259
271
}
260
272
261
- void X509Certificate::Fingerprint (const FunctionCallbackInfo<Value>& args) {
262
- Environment* env = Environment::GetCurrent (args);
263
- X509Certificate* cert;
264
- ASSIGN_OR_RETURN_UNWRAP (&cert, args.Holder ());
265
- Local<Value> ret;
266
- if (GetFingerprintDigest (env, EVP_sha1 (), cert->get ()).ToLocal (&ret))
267
- args.GetReturnValue ().Set (ret);
268
- }
269
-
270
- void X509Certificate::Fingerprint256 (const FunctionCallbackInfo<Value>& args) {
271
- Environment* env = Environment::GetCurrent (args);
272
- X509Certificate* cert;
273
- ASSIGN_OR_RETURN_UNWRAP (&cert, args.Holder ());
274
- Local<Value> ret;
275
- if (GetFingerprintDigest (env, EVP_sha256 (), cert->get ()).ToLocal (&ret))
276
- args.GetReturnValue ().Set (ret);
277
- }
278
-
279
- void X509Certificate::Fingerprint512 (const FunctionCallbackInfo<Value>& args) {
280
- Environment* env = Environment::GetCurrent (args);
281
- X509Certificate* cert;
282
- ASSIGN_OR_RETURN_UNWRAP (&cert, args.Holder ());
283
- Local<Value> ret;
284
- if (GetFingerprintDigest (env, EVP_sha512 (), cert->get ()).ToLocal (&ret))
285
- args.GetReturnValue ().Set (ret);
286
- }
287
-
288
273
void X509Certificate::KeyUsage (const FunctionCallbackInfo<Value>& args) {
289
274
Environment* env = Environment::GetCurrent (args);
290
275
X509Certificate* cert;
@@ -575,9 +560,9 @@ void X509Certificate::RegisterExternalReferences(
575
560
registry->Register (Issuer);
576
561
registry->Register (ValidTo);
577
562
registry->Register (ValidFrom);
578
- registry->Register (Fingerprint);
579
- registry->Register (Fingerprint256 );
580
- registry->Register (Fingerprint512 );
563
+ registry->Register (Fingerprint<EVP_sha1> );
564
+ registry->Register (Fingerprint<EVP_sha256> );
565
+ registry->Register (Fingerprint<EVP_sha512> );
581
566
registry->Register (KeyUsage);
582
567
registry->Register (SerialNumber);
583
568
registry->Register (Pem);
0 commit comments