-
Notifications
You must be signed in to change notification settings - Fork 47
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
julienmoumne
merged 6 commits into
matomo-org:master
from
ptr1120:RegressionTestsForHashGeneration
Apr 18, 2017
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bb6dc46
- extracted generation of hashes to cryptoextensions
ptr1120 a494020
fixed 2 false positive test expectations
ptr1120 966afce
introduced explicit usage of UTF8 encoding for hash generation
ptr1120 3b6c75a
- Sha1 removes hyphens now by default
ptr1120 e98d5bc
sha1 : added overload for byte array
ptr1120 d1dc50d
removed obsolete ToLower ("x2" formats as hex lowercase!)
ptr1120 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a big deal but I think
ToLower
is not required here sinceToSha1
produces lower case chars right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, thats right "x2" formats as lowercase hex. Done.