99
1010import com .newrelic .agent .Agent ;
1111
12- import java .util .Collections ;
12+ import java .util .Arrays ;
1313import java .util .regex .Matcher ;
1414import java .util .regex .Pattern ;
1515
@@ -33,6 +33,11 @@ public static String obfuscateLicenseKey(String originalString) {
3333 return originalString ;
3434 }
3535
36+ // Avoid regex overhead if string doesn't contain "license_key"
37+ if (!originalString .contains ("license_key" )) {
38+ return originalString ;
39+ }
40+
3641 Matcher matcher = LICENSE_KEY_PATTERN .matcher (originalString );
3742 if (!matcher .find ()) {
3843 return originalString ;
@@ -60,10 +65,15 @@ private static String partialObfuscation(String licenseKey) {
6065 // Otherwise, keep the first 10 characters of the key and replace the
6166 // remaining key characters with "*"
6267 if (keyLength > KEY_LENGTH_CUTOFF ) {
63- return licenseKey .substring (0 , KEY_LENGTH_CUTOFF ) +
64- String .join ("" , Collections .nCopies (keyLength - KEY_LENGTH_CUTOFF , "*" ));
68+ return licenseKey .substring (0 , KEY_LENGTH_CUTOFF ) + createAsterisks (keyLength - KEY_LENGTH_CUTOFF );
6569 } else {
66- return String . join ( "" , Collections . nCopies ( keyLength , "*" ) );
70+ return createAsterisks ( keyLength );
6771 }
6872 }
73+
74+ private static String createAsterisks (int count ) {
75+ char [] asterisks = new char [count ];
76+ Arrays .fill (asterisks , '*' );
77+ return new String (asterisks );
78+ }
6979}
0 commit comments