Overview

Namespaces

  • None
  • PHP
  • Pry
    • Auth
      • Interfaces
    • Config
    • Controller
    • Date
    • Db
    • Feed
      • Abstracts
      • Writers
    • File
      • Decorator
    • Form
      • Element
    • Image
    • Log
      • Writer
    • Net
      • Exception
    • Session
    • Util
    • Validate
      • Validator
    • View

Classes

  • Crypt
  • FileCSV
  • FileManager
  • FolderManager
  • Upload
  • Util
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * Pry Framework
  5:  *
  6:  * LICENSE
  7:  *
  8:  * This source file is subject to the new BSD license that is bundled
  9:  * with this package in the file LICENSE.txt.
 10:  * 
 11:  */
 12: 
 13: namespace Pry\File;
 14: 
 15: use Pry\File\FileManager;
 16: 
 17: /**
 18:  * Cryptage / Décryptage de fichier
 19:  * @category Pry
 20:  * @package File
 21:  * @version 0.9
 22:  * @author Olivier ROGER <oroger.fr>
 23:  */
 24: class Crypt
 25: {
 26: 
 27:     /**
 28:      * Clé de chiffrement
 29:      * @var string $key
 30:      */
 31:     private $key = null;
 32: 
 33:     /**
 34:      * Type de chiffrement
 35:      * @see Doc Mcrypt pour la liste
 36:      * @var string $cipher
 37:      */
 38:     private $cipher;
 39: 
 40:     /**
 41:      * Mode de chiffrement
 42:      * @see Doc mcrypt pour liste
 43:      * @var string $mode
 44:      */
 45:     private $mode;
 46: 
 47:     /**
 48:      * Vecteur d'initialisation
 49:      * @var <type> $iv
 50:      */
 51:     private $iv;
 52: 
 53:     /**
 54:      * Constructeur
 55:      * @param string $key Clé de chiffrage
 56:      * @param string $cipher Type de chiffrment (défaut 3DES)
 57:      * @param string $mode Mode de chiffrement (défaut nofb)
 58:      */
 59:     public function __construct($key, $cipher = MCRYPT_3DES, $mode = MCRYPT_MODE_NOFB)
 60:     {
 61:         if (!extension_loaded('mcrypt'))
 62:             throw new \Exception('Extension mcrypt nécessaire');
 63: 
 64:         $this->cipher = $cipher;
 65:         $this->mode   = $mode;
 66:         $key_size     = mcrypt_module_get_algo_key_size($this->cipher);
 67:         $this->key    = substr($this->key, 0, $key_size);
 68:         $iv_size      = mcrypt_get_iv_size($this->cipher, $this->mode);
 69:         $this->iv     = mcrypt_create_iv($iv_size, MCRYPT_RAND);
 70:     }
 71: 
 72:     /**
 73:      * Crypte un fichier
 74:      * @param File_FileManager $filein Fichier à crypter
 75:      * @param File_FileManager $fileout Fichier crypté
 76:      * @param boolean $deletein Supprime ou non le fichier non crypté à la fin du processus.
 77:      */
 78:     public function crypt(FileManager $filein, FileManager $fileout, $deletein = true)
 79:     {
 80:         $data2Crypt = $this->readFile($filein);
 81:         if ($deletein)
 82:             $filein->delete();
 83:         unset($filein);
 84: 
 85:         $dataCrypted = mcrypt_encrypt($this->cipher, $this->key, $data2Crypt, $this->mode, $this->iv);
 86:         $this->writeFile($fileout, $dataCrypted);
 87:     }
 88: 
 89:     /**
 90:      * Décrypte un fichier
 91:      * @param File_FileManager $filein Fichier à décrypter
 92:      * @param File_FileManager $fileout Fichier en clair
 93:      * @param boolean $deletein Supprime ou non le fichier crypté à la fin du processus
 94:      */
 95:     public function decrypt(FileManager $filein, FileManager $fileout, $deletein = false)
 96:     {
 97:         $cryptedData = $this->readFile($filein);
 98:         if ($deletein)
 99:             $filein->delete();
100:         unset($filein);
101: 
102:         $uncryptedData = mcrypt_decrypt($this->cipher, $this->key, $cryptedData, $this->mode, $this->iv);
103:         $this->writeFile($fileout, $uncryptedData);
104:     }
105: 
106:     /**
107:      * Lit le contenu du fichier
108:      * @param File_FileManager $file
109:      * @return string
110:      */
111:     private function readFile(FileManager $file)
112:     {
113:         $file->open('rb');
114:         return $file->read();
115:     }
116: 
117:     /**
118:      * Ecrit le fichier
119:      * @param File_FileManager $file
120:      * @param string $data
121:      */
122:     private function writeFile(FileManager $file, $data)
123:     {
124:         $file->open('wb+');
125:         $file->write($data);
126:         $file->close();
127:     }
128: 
129: }
Pry API documentation generated by ApiGen 2.8.0