diff --git a/Piwik.Tracker.Tests/CryptoExtensionsTests.cs b/Piwik.Tracker.Tests/CryptoExtensionsTests.cs
new file mode 100644
index 0000000..032119a
--- /dev/null
+++ b/Piwik.Tracker.Tests/CryptoExtensionsTests.cs
@@ -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));
+ }
+ }
+}
\ No newline at end of file
diff --git a/Piwik.Tracker.Tests/Piwik.Tracker.Tests.csproj b/Piwik.Tracker.Tests/Piwik.Tracker.Tests.csproj
index 2a05905..07ccdb3 100644
--- a/Piwik.Tracker.Tests/Piwik.Tracker.Tests.csproj
+++ b/Piwik.Tracker.Tests/Piwik.Tracker.Tests.csproj
@@ -84,6 +84,7 @@
+
diff --git a/Piwik.Tracker/CryptoExtensions.cs b/Piwik.Tracker/CryptoExtensions.cs
new file mode 100644
index 0000000..87dd984
--- /dev/null
+++ b/Piwik.Tracker/CryptoExtensions.cs
@@ -0,0 +1,47 @@
+using System;
+using System.Security.Cryptography;
+using System.Text;
+
+namespace Piwik.Tracker
+{
+ internal static class CryptoExtensions
+ {
+ ///
+ /// Creates a sha1 hash from given .
+ ///
+ /// The value to encrypt.
+ ///
+ public static string ToSha1(this string valueToEncrypt)
+ {
+ if (valueToEncrypt == null)
+ {
+ throw new ArgumentNullException(nameof(valueToEncrypt));
+ }
+ return Encoding.UTF8.GetBytes(valueToEncrypt).ToSha1();
+ }
+
+ ///
+ /// Creates a sha1 hash from given .
+ ///
+ /// The value to encrypt.
+ ///
+ 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();
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/Piwik.Tracker/Piwik.Tracker.csproj b/Piwik.Tracker/Piwik.Tracker.csproj
index 2c152b9..9ab6c6d 100644
--- a/Piwik.Tracker/Piwik.Tracker.csproj
+++ b/Piwik.Tracker/Piwik.Tracker.csproj
@@ -46,6 +46,7 @@
+
diff --git a/Piwik.Tracker/PiwikTracker.cs b/Piwik.Tracker/PiwikTracker.cs
index 0f6d0fb..5ebe865 100644
--- a/Piwik.Tracker/PiwikTracker.cs
+++ b/Piwik.Tracker/PiwikTracker.cs
@@ -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;
@@ -431,8 +429,7 @@ public void ClearCustomTrackingParameters()
///
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;
@@ -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;
}
- ///
- /// Gets the hexadecimal string from bytes.
- ///
- /// The bytes.
- ///
- 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();
- }
-
///
/// Tracks a page view
///
@@ -1177,8 +1162,8 @@ public void SetUserId(string userId)
///
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);
}
///