development snapshot

This commit is contained in:
Sam Tuke 2012-08-23 19:19:39 +01:00
parent 32ee3de918
commit e3ac15bad3
3 changed files with 121 additions and 84 deletions

View File

@ -161,7 +161,7 @@ class Crypt {
* @returns decrypted file
*/
public static function decrypt( $encryptedContent, $iv, $passphrase ) {
echo "\n\nJET \$passphrase = $passphrase , \$iv = $iv\n\n";
if ( $plainContent = openssl_decrypt( $encryptedContent, 'AES-128-CFB', $passphrase, false, $iv ) ) {
return $plainContent;
@ -408,18 +408,30 @@ class Crypt {
$remaining = $plainContent;
$testarray = array();
while( strlen( $remaining ) ) {
//echo "\n\n\$block = ".substr( $remaining, 0, 8192 );
// Encrypt a chunk of unencrypted data and add it to the rest
$block = self::symmetricEncryptFileContent( substr( $remaining, 0, 8192 ), $key );
$crypted .= $block;
$testarray[] = $block;
// Remove the data already encrypted from remaining unencrypted data
$remaining = substr( $remaining, 8192 );
}
//echo "hags ";
//echo "\n\n\n\$crypted = $crypted\n\n\n";
//print_r($testarray);
return $crypted;
}
@ -430,13 +442,17 @@ class Crypt {
*/
public static function symmetricBlockDecryptFileContent( $crypted, $key ) {
//echo "\n\n\nfags \$crypted = $crypted\n\n\n";
echo "\n\n\nfags \$crypted = $crypted\n\n\n";
$decrypted = '';
$remaining = $crypted;
$testarray = array();
while( strlen( $remaining ) ) {
$testarray[] = substr( $remaining, 0, 10946 );
// Encrypt a chunk of unencrypted data and add it to the rest
// 10946 is the length of a 8192 string once it has been encrypted
@ -447,6 +463,10 @@ class Crypt {
}
echo "nags ";
print_r($testarray);
return $decrypted;
}

View File

@ -182,7 +182,7 @@ class Stream {
}
/**
* @brief
* @brief Write write plan data as encrypted data
*/
public function stream_write( $data ) {
@ -208,55 +208,60 @@ class Stream {
Keymanager::setFileKey( $this->rawPath, $this->keyfile, new \OC_FilesystemView( '/' ) );
}
if ( $this->writeCache ) {
$data = $this->writeCache . $data;
$this->writeCache = '';
}
// Make sure we always start on a block start
if ( $currentPos % 8192 != 0 ) {
fseek( $this->source, - ( $currentPos % 8192 ), SEEK_CUR );
$encryptedBlock = fread( $this->source, 8192 );
fseek( $this->source, - ( $currentPos % 8192 ), SEEK_CUR );
$block = Crypt::symmetricDecryptFileContent( $encryptedBlock, $this->keyfile );
$data = substr( $block, 0, $currentPos % 8192 ) . $data;
fseek( $this->source, - ( $currentPos % 8192 ), SEEK_CUR );
}
$currentPos = ftell( $this->source );
while( $remainingLength = strlen( $data ) > 0 ) {
if ( $remainingLength < 8192 ) {
$this->writeCache = $data;
$data = '';
} else {
// // Set $data to contents of writeCache
// // Concat writeCache to start of $data
// if ( $this->writeCache ) {
//
// $data = $this->writeCache . $data;
//
// $this->writeCache = '';
//
// }
// // Make sure we always start on a block start
// if ( 0 != ( $currentPos % 8192 ) ) { // If we're not at the end of file yet (in the final chunk), if there will be no bytes left to read after the current chunk
//
// fseek( $this->source, - ( $currentPos % 8192 ), SEEK_CUR );
//
// $encryptedBlock = fread( $this->source, 8192 );
//
// fseek( $this->source, - ( $currentPos % 8192 ), SEEK_CUR );
//
// $block = Crypt::symmetricDecryptFileContent( $encryptedBlock, $this->keyfile );
//
// $x = substr( $block, 0, $currentPos % 8192 );
//
// $data = $x . $data;
//
// fseek( $this->source, - ( $currentPos % 8192 ), SEEK_CUR );
//
// }
// $currentPos = ftell( $this->source );
//
// while( $remainingLength = strlen( $data ) > 0 ) {
//
// // Set writeCache to contents of $data
// if ( $remainingLength < 8192 ) {
//
// $this->writeCache = $data;
//
// $data = '';
//
// } else {
$encrypted = Crypt::symmetricBlockEncryptFileContent( $data, $this->keyfile );
//$encrypted = $data;
fwrite( $this->source, $encrypted );
$data = substr( $data,8192 );
$data = substr( $data, 8192 );
}
}
// }
//
// }
$this->size = max( $this->size, $currentPos + $length );

View File

@ -25,8 +25,6 @@ class Test_Crypt extends \PHPUnit_Framework_TestCase {
$this->view = new \OC_FilesystemView( '/' );
//stream_wrapper_register( 'crypt', 'OCA_Encryption\Stream' );
}
function tearDown(){}
@ -109,58 +107,72 @@ class Test_Crypt extends \PHPUnit_Framework_TestCase {
$key = file_get_contents( '/home/samtuke/owncloud/git/oc3/data/admin/files_encryption/keyfiles/sscceEncrypt-1345649062.key' );
$crypted = Crypt::symmetricBlockEncryptFileContent( substr( $this->dataLong, 0, 6500 ), $key );
$crypted = Crypt::symmetricBlockEncryptFileContent( $this->dataLong, $key );
$this->assertNotEquals( $this->dataLong, $crypted );
//echo "\n\nCAT ".substr( $this->dataLong, 0, 7000 );
$decrypt = Crypt::symmetricBlockDecryptFileContent( $crypted, $key );
$this->assertEquals( substr( $this->dataLong, 0, 6500
), $decrypt );
$this->assertEquals( $this->dataLong, $decrypt );
}
// function testSymmetricBlockStreamEncryptFileContent() {
//
// \OC_User::setUserId( 'admin' );
//
// // Disable encryption proxy to prevent unwanted en/decryption
// \OC_FileProxy::$enabled = false;
//
// $cryptedFile = file_put_contents( 'crypt://' . '/blockEncrypt', $this->dataUrl );
//
// // Test that data was successfully written
// $this->assertTrue( is_int( $cryptedFile ) );
//
// // Disable encryption proxy to prevent unwanted en/decryption
// \OC_FileProxy::$enabled = false;
//
//
//
// // Get file contents without using any wrapper to get it's actual contents on disk
// $retreivedCryptedFile = $this->view->file_get_contents( '/blockEncrypt' );
//
// echo "\n\n\$retreivedCryptedFile = !! $retreivedCryptedFile !!";
//
// $key = file_get_contents( '/home/samtuke/owncloud/git/oc3/data/files_encryption/keyfiles/tmp/testSetFileKey.key' );
//
// echo "\n\n\$key = !! $key !!";
//
// $manualDecrypt = Crypt::symmetricDecryptFileContent( $retreivedCryptedFile, $key );
//
// echo "\n\n\$manualDecrypt = !! $manualDecrypt !!";
//
function testSymmetricStreamEncryptShortFileContent() {
\OC_User::setUserId( 'admin' );
$filename = 'flockEncrypt';
$cryptedFile = file_put_contents( 'crypt://' . '/' . $filename, $this->dataShort );
// Test that data was successfully written
$this->assertTrue( is_int( $cryptedFile ) );
// Get file contents without using any wrapper to get it's actual contents on disk
$retreivedCryptedFile = $this->view->file_get_contents( '/'. $filename );
// Check that the file was encrypted before being written to disk
$this->assertNotEquals( $this->dataShort, $retreivedCryptedFile );
$key = file_get_contents( '/home/samtuke/owncloud/git/oc3/data/admin/files_encryption/keyfiles/' . $filename . '.key' );
$manualDecrypt = Crypt::symmetricBlockDecryptFileContent( $retreivedCryptedFile, $key );
$this->assertEquals( $this->dataShort, $manualDecrypt );
}
function testSymmetricStreamEncryptLongFileContent() {
\OC_User::setUserId( 'admin' );
$filename = 'clockEncrypt';
$cryptedFile = file_put_contents( 'crypt://' . '/' . $filename, $this->dataLong );
// Test that data was successfully written
$this->assertTrue( is_int( $cryptedFile ) );
// Get file contents without using any wrapper to get it's actual contents on disk
$retreivedCryptedFile = $this->view->file_get_contents( '/'. $filename );
echo "\n\nsock $retreivedCryptedFile\n\n";
// // Check that the file was encrypted before being written to disk
// $this->assertNotEquals( $this->dataUrl, $retreivedCryptedFile );
// $this->assertNotEquals( $this->dataLong, $retreivedCryptedFile );
//
// $decrypt = Crypt::symmetricBlockDecryptFileContent( $retreivedCryptedFile, $key);
//
// $this->assertEquals( $this->dataUrl, $decrypt );
// $key = file_get_contents( '/home/samtuke/owncloud/git/oc3/data/admin/files_encryption/keyfiles/' . $filename . '.key' );
//
// }
// $manualDecrypt = Crypt::symmetricBlockDecryptFileContent( $retreivedCryptedFile, $key );
//
// $this->assertEquals( $this->dataLong, $manualDecrypt );
}
// function testSymmetricBlockStreamDecryptFileContent() {
//