Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit f66d9f3

Browse files
committed
Remove intermediate allocations from ToString methods
1 parent baaa017 commit f66d9f3

File tree

3 files changed

+124
-80
lines changed

3 files changed

+124
-80
lines changed

src/System.Security.Cryptography.X509Certificates/src/System/Security/Cryptography/X509Certificates/X509Certificate.cs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -262,32 +262,42 @@ public virtual String ToString(bool fVerbose)
262262
StringBuilder sb = new StringBuilder();
263263

264264
// Subject
265-
sb.Append("[Subject]" + Environment.NewLine + " ");
266-
sb.Append(this.Subject);
265+
sb.AppendLine("[Subject]");
266+
sb.Append(" ");
267+
sb.AppendLine(this.Subject);
267268

268269
// Issuer
269-
sb.Append(Environment.NewLine + Environment.NewLine + "[Issuer]" + Environment.NewLine + " ");
270-
sb.Append(this.Issuer);
270+
sb.AppendLine();
271+
sb.AppendLine("[Issuer]");
272+
sb.Append(" ");
273+
sb.AppendLine(this.Issuer);
271274

272275
// Serial Number
273-
sb.Append(Environment.NewLine + Environment.NewLine + "[Serial Number]" + Environment.NewLine + " ");
276+
sb.AppendLine();
277+
sb.AppendLine("[Serial Number]");
278+
sb.Append(" ");
274279
byte[] serialNumber = this.GetSerialNumber();
275280
Array.Reverse(serialNumber);
276-
sb.Append(serialNumber.ToHexStringUpper());
281+
sb.AppendLine(serialNumber.ToHexStringUpper());
277282

278283
// NotBefore
279-
sb.Append(Environment.NewLine + Environment.NewLine + "[Not Before]" + Environment.NewLine + " ");
280-
sb.Append(FormatDate(this.GetNotBefore()));
284+
sb.AppendLine();
285+
sb.AppendLine("[Not Before]");
286+
sb.Append(" ");
287+
sb.AppendLine(FormatDate(this.GetNotBefore()));
281288

282289
// NotAfter
283-
sb.Append(Environment.NewLine + Environment.NewLine + "[Not After]" + Environment.NewLine + " ");
284-
sb.Append(FormatDate(this.GetNotAfter()));
290+
sb.AppendLine();
291+
sb.AppendLine("[Not After]");
292+
sb.Append(" ");
293+
sb.AppendLine(FormatDate(this.GetNotAfter()));
285294

286295
// Thumbprint
287-
sb.Append(Environment.NewLine + Environment.NewLine + "[Thumbprint]" + Environment.NewLine + " ");
288-
sb.Append(this.GetCertHash().ToHexStringUpper());
296+
sb.AppendLine();
297+
sb.AppendLine("[Thumbprint]");
298+
sb.Append(" ");
299+
sb.AppendLine(this.GetCertHash().ToHexStringUpper());
289300

290-
sb.Append(Environment.NewLine);
291301
return sb.ToString();
292302
}
293303

@@ -362,4 +372,3 @@ internal static String FormatDate(DateTime date)
362372
private const X509KeyStorageFlags KeyStorageFlagsAll = (X509KeyStorageFlags)0x1f;
363373
}
364374
}
365-

src/System.Security.Cryptography.X509Certificates/src/System/Security/Cryptography/X509Certificates/X509Certificate2.cs

Lines changed: 76 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -307,146 +307,156 @@ public override String ToString(bool verbose)
307307
return ToString();
308308

309309
StringBuilder sb = new StringBuilder();
310-
String newLine = Environment.NewLine;
311-
String newLine2 = newLine + newLine;
312-
String newLinesp2 = newLine + " ";
313310

314311
// Version
315-
sb.Append("[Version]");
316-
sb.Append(newLinesp2);
317-
sb.Append("V" + this.Version);
312+
sb.AppendLine("[Version]");
313+
sb.Append(" V");
314+
sb.Append(this.Version);
318315

319316
// Subject
320-
sb.Append(newLine2);
321-
sb.Append("[Subject]");
322-
sb.Append(newLinesp2);
317+
sb.AppendLine();
318+
sb.AppendLine();
319+
sb.AppendLine("[Subject]");
320+
sb.Append(" ");
323321
sb.Append(this.SubjectName.Name);
324322
String simpleName = GetNameInfo(X509NameType.SimpleName, false);
325323
if (simpleName.Length > 0)
326324
{
327-
sb.Append(newLinesp2);
325+
sb.AppendLine();
326+
sb.Append(" ");
328327
sb.Append("Simple Name: ");
329328
sb.Append(simpleName);
330329
}
331330
String emailName = GetNameInfo(X509NameType.EmailName, false);
332331
if (emailName.Length > 0)
333332
{
334-
sb.Append(newLinesp2);
333+
sb.AppendLine();
334+
sb.Append(" ");
335335
sb.Append("Email Name: ");
336336
sb.Append(emailName);
337337
}
338338
String upnName = GetNameInfo(X509NameType.UpnName, false);
339339
if (upnName.Length > 0)
340340
{
341-
sb.Append(newLinesp2);
341+
sb.AppendLine();
342+
sb.Append(" ");
342343
sb.Append("UPN Name: ");
343344
sb.Append(upnName);
344345
}
345346
String dnsName = GetNameInfo(X509NameType.DnsName, false);
346347
if (dnsName.Length > 0)
347348
{
348-
sb.Append(newLinesp2);
349+
sb.AppendLine();
350+
sb.Append(" ");
349351
sb.Append("DNS Name: ");
350352
sb.Append(dnsName);
351353
}
352354

