我使用SHA1在Apple Watch上散列用户数据,并且在运行j = ( UInt32((msg[i]<<24) | (msg[i+1]<<16) | (msg[i+2]<<8) | msg[i+3]) )
函数时,出现以下错误:
Thread1:exc_breakpoint(code = exc_arm_breakpoint,subcode = 0xe7ffdefe)。 这条特定的行给了我错误:
class func SHA1Bytes(msg: [Int])->String{ func rotateLeft(number: UInt32, rotateBy: UInt32)->UInt32{ return ((number << rotateBy) | (number>>(32-rotateBy))) } func cvt_hex(value: UInt32)->String{ var str = "" for i:UInt32 in stride(from: 7, through: 0, by: -1){ let v: UInt32 = (value >> (i*4)&0x0f) str += String(v,radix: 16, uppercase: false) } return str } var W = [UInt32](repeating: 0, count: 80) var H0 = UInt32("67452301",radix: 16)! var H1 = UInt32("EFCDAB89",radix: 16)! var H2 = UInt32("98BADCFE",radix: 16)! var H3 = UInt32("10325476",radix: 16)! var H4 = UInt32("C3D2E1F0",radix: 16)! var wordArray = [UInt32]() for k in stride(from: 0, to: msg.count-3, by: 4) { let j = ( UInt32((msg[k]<<24) | (msg[k+1]<<16) | (msg[k+2]<<8) | msg[k+3]) ) wordArray.append(j) } ... return encoded.uppercased() }
这是从中提取上述行的代码段:
j
完全相同的代码在iOS Playground中运行完美,但在第一代Apple Watch上运行时崩溃。我已检查并且输入数组存在,我正在尝试访问它的现有元素,j=(UInt32) 2308511418, k=(Int)48, msg=([Int])56values
的结果不应该溢出。
代码失败并带有以下变量值:
[47] Int 217
[48] Int 137
[49] Int 153
[50] Int 22
[51] Int 186
[52] Int 163
[53] Int 41
[54] Int 208
[55] Int 104
这些是msg:
的值modulus
答案 0 :(得分:0)
第一代Apple Watch是一款32位设备,与64位设备相比,具有不同的溢出限制。
在32位器件上,Int
类型为32位,您可能会转入符号位。
在游乐场中尝试以下操作:
UInt32(Int64(153) << 24) // Equivalent to your code
UInt32(Int32(153) << 24) // Simulates a 32-bit device and crashes.
UInt32(153) << 24 // A possible solution
答案 1 :(得分:-1)
我设法弄清楚即使UInt32溢出,模拟器也没有崩溃,但是,在这种情况下,32位Apple Watch会崩溃。
解决方案是使用溢出运算符,该运算符仅用于加法,减法和乘法。因此,我将按位左移改变为乘法2 ^(要移位的位数)。
这是正确的解决方案:UInt32(msg[k])&*UInt32(1<<24)
let j = (UInt32(msg[k])&*UInt32(1<<24))|UInt32(msg[k+1]&*(1<<16))|UInt32(msg[k+2]&*(1<<8))|UInt32(msg[k+3])