为UIImage的SHA 256获取错误的值

时间:2018-11-06 06:22:53

标签: ios swift xcode

我正在使用以下代码来获取UIImage的SHA 256

(来源:https://stackoverflow.com/a/50931949/10451073

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
    }
}

我将其用作:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

        if let file = info[UIImagePickerControllerOriginalImage]
        {
            print((file as? UIImage)?.sha256())
        }
        self.dismiss(animated: true, completion: nil)


    }

但是我得到任何UIImage的SHA 256错误。例如,在检查此图像https://drive.google.com/open?id=1p9n01qOFahr6I1Q7FPzoDCKcjLjYVivl的SHA 256时,我得到的是1d6a7c377157c4511183706033898c76c090924ecbf9d47ddff7243237dc9243的值,而不是正确的值是360cd85c64b5e672c48ef948df689a17e41b80a84b9d3d2039143e47a9473395(从https://hash.online-convert.com/sha256-generator验证)

请帮助我了解这里的问题以及如何更改上面的代码以获取正确的SHA 256值。非常感谢您的帮助。谢谢。

编辑:在与@ Paulw11在下面的回答中讨论时,我得到了我需要获取文件的哈希值而不是图像的哈希值。但是我不确定如何更改此代码以获取相同的代码。请同样帮我。

1 个答案:

答案 0 :(得分:1)

UIImage不是PNG或JPEG文件;对于PNG文件和从该文件创建的UIImage,您将不会获得相同的哈希值。

您需要对文件进行哈希处理,而不是UIImage,如果要获取一个值,可以将其与同一文件的另一个哈希进行比较。

您可以使用字典的imageURL条目来获取图像的文件URL。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    if let file = info[UIImagePickerControllerImageURL] as? URL
    {

        try {
            let imageData = Data(contentsOf:file)
            let hash = imageData.sha256
        } catch {
            print(error)
        }
    }
    self.dismiss(animated: true, completion: nil)
}

extension Data {

    public var sha256:String {
        get {
            return hexStringFromData(input: digest(input: self as NSData))
        }
    }

    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
    }
}