Skip to content

Commit 215a79a

Browse files
Standardize netfx test helper for pem encoding (#118259)
1 parent 5ed4079 commit 215a79a

File tree

1 file changed

+20
-2
lines changed
  • src/libraries/Common/tests/System/Security/Cryptography

1 file changed

+20
-2
lines changed

src/libraries/Common/tests/System/Security/Cryptography/ByteUtils.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,26 @@ internal static string PemEncode(string label, byte[] data)
109109
#if NET
110110
return PemEncoding.WriteString(label, data);
111111
#else
112-
return
113-
$"-----BEGIN {label}-----\n{Convert.ToBase64String(data, Base64FormattingOptions.InsertLineBreaks)}\n-----END {label}-----";
112+
StringBuilder sb = new StringBuilder();
113+
114+
sb.Append("-----BEGIN ");
115+
sb.Append(label);
116+
sb.Append("-----\n");
117+
118+
string base64 = Convert.ToBase64String(data);
119+
120+
for (int i = 0; i < base64.Length; i += 64)
121+
{
122+
int len = Math.Min(64, base64.Length - i);
123+
sb.Append(base64, i, len);
124+
sb.Append('\n');
125+
}
126+
127+
sb.Append("-----END ");
128+
sb.Append(label);
129+
sb.Append("-----");
130+
131+
return sb.ToString();
114132
#endif
115133
}
116134
}

0 commit comments

Comments
 (0)