Skip to content

Commit

Permalink
Fix #142 - HashCode isn't deterministic
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Jump committed Oct 8, 2020
1 parent 5b32824 commit 6480a2d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
19 changes: 19 additions & 0 deletions uSync8.Core/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,24 @@ public static string ToShortKeyString(this Guid guid, int length = 26)
return new string(chars);
}


public static int GetDeterministicHashCode(this string str)
{
unchecked
{
int hash1 = (5381 << 16) + 5381;
int hash2 = hash1;

for (int i = 0; i < str.Length; i += 2)
{
hash1 = ((hash1 << 5) + hash1) ^ str[i];
if (i == str.Length - 1)
break;
hash2 = ((hash2 << 5) + hash2) ^ str[i + 1];
}

return hash1 + (hash2 * 1566083941);
}
}
}
}
23 changes: 15 additions & 8 deletions uSync8.Core/Serialization/Serializers/MemberTypeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,17 @@ protected override void SerializeExtraProperties(XElement node, IMemberType item
// get removed when required.
//

private static string[] buildInProperties = new string[] {
"umbracoMemberApproved", "umbracoMemberComments", "umbracoMemberFailedPasswordAttempts",
"umbracoMemberLastLockoutDate", "umbracoMemberLastLogin", "umbracoMemberLastPasswordChangeDate",
"umbracoMemberLockedOut", "umbracoMemberPasswordRetrievalAnswer", "umbracoMemberPasswordRetrievalQuestion"
private static Dictionary<string, string> buildInProperties = new Dictionary<string, string>()
{
{ "umbracoMemberApproved", "e79dccfb-0000-0000-0000-000000000000" },
{ "umbracoMemberComments", "2a280588-0000-0000-0000-000000000000" },
{ "umbracoMemberFailedPasswordAttempts", "0f2ea539-0000-0000-0000-000000000000" },
{ "umbracoMemberLastLockoutDate", "3a7bc3c6-0000-0000-0000-000000000000" },
{ "umbracoMemberLastLogin", "b5e309ba-0000-0000-0000-000000000000" },
{ "umbracoMemberLastPasswordChangeDate", "ded56d3f-0000-0000-0000-000000000000" },
{ "umbracoMemberLockedOut", "c36093d2-0000-0000-0000-000000000000" },
{ "umbracoMemberPasswordRetrievalAnswer", "9700bd39-0000-0000-0000-000000000000" },
{ "umbracoMemberPasswordRetrievalQuestion", "e2d9286a-0000-0000-0000-000000000000" },
};

protected override XElement SerializeProperties(IMemberType item)
Expand All @@ -89,14 +96,14 @@ protected override XElement SerializeProperties(IMemberType item)
foreach (var property in node.Elements("GenericProperty"))
{
var alias = property.Element("Alias").ValueOrDefault(string.Empty);
if (!string.IsNullOrWhiteSpace(alias) && buildInProperties.InvariantContains(alias))
if (!string.IsNullOrWhiteSpace(alias) && buildInProperties.ContainsKey(alias))
{
var keyName = alias;
var key = buildInProperties[alias];
if (!item.Alias.InvariantEquals("Member"))
{
keyName = $"{item.Alias}{alias}";
key = $"{item.Alias}{alias}".GetDeterministicHashCode().ToGuid().ToString();
}
property.Element("Key").Value = keyName.GetHashCode().ToGuid().ToString();
property.Element("Key").Value = key;
}
}
return node;
Expand Down

0 comments on commit 6480a2d

Please sign in to comment.