Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduced usage of Sha1 hash generation with UTF-8 encoding, without hyphens #49

Merged
merged 6 commits into from
Apr 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Piwik.Tracker.Tests/CryptoExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using NUnit.Framework;

namespace Piwik.Tracker.Tests
{
[TestFixture]
internal class CryptoExtensionsTests
{
[Test]
[TestCase("", "da39a3ee5e6b4b0d3255bfef95601890afd80709")]
[TestCase(" ", "b858cb282617fb0956d960215c8e84d1ccf909c6")]
[TestCase("1234dsfa", "644977634278d36d5f451961fe19622ab13cec87")]
[TestCase("1-2-3-45-6", "5c13bf8b7ff1d43869a7b4246bef897f9499833b")]
[TestCase("öüüä%&&", "c24eb4685cd57f32098b33066b5b08b31e378981")]
[TestCase("+- fdgsdgafdgffdsfddgdgdfdfgdfhdghdfghdgfhgfdgar^^°gfra7685&%§$\"$§&(=)(&=,// \\", "bde6cf181dd5bc0ef11342d5c6a4e81a934d9cb8")]
public void ToSha1_RegressionTests(string valueToEncrypt, string expectedHash)
{
//Act
var actualHash = valueToEncrypt.ToSha1();
//Assert
Assert.That(actualHash, Is.EqualTo(expectedHash));
}
}
}
1 change: 1 addition & 0 deletions Piwik.Tracker.Tests/Piwik.Tracker.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="CryptoExtensionsTests.cs" />
<Compile Include="PiwikTrackerTests.cs" />
<Compile Include="PiwikTrackerWithMockedServerTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
47 changes: 47 additions & 0 deletions Piwik.Tracker/CryptoExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Security.Cryptography;
using System.Text;

namespace Piwik.Tracker
{
internal static class CryptoExtensions
{
/// <summary>
/// Creates a sha1 hash from given <paramref name="valueToEncrypt" />.
/// </summary>
/// <param name="valueToEncrypt">The value to encrypt.</param>
/// <returns></returns>
public static string ToSha1(this string valueToEncrypt)
{
if (valueToEncrypt == null)
{
throw new ArgumentNullException(nameof(valueToEncrypt));
}
return Encoding.UTF8.GetBytes(valueToEncrypt).ToSha1();
}

/// <summary>
/// Creates a sha1 hash from given <paramref name="valueToEncrypt" />.
/// </summary>
/// <param name="valueToEncrypt">The value to encrypt.</param>
/// <returns></returns>
public static string ToSha1(this byte[] valueToEncrypt)
{
if (valueToEncrypt == null)
{
throw new ArgumentNullException(nameof(valueToEncrypt));
}
using (var provider = new SHA1CryptoServiceProvider())
{
var encodedBytes = provider.ComputeHash(valueToEncrypt);
var sb = new StringBuilder();
foreach (byte b in encodedBytes)
{
var hex = b.ToString("x2");
sb.Append(hex);
}
return sb.ToString();
}
}
}
}
1 change: 1 addition & 0 deletions Piwik.Tracker/Piwik.Tracker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<ItemGroup>
<Compile Include="AttributionInfo.cs" />
<Compile Include="BrowserPlugins.cs" />
<Compile Include="CryptoExtensions.cs" />
<Compile Include="CustomVar.cs" />
<Compile Include="Enums.cs" />
<Compile Include="PiwikTracker.cs" />
Expand Down
31 changes: 8 additions & 23 deletions Piwik.Tracker/PiwikTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ namespace Piwik.Tracker
using System.Linq;
using System.Net;
using System.Globalization;
using System.Text;
using System.Security.Cryptography;
using System.Web;
using System.Web.Script.Serialization;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -431,8 +429,7 @@ public void ClearCustomTrackingParameters()
/// </summary>
public void SetNewVisitorId()
{
var encodedGuidBytes = new MD5CryptoServiceProvider().ComputeHash(Encoding.Default.GetBytes(Guid.NewGuid().ToString()));
_randomVisitorId = BitConverter.ToString(encodedGuidBytes).Replace("-", "").Substring(0, LengthVisitorId).ToLower();
_randomVisitorId = Guid.NewGuid().ToByteArray().ToSha1().Substring(0, LengthVisitorId);
_userId = null;
_forcedVisitorId = null;
_cookieVisitorId = null;
Expand Down Expand Up @@ -575,26 +572,14 @@ protected static string DomainFixup(string domain)
protected string GetCookieName(string cookieName)
{
// NOTE: If the cookie name is changed, we must also update the method in piwik.js with the same name.
var hash = GetHexStringFromBytes(new SHA1CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes((string.IsNullOrWhiteSpace(_configCookieDomain) ? GetCurrentHost() : _configCookieDomain) + _configCookiePath))).Substring(0, 4);
var cookieDomain = (string.IsNullOrWhiteSpace(_configCookieDomain)
? GetCurrentHost()
: _configCookieDomain)
+ _configCookiePath;
var hash = cookieDomain.ToSha1().Substring(0, 4);
return FirstPartyCookiesPrefix + cookieName + "." + IdSite + "." + hash;
}

/// <summary>
/// Gets the hexadecimal string from bytes.
/// </summary>
/// <param name="bytes">The bytes.</param>
/// <returns></returns>
protected static string GetHexStringFromBytes(byte[] bytes)
{
var sb = new StringBuilder();
foreach (byte b in bytes)
{
var hex = b.ToString("x2");
sb.Append(hex);
}
return sb.ToString();
}

/// <summary>
/// Tracks a page view
/// </summary>
Expand Down Expand Up @@ -1177,8 +1162,8 @@ public void SetUserId(string userId)
/// <returns></returns>
public static string GetUserIdHashed(string id)
{
var encodedIdBytes = new SHA1CryptoServiceProvider().ComputeHash(Encoding.Default.GetBytes(id));
return BitConverter.ToString(encodedIdBytes).Substring(0, 16);
var hash = (id ?? string.Empty).ToSha1();
return hash.Substring(0, 16);
}

/// <summary>
Expand Down