From 772b3a6ba1b664cf2caae09cca7508d7b5f431b0 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Fri, 12 May 2017 09:21:33 -0400 Subject: [PATCH] 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