353355
// Issuer
354-
sb.Append(newLine2);
355-
sb.Append("[Issuer]");
356-
sb.Append(newLinesp2);
356+
sb.AppendLine();
357+
sb.AppendLine();
358+
sb.AppendLine("[Issuer]");
359+
sb.Append(" ");
357360
sb.Append(this.IssuerName.Name);
358361
simpleName = GetNameInfo(X509NameType.SimpleName, true);
359362
if (simpleName.Length > 0)
360363
{
361-
sb.Append(newLinesp2);
364+
sb.AppendLine();
365+
sb.Append(" ");
362366
sb.Append("Simple Name: ");
363367
sb.Append(simpleName);
364368
}
365369
emailName = GetNameInfo(X509NameType.EmailName, true);
366370
if (emailName.Length > 0)
367371
{
368-
sb.Append(newLinesp2);
372+
sb.AppendLine();
373+
sb.Append(" ");
369374
sb.Append("Email Name: ");
370375
sb.Append(emailName);
371376
}
372377
upnName = GetNameInfo(X509NameType.UpnName, true);
373378
if (upnName.Length > 0)
374379
{
375-
sb.Append(newLinesp2);
380+
sb.AppendLine();
381+
sb.Append(" ");
376382
sb.Append("UPN Name: ");
377383
sb.Append(upnName);
378384
}
379385
dnsName = GetNameInfo(X509NameType.DnsName, true);
380386
if (dnsName.Length > 0)
381387
{
382-
sb.Append(newLinesp2);
388+
sb.AppendLine();
389+
sb.Append(" ");
383390
sb.Append("DNS Name: ");
384391
sb.Append(dnsName);
385392
}
386393

387394
// Serial Number
388-
sb.Append(newLine2);
389-
sb.Append("[Serial Number]");
390-
sb.Append(newLinesp2);
391-
sb.Append(this.SerialNumber);
395+
sb.AppendLine();
396+
sb.AppendLine();
397+
sb.AppendLine("[Serial Number]");
398+
sb.Append(" ");
399+
sb.AppendLine(this.SerialNumber);
392400

393401
// NotBefore
394-
sb.Append(newLine2);
395-
sb.Append("[Not Before]");
396-
sb.Append(newLinesp2);
397-
sb.Append(FormatDate(this.NotBefore));
402+
sb.AppendLine();
403+
sb.AppendLine("[Not Before]");
404+
sb.Append(" ");
405+
sb.AppendLine(FormatDate(this.NotBefore));
398406

399407
// NotAfter
400-
sb.Append(newLine2);
401-
sb.Append("[Not After]");
402-
sb.Append(newLinesp2);
403-
sb.Append(FormatDate(this.NotAfter));
408+
sb.AppendLine();
409+
sb.AppendLine("[Not After]");
410+
sb.Append(" ");
411+
sb.AppendLine(FormatDate(this.NotAfter));
404412

405413
// Thumbprint
406-
sb.Append(newLine2);
407-
sb.Append("[Thumbprint]");
408-
sb.Append(newLinesp2);
409-
sb.Append(this.Thumbprint);
414+
sb.AppendLine();
415+
sb.AppendLine("[Thumbprint]");
416+
sb.Append(" ");
417+
sb.AppendLine(this.Thumbprint);
410418

411419
// Signature Algorithm
412-
sb.Append(newLine2);
413-
sb.Append("[Signature Algorithm]");
414-
sb.Append(newLinesp2);
415-
sb.Append(this.SignatureAlgorithm.FriendlyName + "(" + this.SignatureAlgorithm.Value + ")");
420+
sb.AppendLine();
421+
sb.AppendLine("[Signature Algorithm]");
422+
sb.Append(" ");
423+
sb.Append(this.SignatureAlgorithm.FriendlyName);
424+
sb.Append('(');
425+
sb.Append(this.SignatureAlgorithm.Value);
426+
sb.AppendLine(")");
416427

