def _serialize_data(root, header, password): groups, entries = root.get_groups_and_entries() # Update header header.num_groups = len(groups) header.num_entries = len(entries) header.final_random_seed = crypto.randomize(16) header.encryption_iv = crypto.randomize(16) # Generate body body = str() for g in groups: body += g.to_bytearray() for e in entries: body += e.to_bytearray() # Calculate hash from the body header.contents_hash = crypto.sha256(body) # Encrypt body encrypted = crypto.encrypt(body, Database._generate_key(header, password), header.encryption_iv) # Generate file content data = str() data += header.to_bytearray() data += encrypted return data
def serialize(self, password): if not self._header: self._header = Header() self._header.transf_random_seed = crypto.randomize(32) self._header.key_transf_rounds = 50000 return self._serialize_data(self._root, self._header, password)