This commit is contained in:
Gnieark 2019-12-22 20:40:15 +01:00
parent bb0b3b50f3
commit 51bd2df8d0
9 changed files with 381 additions and 17 deletions

View File

@ -8,20 +8,64 @@ function showHideMenu()
doc.className = "hidden-by-default";
}
}
function getEncFieldsListStrings(){
return ['iv','mode','adata','cipher','salt'];
}
function getEncFieldsListIntegers(){
return ['v','iter','ks','ts'];
}
function crypt()
{
var key = document.getElementById("key").value;
var enc = sjcl.encrypt(key,document.getElementById("form_content").value);
document.getElementById("form_content").value = enc;
var enc = JSON.parse(sjcl.encrypt(key,document.getElementById("form_content").value));
var encFields = getEncFieldsListStrings();
encFields.forEach(element => {
document.getElementById("form_crypt_" + element).value = enc[element];
});
var encFields = getEncFieldsListIntegers();
encFields.forEach(element => {
document.getElementById("form_crypt_" + element).value = parseInt(enc[element]);
});
document.getElementById("form_content").value = enc["ct"];
}
function truncateCryptFields(){
getEncFieldsListIntegers().forEach( element=> {
document.getElementById("form_crypt_" + element).value = "";
});
getEncFieldsListStrings().forEach( element=> {
document.getElementById("form_crypt_" + element).value = "";
});
}
function uncrypt()
{
var key = document.getElementById("key").value;
if( document.getElementById("form_content") ){
var clearTxt = sjcl.decrypt(key,document.getElementById("form_content").value);
document.getElementById("form_content").value = clearTxt;
var encArr = new Object();
var encFields = getEncFieldsListStrings();
encFields.forEach(element => {
encArr[element] = document.getElementById("form_crypt_" + element).value;
});
var encFields = getEncFieldsListIntegers();
encFields.forEach(element => {
encArr[element] = parseInt(document.getElementById("form_crypt_" + element).value);
});
encArr["ct"] = document.getElementById("form_content").value;
try{
var clearTxt = sjcl.decrypt(key,JSON.stringify(encArr));
document.getElementById("form_content").value = clearTxt;
truncateCryptFields();
}
catch(error)
{
alert("Le pad n'a pas pu être déchiffré.");
}
}else{
var clearTxt = sjcl.decrypt(key,document.getElementById("content").innerHTML);
document.getElementById("content").innerHTML = clearTxt;

View File

@ -6,6 +6,7 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use App\Entity\Pad;
@ -18,8 +19,18 @@ Class PadController extends AbstractController
return $this->createFormBuilder()
->add('content', TextareaType::class)
->add('save', SubmitType::class, ['label' => 'Enregistrer'])
->add('crypt_iv', HiddenType::class,[])
->add('crypt_v', HiddenType::class,[])
->add('crypt_iter', HiddenType::class,[])
->add('crypt_ks', HiddenType::class,[])
->add('crypt_ts', HiddenType::class,[])
->add('crypt_mode', HiddenType::class,[])
->add('crypt_adata', HiddenType::class,[])
->add('crypt_cipher', HiddenType::class,[])
->add('crypt_salt', HiddenType::class,[])
->setAction($this->generateUrl('api_post_new'))
->getForm();
}
public function showForm(){
@ -45,15 +56,15 @@ Class PadController extends AbstractController
return $this->render('pad-view.html.twig', [
'head_title' => 'Pad id: ' . $pad->getName(),
'page_title' => 'Pad id: ' . $pad->getName(),
'pad_content' => $pad->getContent()
'head_title' => 'Pad id: ' . $pad->getName(),
'page_title' => 'Pad id: ' . $pad->getName(),
'pad_content' => $pad->getContent(),
'crypted' => (!empty($pad->getCryptCipher))
]);
}
private function get_free_name( $depth = 0, $length=6)
{
if($depth > 3 ){
throw new \UnexpectedValueException("I cant generate an unique key");
}
@ -83,15 +94,19 @@ Class PadController extends AbstractController
$entityManager = $this->getDoctrine()->getManager();
$pad = new PAD();
$pad->setContent($data["content"])
->setName( $this-> get_free_name() );
->setName( $this-> get_free_name() )
->setCryptIv( $data["crypt_iv"] )
->setCryptMode( $data["crypt_mode"] )
->setCryptAdata( $data["crypt_adata"])
->setCryptCipher( $data["crypt_cipher"] )
->setCryptSalt( $data["crypt_salt"] )
->setCryptV( $data["crypt_v"] )
->setCryptIter( $data["crypt_iter"] )
->setCryptKs( $data["crypt_ks"] )
->setCryptTs( $data["crypt_ts"] );
$entityManager->persist($pad);
// actually executes the queries (i.e. the INSERT query)
$entityManager->flush();
return $this->redirectToRoute('view',["name" => $pad->getName() ]);
}
}
}

View File

