bugfixes for encryption library and test cases
This commit is contained in:
parent
57b8ff890c
commit
26e9a0dd13
|
@ -119,7 +119,7 @@ class OC_Crypt {
|
||||||
*/
|
*/
|
||||||
public static function encrypt( $content, $key='') {
|
public static function encrypt( $content, $key='') {
|
||||||
$bf = self::getBlowfish($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='') {
|
public static function decrypt( $content, $key='') {
|
||||||
$bf = self::getBlowfish($key);
|
$bf = self::getBlowfish($key);
|
||||||
return($bf->decrypt($content));
|
$data=$bf->decrypt($content);
|
||||||
|
return rtrim($data, "\0");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief encryption of a file
|
* @brief encryption of a file
|
||||||
* @param $filename
|
* @param string $source
|
||||||
* @param $key the encryption key
|
* @param string $target
|
||||||
|
* @param string $key the decryption key
|
||||||
*
|
*
|
||||||
* This function encrypts a file
|
* This function encrypts a file
|
||||||
*/
|
*/
|
||||||
public static function encryptfile( $filename, $key) {
|
public static function encryptFile( $source, $target, $key='') {
|
||||||
$handleread = fopen($filename, "rb");
|
$handleread = fopen($source, "rb");
|
||||||
if($handleread<>FALSE) {
|
if($handleread!=FALSE) {
|
||||||
$handlewrite = fopen($filename.OC_Crypt::$encription_extension, "wb");
|
$handlewrite = fopen($target, "wb");
|
||||||
while (!feof($handleread)) {
|
while (!feof($handleread)) {
|
||||||
$content = fread($handleread, 8192);
|
$content = fread($handleread, 8192);
|
||||||
$enccontent=OC_CRYPT::encrypt( $content, $key);
|
$enccontent=OC_CRYPT::encrypt( $content, $key);
|
||||||
fwrite($handlewrite, $enccontent);
|
fwrite($handlewrite, $enccontent);
|
||||||
}
|
}
|
||||||
fclose($handlewrite);
|
fclose($handlewrite);
|
||||||
unlink($filename);
|
fclose($handleread);
|
||||||
}
|
}
|
||||||
fclose($handleread);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief decryption of a file
|
* @brief decryption of a file
|
||||||
* @param $filename
|
* @param string $source
|
||||||
* @param $key the decryption key
|
* @param string $target
|
||||||
*
|
* @param string $key the decryption key
|
||||||
* This function decrypts a file
|
*
|
||||||
*/
|
* This function decrypts a file
|
||||||
public static function decryptfile( $filename, $key) {
|
*/
|
||||||
$handleread = fopen($filename.OC_Crypt::$encription_extension, "rb");
|
public static function decryptFile( $source, $target, $key='') {
|
||||||
if($handleread<>FALSE) {
|
$handleread = fopen($source, "rb");
|
||||||
$handlewrite = fopen($filename, "wb");
|
if($handleread!=FALSE) {
|
||||||
|
$handlewrite = fopen($target, "wb");
|
||||||
while (!feof($handleread)) {
|
while (!feof($handleread)) {
|
||||||
$content = fread($handleread, 8192);
|
$content = fread($handleread, 8192);
|
||||||
$enccontent=OC_CRYPT::decrypt( $content, $key);
|
$enccontent=OC_CRYPT::decrypt( $content, $key);
|
||||||
fwrite($handlewrite, $enccontent);
|
fwrite($handlewrite, $enccontent);
|
||||||
}
|
}
|
||||||
fclose($handlewrite);
|
fclose($handlewrite);
|
||||||
unlink($filename.OC_Crypt::$encription_extension);
|
fclose($handleread);
|
||||||
}
|
}
|
||||||
fclose($handleread);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* encrypt data in 8192b sized blocks
|
* encrypt data in 8192b sized blocks
|
||||||
*/
|
*/
|
||||||
public static function blockEncrypt($data){
|
public static function blockEncrypt($data, $key=''){
|
||||||
$result='';
|
$result='';
|
||||||
while(strlen($data)){
|
while(strlen($data)){
|
||||||
$result=self::encrypt(substr($data,0,8192));
|
$result.=self::encrypt(substr($data,0,8192),$key);
|
||||||
$data=substr($data,8192);
|
$data=substr($data,8192);
|
||||||
}
|
}
|
||||||
return $result;
|
return $result;
|
||||||
|
@ -195,10 +196,10 @@ class OC_Crypt {
|
||||||
/**
|
/**
|
||||||
* decrypt data in 8192b sized blocks
|
* decrypt data in 8192b sized blocks
|
||||||
*/
|
*/
|
||||||
public static function blockDecrypt($data){
|
public static function blockDecrypt($data, $key=''){
|
||||||
$result='';
|
$result='';
|
||||||
while(strlen($data)){
|
while(strlen($data)){
|
||||||
$result=self::decrypt(substr($data,0,8192));
|
$result.=self::decrypt(substr($data,0,8192),$key);
|
||||||
$data=substr($data,8192);
|
$data=substr($data,8192);
|
||||||
}
|
}
|
||||||
return $result;
|
return $result;
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue