public function recrypt($table, $fields, $where = '', $pks = ['id'], CryptoTool $source = null)
{
if ($source === null) {
$source = $this->source;
}
$this->console->out("Reencrypting table '{$table}' fields:\n\t" . implode("\n\t", $fields));
$names = '`' . implode('`, `', array_merge($pks, $fields)) . '`';
$data = $this->db->Execute("SELECT {$names} FROM `{$table}` {$where} FOR UPDATE;");
$params = '`' . implode('` = ?, `', $fields) . '` = ?';
$where = '`' . implode('` = ? AND `', $pks) . '` = ?';
$stmt = $this->db->Prepare("UPDATE `{$table}` SET {$params} WHERE {$where};");
$affected = 0;
foreach ($data as $entry) {
$in = [];
foreach ($fields as $field) {
$in[] = $this->target->encrypt($source->_decrypt($entry[$field]));
}
foreach ($pks as $pk) {
$in[] = $entry[$pk];
}
$this->db->Execute($stmt, $in);
$affected += $this->db->Affected_Rows();
}
$this->console->out("Updated {$affected} rows!\n");
return $affected;
}