@ -46,6 +46,51 @@ class Pad
*/
private $name;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $crypt_iv;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $crypt_v;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $crypt_iter;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $crypt_ks;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $crypt_ts;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $crypt_mode;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $crypt_cipher;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $crypt_salt;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $crypt_adata;
public function __construct()
{
@ -128,4 +173,112 @@ class Pad
return $this;
}
public function getCryptIv(): ?string
{
return $this->crypt_iv;
}
public function setCryptIv(?string $crypt_iv): self
{
$this->crypt_iv = $crypt_iv;
return $this;
}
public function getCryptV(): ?int
{
return $this->crypt_v;
}
public function setCryptV(?int $crypt_v): self
{
$this->crypt_v = $crypt_v;
return $this;
}
public function getCryptIter(): ?int
{
return $this->crypt_iter;
}
public function setCryptIter(?int $crypt_iter): self
{
$this->crypt_iter = $crypt_iter;
return $this;
}
public function getCryptKs(): ?int
{
return $this->crypt_ks;
}
public function setCryptKs(?int $crypt_ks): self
{
$this->crypt_ks = $crypt_ks;
return $this;
}
public function getCryptTs(): ?int
{
return $this->crypt_ts;
}
public function setCryptTs(?int $crypt_ts): self
{
$this->crypt_ts = $crypt_ts;
return $this;
}
public function getCryptMode(): ?string
{
return $this->crypt_mode;
}
public function setCryptMode(?string $crypt_mode): self
{
$this->crypt_mode = $crypt_mode;
return $this;
}
public function getCryptCipher(): ?string
{
return $this->crypt_cipher;
}
public function setCryptCipher(?string $crypt_cipher): self
{
$this->crypt_cipher = $crypt_cipher;
return $this;
}
public function getCryptSalt(): ?string
{
return $this->crypt_salt;
}
public function setCryptSalt(?string $crypt_salt): self
{
$this->crypt_salt = $crypt_salt;
return $this;
}
public function getCryptAdata(): ?string
{
return $this->crypt_adata;
}
public function setCryptAdata(?string $crypt_adata): self
{
$this->crypt_adata = $crypt_adata;
return $this;
}
}

View File

@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20191219161539 extends AbstractMigration
{
public function getDescription() : string
{
return '';
}
public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('ALTER TABLE pad ADD crypt_iv VARCHAR(255) DEFAULT NULL, ADD crypt_v INT DEFAULT NULL, ADD crypt_iter INT DEFAULT NULL, ADD crypt_ks INT DEFAULT NULL, ADD crypt_ts INT DEFAULT NULL, ADD crypt_mode VARCHAR(255) DEFAULT NULL, ADD crypt_chiper VARCHAR(255) DEFAULT NULL, ADD crypt_salt VARCHAR(255) DEFAULT NULL, ADD crypt_ct VARCHAR(255) DEFAULT NULL');
}
public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('ALTER TABLE pad DROP crypt_iv, DROP crypt_v, DROP crypt_iter, DROP crypt_ks, DROP crypt_ts, DROP crypt_mode, DROP crypt_chiper, DROP crypt_salt, DROP crypt_ct');
}
}

View File

@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20191219162559 extends AbstractMigration
{
public function getDescription() : string
{
return '';
}
public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('ALTER TABLE pad ADD adata VARCHAR(255) DEFAULT NULL');
}
public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('ALTER TABLE pad DROP adata');
}
}

View File

@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20191220210806 extends AbstractMigration
{
public function getDescription() : string
{
return '';
}
public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('ALTER TABLE pad ADD crypt_adata VARCHAR(255) DEFAULT NULL, DROP crypt_ct, DROP adata');
}
public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('ALTER TABLE pad ADD adata VARCHAR(255) CHARACTER SET utf8mb4 DEFAULT NULL COLLATE `utf8mb4_unicode_ci`, CHANGE crypt_adata crypt_ct VARCHAR(255) CHARACTER SET utf8mb4 DEFAULT NULL COLLATE `utf8mb4_unicode_ci`');
}
}

View File

@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20191220211632 extends AbstractMigration
{
public function getDescription() : string
{
return '';
}
public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('ALTER TABLE pad CHANGE crypt_chiper crypt_cipher VARCHAR(255) DEFAULT NULL');
}
public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('ALTER TABLE pad CHANGE crypt_cipher crypt_chiper VARCHAR(255) CHARACTER SET utf8mb4 DEFAULT NULL COLLATE `utf8mb4_unicode_ci`');
}
}

View File

@ -7,17 +7,20 @@
{% endblock %}
<script src="{{ asset('js/main.js') }}"></script>
<script src="{{ asset('js/sjcl.js') }}"></script>
</head>
<body onload="ready()">
<h1>{{head_title|e('html')}} </h1>
<nav>
{% if crypted == true %}
<img alt="menu" title="plus d'options" src="img/menus.svg" id="showOptions" class="link"/>
<em id="moreoptions" class="hidden-by-default">
Déchiffrer le pad:
<input type="text" id="key" placeholder="Clef de chiffrement"/>
<input type="button" id="buttonUncrypt" value="déchiffrer"/>
</em>
</nav>
{% endif %}
</nav>
<pre id="content">{{ pad_content|e('html') }}</pre>
<p><a href="/">Créer un nouveau Pad</a></p>
</body>

View File

@ -21,6 +21,15 @@
</em>
</nav>
{{ form_start(form) }}
{{ form_widget(form.crypt_iv, {}) }}
{{ form_widget(form.crypt_v, {}) }}
{{ form_widget(form.crypt_iter, {}) }}
{{ form_widget(form.crypt_ks, {}) }}
{{ form_widget(form.crypt_ts, {}) }}
{{ form_widget(form.crypt_mode, {}) }}
{{ form_widget(form.crypt_adata, {}) }}
{{ form_widget(form.crypt_cipher, {"value": "none"}) }}
{{ form_widget(form.crypt_salt, {}) }}
{{ form_widget(form.content, {}) }}
{{ form_end(form) }}