public function testDecrypt() { $cipherText1 = $this->getFaker()->unique()->word; $plainText1 = $this->getFaker()->unique()->word; $this->cipherRegistry->get($this->cipher1Name)->shouldReceive('decipher')->once()->with($cipherText1, $this->keySource->get($this->key1Name))->andReturn($plainText1); $this->serializerDeserializer->shouldReceive('deserialize')->once()->with($cipherText1)->andReturn(new CipherText($cipherText1, $this->profileRegistry->get($this->profile1Name))); $this->assertEquals($plainText1, $this->encryptor->decrypt($cipherText1)); $this->cipherRegistry->get($this->cipher1Name)->shouldReceive('decipher')->once()->with($cipherText1, $this->keySource->get($this->key1Name))->andReturn($plainText1); $this->serializerDeserializer->shouldReceive('deserialize')->once()->with($cipherText1)->andReturn(new CipherText($cipherText1, $this->profileRegistry->get($this->profile1Name))); $this->assertEquals($plainText1, $this->encryptor->decrypt($cipherText1, $this->profile1Name)); $cipherText2 = $this->getFaker()->unique()->word; $plainText2 = $this->getFaker()->unique()->word; $this->cipherRegistry->get($this->cipher2Name)->shouldReceive('decipher')->once()->with($cipherText2, $this->keySource->get($this->key2Name))->andReturn($plainText2); $this->assertEquals($plainText2, $this->encryptor->decrypt(new CipherText($cipherText2, $this->profileRegistry->get($this->profile2Name)))); }
public function rotate(ObserverInterface $observer, Encryptor $encryptor, $newProfile = null) { $fields = $this->fields; $fields[] = $this->idField; $stmt = $this->connection->createQueryBuilder()->select($fields)->from($this->table)->execute(); while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { $id = $row[$this->idField]; $observer->rotating($id); unset($row[$this->idField]); foreach ($row as $key => $value) { $row[$key] = $encryptor->encrypt($encryptor->decrypt($value), $newProfile); } $this->connection->update($this->table, $row, array($this->idField => $id)); $observer->rotated($id); } }
public function rotate(ObserverInterface $observer, Encryptor $encryptor, $newProfile = null) { $fields = $this->fields; $fields[] = $this->idField; $stmt = $this->pdo->prepare(sprintf('SELECT %s FROM %s', implode(', ', $fields), $this->table)); $stmt->execute(); while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { $id = $row[$this->idField]; $observer->rotating($id); unset($row[$this->idField]); foreach ($row as $key => $value) { $row[$key] = $encryptor->encrypt($encryptor->decrypt($value), $newProfile); } $parameters = array(); $setFields = array_map(function ($field, $value) use(&$parameters) { $parameters[] = $value; return sprintf('%s = ?', $field); }, array_keys($row), $row); $parameters[] = $id; $this->pdo->prepare(sprintf('UPDATE %s SET %s WHERE %s = ?', $this->table, implode(',', $setFields), $this->idField))->execute($parameters); $observer->rotated($id); } }