Swift 中的校验和和 XOR

Checksum and XOR in Swift

我在 Objective-C 中写了这些方法。它们只是校验和和 XOR 一些 NSData

- (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;
}

但是,我不熟悉按位运算符,尤其是在 Swift 中,这些 UnsafeMutablePointer<Void>... 东西。

有人可以帮我转换吗? (基本上,我需要校验和和 XOR 函数)
还有一件事,它们应该放在 NSData/NSMutableData 扩展中吗?

谢谢。

UnsafeBufferPointer/UnsafeMutableBufferPointer 可能就是您现在需要的。我已尝试将您的代码翻译成下面的 Swift。 (但是代码没有测试好。)

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
}

已更新 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
}

Swift 3 次更新:

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([=10=]) }.reduce(0, +) & 0xff
    }
}

您还可以创建另一个函数:xored(key: Data) -> Data
然后您可以链接这些运算符:xored(key).checksum()

Swift支持运算符重载,让你轻松做到let xorData = data1 ^ data2。我已经为 xor 写了一个非相似大小数据的扩展。

extension Data {
    static func ^ (left: Data, right: Data) -> Data {
        if left.count != right.count {
            NSLog("Warning! XOR operands are not equal. left = \(left), right = \(right)")
        }

        var result: Data = Data()
        var smaller: Data, bigger: Data
        if left.count <= right.count {
            smaller = left
            bigger = right
        } else {
            smaller = right
            bigger = left
        }

        let bs:[UInt8] = Array(smaller)
        let bb:[UInt8] = Array (bigger)
        var br = [UInt8] ()
        for i in 0..<bs.count {
            br.append(bs[i] ^ bb[i])
        }
        for j in bs.count..<bb.count {
            br.append(bb[j])
        }
        result = Data(br)
        return result
    }
}