SwiftDigest
SwiftDigest copied to clipboard
Workaround for stupid Xcode 9.2 hard errors?
In MD5Digest, Xcode 9.2 is flagging these two lines as exceeding the size of a UInt32, which is of course totally wrong:
var b = UInt32(0xefcdab89)
var c = UInt32(0x98badcfe)
Well, I found a workaround (untested at this point):
var b = UInt32(0x70000000) + UInt32(0x7fcdab89) // 0xefcdab89
var c = UInt32(0x40000000) + UInt32(0x58badcfe) // 0x98badcfe
Geez ... [Post is really to help out any other poor soul running into this issue...]
@dhoerlSA Thanks for posting this. I've tested it and confirmed it works here for the use-cases I'm using this for. Maybe change this to a PR?
Sure. Will do my best to get it done tomorrow (Sat).
On Feb 23, 2018, at 4:58 PM, Chad Robinson [email protected] wrote:
@dhoerlSA Thanks for posting this. I've tested it and confirmed it works here for the use-cases I'm using this for. Maybe change this to a PR?
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.
hi man, you just need change
var a = UInt32(0x67452301)
var b = UInt32(0xefcdab89)
var c = UInt32(0x98badcfe)
var d = UInt32(0x10325476)
to
var a: UInt32 = 0x67452301
var b: UInt32 = 0xefcdab89
var c: UInt32 = 0x98badcfe
var d: UInt32 = 0x10325476
all errors should go away.
This workaround works for 64-bit devices, but on 32-bit devices half of the resulting MD5 is filled with zeroes.
On an iPhone X, md5("Hello, world") = bc6e6f16b8a077ef5fbc8d59d0b931b9 ✅ On an iPhone 5, md5("Hello, world") = 00000000b8a077ef00000000bc6e6f16 ❌
Using Xcode 9.4 beta (9Q1004a), iOS 11.4 SDK — same results on simulator or on device.
The problem is in
public var description: String {
return String(format: "%016lx%016lx",
_digest.0.byteSwapped,
_digest.1.byteSwapped)
}
because on a 32-bit platform long int
is a 32-bit integer. The solution is to use llx
formats:
public var description: String {
return String(format: "%016llx%016llx",
_digest.0.byteSwapped,
_digest.1.byteSwapped)
}
That should work correctly on 32-bit and 64-bit.