0 Replies
      Latest reply on Jul 14, 2019 11:32 AM by nullpointr
      nullpointr Level 1 Level 1 (0 points)

        My app lets the user take a camera photo. After the photo was taken sha256 hash should be calculated and sent to server.

         

        I use the following code to save it to photos album and then calculate hash from self.photo which comes from UIImagePickerController:

         

        UIImageWriteToSavedPhotosAlbum(self.photo, self, #selector(saveimage(_:didFinishSavingWithError:contextInfo:)), nil)

         

        let imageData : Data = self.photo!.pngData()!

        let imageHash : String = getImageHash(data: imageData)

         

        func getImageHash(data : Data) -> String {

         

            var hashBytes = [UInt8](repeating: 0,  count: Int(CC_SHA256_DIGEST_LENGTH))

            data.withUnsafeBytes {

                _ = CC_SHA256($0.baseAddress, CC_LONG(data.count), &hashBytes)

            }

         

            var hex = ""

            for index in 0..<Int(CC_SHA256_DIGEST_LENGTH) {

                hex += String(format: "%02x", hashBytes[index])

            }

            return hex

        }

         

        Now the problem is that the code gives me a different hash than when I copy the image from my iPhone's photo album to my Mac and calculate the hash there. I also uploaded the photo directly from iPhone to an online hash generator which gave me same hash as Mac did.

         

        Therefore I would like to know if there is a way to calculate the correct hash directly from the data from the UIImagePickerController.

         

        It seems as if additional information (e.g. metadata) is added to the photo when it is saved to album, so that the hashes differ.

         

        Can anyone help me here?