Skip to content

Commit 35263ff

Browse files
committed
* Move Obfuscate out of StringExtensions
1 parent b8e8099 commit 35263ff

File tree

4 files changed

+38
-80
lines changed

4 files changed

+38
-80
lines changed

code/src/CoreTemplateStudio/CoreTemplateStudio.Core/Extensions/StringExtensions.cs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,6 @@ namespace Microsoft.Templates.Core
1313
{
1414
public static class StringExtensions
1515
{
16-
public static string Obfuscate(this string data)
17-
{
18-
string result = data;
19-
byte[] b64data = Encoding.UTF8.GetBytes(data);
20-
21-
using (SHA512 sha2 = SHA512.Create())
22-
{
23-
result = GetHash(sha2, b64data);
24-
}
25-
26-
return result.ToUpperInvariant();
27-
}
28-
29-
private static string GetHash(HashAlgorithm md5Hash, byte[] inputData)
30-
{
31-
byte[] data = md5Hash.ComputeHash(inputData);
32-
33-
var sb = new StringBuilder();
34-
35-
for (int i = 0; i < data.Length; i++)
36-
{
37-
sb.Append(data[i].ToString("x2"));
38-
}
39-
40-
return sb.ToString();
41-
}
42-
4316
public static string[] GetMultiValue(this string value)
4417
{
4518
if (string.IsNullOrWhiteSpace(value) || string.IsNullOrEmpty(value.Trim()))

code/src/CoreTemplateStudio/CoreTemplateStudio.Core/Packaging/TemplatePackage.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Security;
1212
using System.Security.Cryptography;
1313
using System.Security.Cryptography.X509Certificates;
14+
using System.Text;
1415
using System.Threading;
1516
using System.Threading.Tasks;
1617

@@ -262,7 +263,7 @@ public List<CertInfo> GetCertsInfo(string signedPackageFilename)
262263
var certInfo = new CertInfo()
263264
{
264265
Cert = new X509Certificate2(cert),
265-
Pin = cert.GetPublicKeyString().Obfuscate(),
266+
Pin = Obfuscate(cert.GetPublicKeyString()),
266267
Status = _digitalSignatureService.VerifyCertificate(cert),
267268
};
268269

@@ -340,11 +341,43 @@ private bool VerifyCertificate(X509Certificate cert)
340341
return status == X509ChainStatusFlags.NoError;
341342
}
342343

344+
private static string GetHash(HashAlgorithm md5Hash, byte[] inputData)
345+
{
346+
byte[] data = md5Hash.ComputeHash(inputData);
347+
348+
var sb = new StringBuilder();
349+
350+
for (int i = 0; i < data.Length; i++)
351+
{
352+
sb.Append(data[i].ToString("x2"));
353+
}
354+
355+
return sb.ToString();
356+
}
357+
358+
private static string Obfuscate(string data)
359+
{
360+
if (string.IsNullOrWhiteSpace(data))
361+
{
362+
return string.Empty;
363+
}
364+
365+
string result = data;
366+
byte[] b64data = Encoding.UTF8.GetBytes(data);
367+
368+
using (SHA512 sha2 = SHA512.Create())
369+
{
370+
result = GetHash(sha2, b64data);
371+
}
372+
373+
return result.ToUpperInvariant();
374+
}
375+
343376
private bool VerifyAllowedPublicKey(X509Certificate cert)
344377
{
345378
var pubKeyCert = cert.GetPublicKeyString();
346379

347-
var pubKeyPin = pubKeyCert.Obfuscate();
380+
var pubKeyPin = Obfuscate(pubKeyCert);
348381

349382
AppHealth.Current.Verbose.TrackAsync($"{StringRes.PackageCertificateString} {cert.Subject}").FireAndForget();
350383
AppHealth.Current.Verbose.TrackAsync($"Key: {pubKeyCert}").FireAndForget();

code/test/CoreTemplateStudio.Core.Test/Extensions/StringExtensionsTests.cs

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,55 +11,6 @@ namespace Microsoft.Templates.Core.Test.Extensions
1111
[Trait("ExecutionSet", "Minimum")]
1212
public class StringExtensionsTests
1313
{
14-
[Theory]
15-
[InlineData("string")]
16-
[InlineData("string23TEST")]
17-
[InlineData(" string23TEST")]
18-
[InlineData("string23TEST ")]
19-
[InlineData("str ing23TEST")]
20-
public void Obfuscate_ShouldNotBeEmpty(string value)
21-
{
22-
var actual = value.Obfuscate();
23-
24-
Assert.NotEmpty(actual);
25-
}
26-
27-
[Theory]
28-
[InlineData("string")]
29-
[InlineData("string23TEST")]
30-
[InlineData(" string23TEST")]
31-
[InlineData("string23TEST ")]
32-
[InlineData("str ing23TEST")]
33-
public void Obfuscate_ShouldNotEqualTheOriginalValue(string value)
34-
{
35-
var actual = value.Obfuscate();
36-
37-
Assert.False(value.Equals(actual, StringComparison.Ordinal));
38-
}
39-
40-
[Theory]
41-
[InlineData("string")]
42-
[InlineData("string23TEST")]
43-
[InlineData(" string23TEST")]
44-
[InlineData("string23TEST ")]
45-
[InlineData("str ing23TEST")]
46-
public void Obfuscate_ShouldHaveBeenConvertedToUppercase(string value)
47-
{
48-
var actual = value.Obfuscate();
49-
50-
Assert.DoesNotContain(actual, c => char.IsLower(c));
51-
}
52-
53-
[Fact(Skip = "Control null and empty scenarios")]
54-
public void Obfuscate_Null_ShouldBeControlled()
55-
{
56-
// var actual = value.Obfuscate();
57-
58-
// control these scenarios.
59-
// used from PackagePackage > VerifyAllowedPublicKey
60-
// used from PackagePackage > GetCertsInfo
61-
}
62-
6314
[Theory]
6415
[InlineData("")]
6516
[InlineData(" ")]

code/test/CoreTemplateStudio.Core.Test/Helpers/LogFixture.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace Microsoft.Templates.Core.Test.Helpers.FsTests.Helpers
1212
{
13-
[SuppressMessage("Design", "CA1063:Implement IDisposable Correctly", Justification = "Testing purposes")]
13+
[SuppressMessage("Design", "CA1063:Implement IDisposable Correctly", Justification = "Testing purposes only")]
1414
public class LogFixture : IDisposable
1515
{
1616
public string LogFile { get; private set; }
@@ -19,7 +19,7 @@ public class LogFixture : IDisposable
1919

2020
public LogFixture()
2121
{
22-
TestFolderPath = Path.Combine(Environment.CurrentDirectory, "TestFolderPath");
22+
TestFolderPath = Path.Combine(Environment.CurrentDirectory, "TempTestData");
2323
Directory.CreateDirectory(TestFolderPath);
2424

2525
LogFile = Path.Combine(
@@ -61,6 +61,7 @@ public bool IsErrorMessageInLogFile(DateTime logDate, string errorLevel, string
6161
return false;
6262
}
6363

64+
[SuppressMessage("Usage", "CA1816:Dispose methods should call SuppressFinalize", Justification = "Testing purposes only")]
6465
public void Dispose()
6566
{
6667
if (Directory.Exists(TestFolderPath))

0 commit comments

Comments
 (0)