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

Commit 223e904

Browse files
committed
Keep things DRY.
Move `GetSha256Hash` into `StringExtensions` as it's needed in >1 place.
1 parent 24dec7d commit 223e904

File tree

3 files changed

+25
-48
lines changed

3 files changed

+25
-48
lines changed

src/GitHub.App/Api/ApiClient.cs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -149,30 +149,10 @@ public IObservable<LicenseMetadata> GetLicenses()
149149

150150
public HostAddress HostAddress { get; }
151151

152-
static string GetSha256Hash(string input)
153-
{
154-
Guard.ArgumentNotEmptyString(input, nameof(input));
155-
156-
try
157-
{
158-
using (var sha256 = SHA256.Create())
159-
{
160-
var bytes = Encoding.UTF8.GetBytes(input);
161-
var hash = sha256.ComputeHash(bytes);
162-
163-
return string.Join("", hash.Select(b => b.ToString("x2", CultureInfo.InvariantCulture)));
164-
}
165-
}
166-
catch (Exception e)
167-
{
168-
log.Error(e, "IMPOSSIBLE! Generating Sha256 hash caused an exception");
169-
return null;
170-
}
171-
}
172-
173152
static string GetFingerprint()
174153
{
175-
return GetSha256Hash(ProductName + ":" + GetMachineIdentifier());
154+
var fingerprint = ProductName + ":" + GetMachineIdentifier();
155+
return fingerprint.GetSha256Hash();
176156
}
177157

178158
static string GetMachineNameSafe()

src/GitHub.App/Services/PullRequestService.cs

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,14 @@
1616
using System.Collections.Generic;
1717
using LibGit2Sharp;
1818
using GitHub.Logging;
19-
using System.Security.Cryptography;
2019
using GitHub.Extensions;
21-
using Serilog;
2220

2321
namespace GitHub.Services
2422
{
2523
[Export(typeof(IPullRequestService))]
2624
[PartCreationPolicy(CreationPolicy.Shared)]
2725
public class PullRequestService : IPullRequestService
2826
{
29-
static readonly ILogger log = LogManager.ForContext<PullRequestService>();
3027
const string SettingCreatedByGHfVS = "created-by-ghfvs";
3128
const string SettingGHfVSPullRequest = "ghfvs-pr-owner-number";
3229

@@ -413,7 +410,7 @@ public IObservable<Unit> SwitchToBranch(ILocalRepositoryModel repository, IPullR
413410
{
414411
var branchName = GetLocalBranchesInternal(repository, repo, pullRequest).FirstOrDefault();
415412

416-
log.Assert(branchName != null, "PullRequestService.SwitchToBranch called but no local branch found");
413+
Log.Assert(branchName != null, "PullRequestService.SwitchToBranch called but no local branch found");
417414

418415
if (branchName != null)
419416
{
@@ -681,7 +678,7 @@ static string CalculateTempFileName(string relativePath, string commitSha, Encod
681678
// The combination of relative path, commit SHA and encoding should be sufficient to uniquely identify a file.
682679
var relativeDir = Path.GetDirectoryName(relativePath) ?? string.Empty;
683680
var key = relativeDir + '|' + encoding.WebName;
684-
var relativePathHash = GetSha256Hash(key);
681+
var relativePathHash = key.GetSha256Hash();
685682
var tempDir = Path.Combine(Path.GetTempPath(), "GitHubVisualStudio", "FileContents", relativePathHash);
686683
var tempFileName = $"{Path.GetFileNameWithoutExtension(relativePath)}@{commitSha}{Path.GetExtension(relativePath)}";
687684
return Path.Combine(tempDir, tempFileName);
@@ -713,26 +710,5 @@ static Tuple<string, int> ParseGHfVSConfigKeyValue(string value)
713710

714711
return null;
715712
}
716-
717-
static string GetSha256Hash(string input)
718-
{
719-
Guard.ArgumentNotNull(input, nameof(input));
720-
721-
try
722-
{
723-
using (var sha256 = SHA256.Create())
724-
{
725-
var bytes = Encoding.UTF8.GetBytes(input);
726-
var hash = sha256.ComputeHash(bytes);
727-
728-
return string.Join("", hash.Select(b => b.ToString("x2", CultureInfo.InvariantCulture)));
729-
}
730-
}
731-
catch (Exception e)
732-
{
733-
log.Error(e, "IMPOSSIBLE! Generating Sha256 hash caused an exception");
734-
return null;
735-
}
736-
}
737713
}
738714
}

src/GitHub.Extensions/StringExtensions.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
using System.Globalization;
55
using System.IO;
66
using System.Linq;
7+
using System.Security.Cryptography;
78
using System.Text;
89
using System.Text.RegularExpressions;
10+
using GitHub.Logging;
11+
using Splat;
912

1013
namespace GitHub.Extensions
1114
{
@@ -222,5 +225,23 @@ public static string Humanize(this string s)
222225
var combined = String.Join(" ", result);
223226
return Char.ToUpper(combined[0], CultureInfo.InvariantCulture) + combined.Substring(1);
224227
}
228+
229+
/// <summary>
230+
/// Generates a SHA256 hash for a string.
231+
/// </summary>
232+
/// <param name="input">The input string.</param>
233+
/// <returns>The SHA256 hash.</returns>
234+
public static string GetSha256Hash(this string input)
235+
{
236+
Guard.ArgumentNotNull(input, nameof(input));
237+
238+
using (var sha256 = SHA256.Create())
239+
{
240+
var bytes = Encoding.UTF8.GetBytes(input);
241+
var hash = sha256.ComputeHash(bytes);
242+
243+
return string.Join("", hash.Select(b => b.ToString("x2", CultureInfo.InvariantCulture)));
244+
}
245+
}
225246
}
226247
}

0 commit comments

Comments
 (0)