SwiftDigest icon indicating copy to clipboard operation
SwiftDigest copied to clipboard

Workaround for stupid Xcode 9.2 hard errors?

Open ghost opened this issue 7 years ago • 5 comments

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...]

ghost avatar Feb 19 '18 20:02 ghost

@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?

crrobinson14 avatar Feb 23 '18 21:02 crrobinson14

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.

ghost avatar Feb 23 '18 22:02 ghost

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.

raymond-liao avatar Mar 20 '18 06:03 raymond-liao

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.

cyrille-legrand avatar Apr 25 '18 07:04 cyrille-legrand

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.

martinr448 avatar Apr 25 '18 14:04 martinr448