From 5ac08dfeb48184ee89227a6e47c7a263d05b9a15 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Thu, 11 May 2017 12:59:03 -0400 Subject: [PATCH 1/3] hashable method to detect when contact has changed // FREEBIE --- src/Contacts/Contact.m | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/Contacts/Contact.m b/src/Contacts/Contact.m index e455b8069..43152d286 100644 --- a/src/Contacts/Contact.m +++ b/src/Contacts/Contact.m @@ -281,6 +281,22 @@ NS_ASSUME_NONNULL_BEGIN return value; } +- (NSUInteger)hash +{ + NSUInteger hash = 1825038313 ^ self.fullName.hash; + + // NSData.hash appears not to change even when the underlying bytes change. + // maybe it's built on address? + hash = hash ^ self.cnContact.thumbnailImageData.description.hash; + + for (PhoneNumber *phoneNumber in self.parsedPhoneNumbers) { + hash = hash ^ phoneNumber.toE164.hash; + } + + return hash; +} + + @end NS_ASSUME_NONNULL_END From 873d8ff2bcc6af76150a12b81a7be027c289f1fd Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Thu, 11 May 2017 17:56:18 -0400 Subject: [PATCH 2/3] include emails in contat hash // FREEBIE --- src/Contacts/Contact.m | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Contacts/Contact.m b/src/Contacts/Contact.m index 43152d286..9a96bf95c 100644 --- a/src/Contacts/Contact.m +++ b/src/Contacts/Contact.m @@ -293,6 +293,10 @@ NS_ASSUME_NONNULL_BEGIN hash = hash ^ phoneNumber.toE164.hash; } + for (NSString *email in self.emails) { + hash = hash ^ email.hash; + } + return hash; } From 772b3a6ba1b664cf2caae09cca7508d7b5f431b0 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Fri, 12 May 2017 09:21:33 -0400 Subject: [PATCH 3/3] thumbnail hash without allocating big string. // FREEBIE --- src/Contacts/Contact.m | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Contacts/Contact.m b/src/Contacts/Contact.m index 9a96bf95c..b941b8279 100644 --- a/src/Contacts/Contact.m +++ b/src/Contacts/Contact.m @@ -3,6 +3,7 @@ // #import "Contact.h" +#import "Cryptography.h" #import "PhoneNumber.h" #import "SignalRecipient.h" #import "TSAccountManager.h" @@ -283,11 +284,20 @@ NS_ASSUME_NONNULL_BEGIN - (NSUInteger)hash { - NSUInteger hash = 1825038313 ^ self.fullName.hash; + // base hash is some arbitrary number + NSUInteger hash = 1825038313; - // NSData.hash appears not to change even when the underlying bytes change. - // maybe it's built on address? - hash = hash ^ self.cnContact.thumbnailImageData.description.hash; + hash = hash ^ self.fullName.hash; + + // base thumbnailHash is some arbitrary number + NSUInteger thumbnailHash = 389201946; + if (self.cnContact.thumbnailImageData) { + NSData *thumbnailHashData = + [Cryptography computeSHA256Digest:self.cnContact.thumbnailImageData truncatedToBytes:sizeof(thumbnailHash)]; + [thumbnailHashData getBytes:&thumbnailHash length:sizeof(thumbnailHash)]; + } + + hash = hash ^ thumbnailHash; for (PhoneNumber *phoneNumber in self.parsedPhoneNumbers) { hash = hash ^ phoneNumber.toE164.hash; @@ -300,7 +310,6 @@ NS_ASSUME_NONNULL_BEGIN return hash; } - @end NS_ASSUME_NONNULL_END