417428
// Public Key
418-
sb.Append(newLine2);
429+
sb.AppendLine();
419430
sb.Append("[Public Key]");
420431
// It could throw if it's some user-defined CryptoServiceProvider
421432
try
422433
{
423434
PublicKey pubKey = this.PublicKey;
424435

425-
String temp = pubKey.Oid.FriendlyName;
426-
sb.Append(newLinesp2);
436+
sb.AppendLine();
437+
sb.Append(" ");
427438
sb.Append("Algorithm: ");
428-
sb.Append(temp);
439+
sb.Append(pubKey.Oid.FriendlyName);
429440
// So far, we only support RSACryptoServiceProvider & DSACryptoServiceProvider Keys
430441
try
431442
{
432-
temp = pubKey.Key.KeySize.ToString();
433-
sb.Append(newLinesp2);
443+
sb.AppendLine();
444+
sb.Append(" ");
434445
sb.Append("Length: ");
435-
sb.Append(temp);
446+
sb.Append(pubKey.Key.KeySize);
436447
}
437448
catch (NotSupportedException)
438449
{
439450
}
440451

441-
temp = pubKey.EncodedKeyValue.Format(true);
442-
sb.Append(newLinesp2);
452+
sb.AppendLine();
453+
sb.Append(" ");
443454
sb.Append("Key Blob: ");
444-
sb.Append(temp);
455+
sb.AppendLine(pubKey.EncodedKeyValue.Format(true));
445456

446-
temp = pubKey.EncodedParameters.Format(true);
447-
sb.Append(newLinesp2);
457+
sb.Append(" ");
448458
sb.Append("Parameters: ");
449-
sb.Append(temp);
459+
sb.Append(pubKey.EncodedParameters.Format(true));
450460
}
451461
catch (CryptographicException)
452462
{
@@ -459,29 +469,31 @@ public override String ToString(bool verbose)
459469
X509ExtensionCollection extensions = this.Extensions;
460470
if (extensions.Count > 0)
461471
{
462-
sb.Append(newLine2);
472+
sb.AppendLine();
473+
sb.AppendLine();
463474
sb.Append("[Extensions]");
464-
String temp;
465475
foreach (X509Extension extension in extensions)
466476
{
467477
try
468478
{
469-
temp = extension.Oid.FriendlyName;
470-
sb.Append(newLine);
471-
sb.Append("* " + temp);
472-
sb.Append("(" + extension.Oid.Value + "):");
473-
474-
temp = extension.Format(true);
475-
sb.Append(newLinesp2);
476-
sb.Append(temp);
479+
sb.AppendLine();
480+
sb.Append("* ");
481+
sb.Append(extension.Oid.FriendlyName);
482+
sb.Append('(');
483+
sb.Append(extension.Oid.Value);
484+
sb.Append("):");
485+
486+
sb.AppendLine();
487+
sb.Append(" ");
488+
sb.Append(extension.Format(true));
477489
}
478490
catch (CryptographicException)
479491
{
480492
}
481493
}
482494
}
483495

484-
sb.Append(newLine);
496+
sb.AppendLine();
485497
return sb.ToString();
486498
}
487499

@@ -518,4 +530,3 @@ private static X509Extension CreateCustomExtensionIfAny(Oid oid)
518530
private volatile X509ExtensionCollection _lazyExtensions;
519531
}
520532
}
521-

src/System.Security.Cryptography.X509Certificates/tests/CertTests.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,30 @@ public static void X509CertStoreChain()
104104
}
105105
}
106106

107+
[Fact]
108+
public static void X509CertEmptyToString()
109+
{
110+
using (var c = new X509Certificate())
111+
{
112+
string expectedResult = "System.Security.Cryptography.X509Certificates.X509Certificate";
113+
Assert.Equal(expectedResult, c.ToString());
114+
Assert.Equal(expectedResult, c.ToString(false));
115+
Assert.Equal(expectedResult, c.ToString(true));
116+
}
117+
}
118+
119+
[Fact]
120+
public static void X509Cert2EmptyToString()
121+
{
122+
using (var c2 = new X509Certificate2())
123+
{
124+
string expectedResult = "System.Security.Cryptography.X509Certificates.X509Certificate2";
125+
Assert.Equal(expectedResult, c2.ToString());
126+
Assert.Equal(expectedResult, c2.ToString(false));
127+
Assert.Equal(expectedResult, c2.ToString(true));
128+
}
129+
}
130+
107131
[Fact]
108132
[ActiveIssue(1993, PlatformID.AnyUnix)]
109133
public static void X509Cert2ToStringVerbose()
@@ -113,7 +137,7 @@ public static void X509Cert2ToStringVerbose()
113137

114138
foreach (X509Certificate2 c in store.Certificates)
115139
{
116-
Assert.NotNull(c.ToString(true));
140+
Assert.False(string.IsNullOrWhiteSpace(c.ToString(true)));
117141
}
118142
}
119143

0 commit comments

Comments
 (0)