Checksum and XOR in Swift

微笑、不失礼 提交于 2020-05-14 21:34:38

问题


I worte these methods in Objective-C. They're just checksum and XOR some 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;
}

However, I'm not familiar with Bitwise operators, especially in Swift, with these UnsafeMutablePointer<Void>... things.

Can anybody help me converting this ? (Basically, I need checksum and XOR functions)
One more things, should they be put in NSData/NSMutableData extension ?

Thank you.


回答1:


UnsafeBufferPointer/UnsafeMutableBufferPointer might be what you need now. I've tried translating your code into Swift below. (But the code is not tested well.)

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
}



回答2:


Swift 3 update:

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

You can also create another function: xored(key: Data) -> Data.
Then you can chain these operators: xored(key).checksum()




回答3:


Swift support operator overloading, so you can easily do let xorData = data1 ^ data2. I have written an extension for non-similar size data to 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
    }
}



回答4:


Updated for 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
}


来源:https://stackoverflow.com/questions/29382907/checksum-and-xor-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!