]> source.dussan.org Git - nextcloud-server.git/commitdiff
bugfixes for encryption library and test cases
authorRobin Appelman <icewind@owncloud.com>
Tue, 17 Apr 2012 18:56:53 +0000 (20:56 +0200)
committerRobin Appelman <icewind@owncloud.com>
Wed, 18 Apr 2012 18:54:28 +0000 (20:54 +0200)
apps/files_encryption/lib/crypt.php
apps/files_encryption/tests/encryption.php [new file with mode: 0644]

index 246d4f672db1abdbd41d2d89ca3c40a11c2dc182..8cf9451c63c315f6e08737c79d07e6d327aa91ae 100644 (file)
@@ -119,7 +119,7 @@ class OC_Crypt {
         */
        public static function encrypt( $content, $key='') {
                $bf = self::getBlowfish($key);
-               return($bf->encrypt($content));
+               return $bf->encrypt($content);
        }
 
        /**
@@ -132,61 +132,62 @@ class OC_Crypt {
        */
        public static function decrypt( $content, $key='') {
                $bf = self::getBlowfish($key);
-               return($bf->decrypt($content));
+               $data=$bf->decrypt($content);
+               return rtrim($data, "\0");
        }
 
        /**
        * @brief encryption of a file
-       * @param $filename
-       * @param $key the encryption key
+       * @param string $source
+       * @param string $target
+       * @param string $key the decryption key
        *
        * This function encrypts a file
        */
-       public static function encryptfile( $filename, $key) {
-               $handleread  = fopen($filename, "rb");
-               if($handleread<>FALSE) {
-                       $handlewrite = fopen($filename.OC_Crypt::$encription_extension, "wb");
+       public static function encryptFile( $source, $target, $key='') {
+               $handleread  = fopen($source, "rb");
+               if($handleread!=FALSE) {
+                       $handlewrite = fopen($target, "wb");
                        while (!feof($handleread)) {
                                $content = fread($handleread, 8192);
                                $enccontent=OC_CRYPT::encrypt( $content, $key);
                                fwrite($handlewrite, $enccontent);
                        }
                        fclose($handlewrite);
-                       unlink($filename);
+                       fclose($handleread);
                }
-               fclose($handleread);
        }
 
 
-        /**
-         * @brief decryption of a file
-         * @param $filename
-         * @param $key the decryption key
-         *
-         * This function decrypts a file
-         */
-       public static function decryptfile( $filename, $key) {
-               $handleread  = fopen($filename.OC_Crypt::$encription_extension, "rb");
-               if($handleread<>FALSE) {
-                       $handlewrite = fopen($filename, "wb");
+       /**
+               * @brief decryption of a file
+               * @param string $source
+               * @param string $target
+               * @param string $key the decryption key
+               *
+               * This function decrypts a file
+               */
+       public static function decryptFile( $source, $target, $key='') {
+               $handleread  = fopen($source, "rb");
+               if($handleread!=FALSE) {
+                       $handlewrite = fopen($target, "wb");
                        while (!feof($handleread)) {
                                $content = fread($handleread, 8192);
                                $enccontent=OC_CRYPT::decrypt( $content, $key);
                                fwrite($handlewrite, $enccontent);
                        }
                        fclose($handlewrite);
-                       unlink($filename.OC_Crypt::$encription_extension);
+                       fclose($handleread);
                }
-               fclose($handleread);
        }
        
        /**
         * encrypt data in 8192b sized blocks
         */
-       public static function blockEncrypt($data){
+       public static function blockEncrypt($data, $key=''){
                $result='';
                while(strlen($data)){
-                       $result=self::encrypt(substr($data,0,8192));
+                       $result.=self::encrypt(substr($data,0,8192),$key);
                        $data=substr($data,8192);
                }
                return $result;
@@ -195,10 +196,10 @@ class OC_Crypt {
        /**
         * decrypt data in 8192b sized blocks
         */
-       public static function blockDecrypt($data){
+       public static function blockDecrypt($data, $key=''){
                $result='';
                while(strlen($data)){
-                       $result=self::decrypt(substr($data,0,8192));
+                       $result.=self::decrypt(substr($data,0,8192),$key);
                        $data=substr($data,8192);
                }
                return $result;
diff --git a/apps/files_encryption/tests/encryption.php b/apps/files_encryption/tests/encryption.php
new file mode 100644 (file)
index 0000000..140fe6b
--- /dev/null
@@ -0,0 +1,36 @@
+<?php
+/**
+ * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+class Test_Encryption extends UnitTestCase {
+       function testEncryption(){
+               $key=uniqid();
+               $file=OC::$SERVERROOT.'/3rdparty/MDB2.php';
+               $source=file_get_contents($file); //nice large text file
+               $encrypted=OC_Crypt::encrypt($source,$key);
+               $decrypted=OC_Crypt::decrypt($encrypted,$key);
+               $this->assertNotEqual($encrypted,$source);
+               $this->assertEqual($decrypted,$source);
+               
+               $encrypted=OC_Crypt::blockEncrypt($source,$key);
+               $decrypted=OC_Crypt::blockDecrypt($encrypted,$key);
+               $this->assertNotEqual($encrypted,$source);
+               $this->assertEqual($decrypted,$source);
+
+               $tmpFileEncrypted=OC_Helper::tmpFile();
+               OC_Crypt::encryptfile($file,$tmpFileEncrypted,$key);
+               $encrypted=file_get_contents($tmpFileEncrypted);
+               $decrypted=OC_Crypt::blockDecrypt($encrypted,$key);
+               $this->assertNotEqual($encrypted,$source);
+               $this->assertEqual($decrypted,$source);
+
+               $tmpFileDecrypted=OC_Helper::tmpFile();
+               OC_Crypt::decryptfile($tmpFileEncrypted,$tmpFileDecrypted,$key);
+               $decrypted=file_get_contents($tmpFileDecrypted);
+               $this->assertEqual($decrypted,$source);
+       }
+}