Generate hash from UIImage

前端 未结 4 1670
忘了有多久
忘了有多久 2020-11-28 07:59

I\'m trying to compare two UIImages from the file system to see if they are the same. Obviously, I can\'t use NSObject\'s hash method, since this returns a hash of the objec

相关标签:
4条回答
  • 2020-11-28 08:55

    more elegant code below

    +(NSString *)MD5HexDigest:(NSData *)input {
        unsigned char result[CC_MD5_DIGEST_LENGTH];
    
        CC_MD5(input.bytes, (unsigned int)input.length, result);
        NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH*2];
        for (int i = 0; i<CC_MD5_DIGEST_LENGTH; i++) {
            [ret appendFormat:@"%02x",result[i]];
        }
        return ret;
    }

    0 讨论(0)
  • 2020-11-28 08:57

    A less than optimal solution:

    [ UIImagePNGRepresentation( uiImage1 ) isEqualToData: 
          UIImagePNGRepresentation( uiImage2 ) ];
    

    This basically compares the PNG encoded data of the 2 images. Since image similarity is a complex subject, better and faster solutions can be devised based on what exactly the end goal is (i.e. are you looking to compare images, pixel by pixel, or just approximate similarity, which could use a downsampled version of the source image, etc).

    0 讨论(0)
  • 2020-11-28 09:03

    I wound up using the following code to accomplish the task. Note that this requires that you import <CommonCrypto/CommonDigest.h>:

    unsigned char result[CC_MD5_DIGEST_LENGTH];
    NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(inImage)];
    CC_MD5([imageData bytes], [imageData length], result);
    NSString *imageHash = [NSString stringWithFormat:
                           @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
                           result[0], result[1], result[2], result[3], 
                           result[4], result[5], result[6], result[7],
                           result[8], result[9], result[10], result[11],
                           result[12], result[13], result[14], result[15]
                           ];
    
    0 讨论(0)
  • 2020-11-28 09:03

    UIImage Hashing

    Swift Code for hashing a UIImage with the SHA256 algorithm in swift 4.2. There are other algorithms you can use, which are may faster or generate less duplicates but this one is good enough for most usecases.

    Just drop the code somewhere in your Project and use it like this: yourUIImage.sha256()

    extension UIImage{
        
        public func sha256() -> String{
            if let imageData = cgImage?.dataProvider?.data as? Data {
                return hexStringFromData(input: digest(input: imageData as NSData))
            }
            return ""
        }
        
        private func digest(input : NSData) -> NSData {
            let digestLength = Int(CC_SHA256_DIGEST_LENGTH)
            var hash = [UInt8](repeating: 0, count: digestLength)
            CC_SHA256(input.bytes, UInt32(input.length), &hash)
            return NSData(bytes: hash, length: digestLength)
        }
        
        private  func hexStringFromData(input: NSData) -> String {
            var bytes = [UInt8](repeating: 0, count: input.length)
            input.getBytes(&bytes, length: input.length)
            
            var hexString = ""
            for byte in bytes {
                hexString += String(format:"%02x", UInt8(byte))
            }
            
            return hexString
        }
    }
    

    Bridging Header

    By the way you need a Bridging Header that imports CommonCrypto. If you don't have one follow these steps:

    1. Create new File -> Header File -> Save as BridgingHeader
    2. In Build Settings -> Objective-C Bridging Header -> add ProjectName/BridgingHeader.h
    3. Put #import <CommonCrypto/CommonHMAC.h> in your Header File

    Duplicate Probability

    The duplicate probability is low, but you can upgrade to SHA512 relatively easy if you got huge datasets:

    The Duplicate Probability is about:

    Source: https://crypto.stackexchange.com/questions/24732/probability-of-sha256-collisions-for-certain-amount-of-hashed-values

    0 讨论(0)
提交回复
热议问题