Fastest bitwise xor between two multibyte binary data variables

后端 未结 7 1843
耶瑟儿~
耶瑟儿~ 2021-01-02 01:05

What is the fastest way to implementat the following logic:

def xor(data, key):
    l = len(key)

    buff = \"\"
    for i in range(0, len(data)):
        b         


        
7条回答
  •  醉话见心
    2021-01-02 01:25

    Following on my comment in the initial post, you can process large files rather quickly if you stick to numpy for key padding and bitwise XOR'ing, like so:

    import numpy as np
    
    # ...
    
    def xor(key, data):
    
        data = np.fromstring(data, dtype=np.byte)
        key = np.fromstring(key, dtype=np.byte)
    
        # Pad the key to match the data length
        key = np.pad(key, (0, len(data) - len(key)), 'wrap')
    
        return np.bitwise_xor(key, data)
    
    

提交回复
热议问题