def set_key(self, key): if __debug__: # Insecure. assert len(key) >= self.digest_size if len(key) > self.block_size: # Key is too big. Hash it and use the result as the key. # This really isn't necessary because we're certain that the key # is going to be the correct size. However, I put it in here # for completeness with the HMAC spec. import sys sys.stderr.write('WARNING: Unexecpted HMAC key size!!!\n') h = self.get_hash_object() self.key = h.update(key).digest() else: self.key = key ipad = '\x36' * self.block_size opad = '\x5C' * self.block_size padded_key = self.key + '\0' * (self.block_size - len(self.key)) self._enc_ipad = str_xor(padded_key, ipad) self._enc_opad = str_xor(padded_key, opad)
def des_ede3_cbc_decrypt(data, iv, key): assert (len(data) % 8 == 0), 'Data block must be a multiple of 8: %i' % len(data) key1 = DES.new(key[0:8], DES.MODE_ECB) key2 = DES.new(key[8:16], DES.MODE_ECB) key3 = DES.new(key[16:24], DES.MODE_ECB) # Outer-CBC Mode # 8-byte blocks result = [] prev = iv for i in xrange(0, len(data), 8): block = data[i:i + 8] value = key1.decrypt( key2.encrypt( key3.decrypt(block))) result.append(str_xor(prev, value)) prev = block return ''.join(result)