Swift plist校验和

Swift plist checksum

我想计算并验证 iOS 中 属性 列表的一些校验和。

plist 的内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>foo</key>
    <string>bar</string>
    <key>foornum</key>
    <integer>42</integer>
</dict>
</plist>

为了计算哈希值(使用 OpenSSL),我做了以下操作:

# Convert the plist to binary format
plutil -convert binary1 Test.plist

openssl dgst -sha256 Test.plist    
# SHA256(Test.plist)= 187592fed5ad62620016c5d0b9f08bc58c93c8fdcc9fc7464aea8fdfdf75a48c

# Or by using the internal shasum method
shasum -a 256 Test.plist
# 187592fed5ad62620016c5d0b9f08bc58c93c8fdcc9fc7464aea8fdfdf75a48c  Test.plist

# Transform to base64 so it can be loaded easily into a Swift Data object
echo -n 187592fed5ad62620016c5d0b9f08bc58c93c8fdcc9fc7464aea8fdfdf75a48c | xxd -r -p | base64
# GHWS/tWtYmIAFsXQufCLxYyTyP3Mn8dGSuqP3991pIw=

然后在 Swift 中我执行以下操作:

import CommonCrypto

let testHash = Data(base64Encoded: "GHWS/tWtYmIAFsXQufCLxYyTyP3Mn8dGSuqP3991pIw=")!
print(testHash as NSData)
let fileURL = Bundle.main.url(forResource: "Test", withExtension: "plist")!

var fileData = try! Data(contentsOf: fileURL)

var digest = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
fileData.withUnsafeBytes {
    _ = CC_SHA256([=13=].baseAddress, CC_LONG(fileData.count), &digest)
}
print(Data(digest) as NSData)

这给了我控制台的以下输出:

<187592fe d5ad6262 0016c5d0 b9f08bc5 8c93c8fd cc9fc746 4aea8fdf df75a48c>
<24a5245b 317796a5 59cdee0b 86912974 28cc2491 20f6923c 87f59ba5 4e799345>

为什么哈希不同,我该如何解决它才能正确计算和验证哈希?

虽然我不完全确定是什么导致了这个问题,但我确实设法通过使用 python library.

解决了它
import plistlib
import hashlib

def calc_hash(file_name):
    try:
        f = open(file_name, 'r')
        f_data = f.read()
        f.close()
        file = plistlib.loads(f_data.encode())
    except (ValueError, Exception) as e:
        print("Failed to load file {}".format(file_name))
        return
    dump = plistlib.dumps(file, fmt=plistlib.FMT_BINARY)

    hasher = hashlib.sha256()
    hasher.update(dump)
    digest = hasher.digest()

    print("{} {}".format(file_name, digest.hex()))

file_name = str(input('Please enter the relative path to the plist (i.e. path/to/plist.plist):\n'))
calc_hash(file_name)