2015-04-01 24 views
5

I worte questi metodi in Objective-C. Sono solo checksum e XOR alcuni NSDataChecksum e XOR in Swift

- (void)XOR:(NSMutableData *)inputData withKey:(NSData *)key 
{ 
    unsigned char* inputByteData = (unsigned char*)[inputData mutableBytes]; 
    unsigned char* keyByteData = (unsigned char*)[key bytes]; 
    for (int i = 0; i < [inputData length]; i++) 
    { 
     inputByteData[i] = inputByteData[i]^keyByteData[i % [key length]]; 
    } 
} 

- (Byte)checkSum:(NSMutableData *)data withLength:(Byte)dataLength 
{ 
    Byte * dataByte = (Byte *)malloc(dataLength); 
    memcpy(dataByte, [data bytes], dataLength); 

    Byte result = 0; 
    int count = 0; 
    while (dataLength>0) { 
     result += dataByte[count]; 
     dataLength--; 
     count++; 
    }; 
    result = result&0xff; 
    return result&0xff; 
} 

Tuttavia, io non sono a conoscenza operatori bit per bit, in particolare a Swift, con questi UnsafeMutablePointer<Void> ... cose.

Qualcuno può aiutarmi a convertire questo? (Fondamentalmente, ho bisogno di checksum e funzioni XOR)
Un'altra cosa, dovrebbero essere inserite nell'estensione NSData/NSMutableData?

Grazie.

risposta

0

Swift 3 aggiornamento:

public extension Data { 

    public mutating func xor(key: Data) { 
     for i in 0..<self.count { 
      self[i] ^= key[i % key.count] 
     } 
    } 


    public func checkSum() -> Int { 
     return self.map { Int($0) }.reduce(0, +) & 0xff 
    } 
} 

È inoltre possibile creare un'altra funzione: xored(key: Data) -> Data.
Quindi è possibile concatenare questi operatori: xored(key).checksum()

8

UnsafeBufferPointer/UnsafeMutableBufferPointer potrebbe essere quello che ti serve adesso. Ho provato a tradurre il tuo codice in Swift di seguito. (Ma il codice non è testato bene.)

func XOR(inputData: NSMutableData, withKey key: NSData) { 
    let b = UnsafeMutableBufferPointer<UInt8>(start: 
     UnsafeMutablePointer(inputData.mutableBytes), count: inputData.length) 

    let k = UnsafeBufferPointer<UInt8>(start: 
     UnsafePointer(key.bytes), count: key.length) 

    for i in 0..<inputData.length { 
     b[i] ^= k[i % key.length] 
    } 
} 

func checkSum(data: NSData) -> Int { 
    let b = UnsafeBufferPointer<UInt8>(start: 
     UnsafePointer(data.bytes), count: data.length) 

    var sum = 0 
    for i in 0..<data.length { 
     sum += Int(b[i]) 
    } 
    return sum & 0xff 
} 
+1

Non è necessario utilizzare 'unsafeBitCast'. Ad esempio: let k = UnsafeBufferPointer (inizio: UnsafePointer (key.bytes), count: key.length) '. –

+0

@MartinR Grazie per la buona informazione. Il mio codice è migliorato. – findall

+1

Grazie. Lo verificherò e contrassegnerò la tua risposta come accettata più tardi;) –

0

aggiornato per Swift 3:

func xor(data: Data, with key: Data) -> Data { 
    var xorData = data 

    xorData.withUnsafeMutableBytes { (start: UnsafeMutablePointer<UInt8>) -> Void in 
     key.withUnsafeBytes { (keyStart: UnsafePointer<UInt8>) -> Void in 
      let b = UnsafeMutableBufferPointer<UInt8>(start: start, count: xorData.count) 

      let k = UnsafeBufferPointer<UInt8>(start: keyStart, count: data.count) 
      let length = data.count 

      for i in 0..<xorData.count { 
       b[i] ^= k[i % length] 
      } 
     } 
    } 

    return xorData 
} 
+0

controlla la mia risposta: D –