Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
our_own_cloud_project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
die_coolen_jungs
our_own_cloud_project
Commits
70cb053e
Commit
70cb053e
authored
12 years ago
by
Robin Appelman
Browse files
Options
Downloads
Patches
Plain Diff
improve cryptstream fro writing non-chunksized data
parent
d8751917
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
apps/files_encryption/lib/cryptstream.php
+25
-3
25 additions, 3 deletions
apps/files_encryption/lib/cryptstream.php
apps/files_encryption/tests/encryption.php
+11
-0
11 additions, 0 deletions
apps/files_encryption/tests/encryption.php
with
36 additions
and
3 deletions
apps/files_encryption/lib/cryptstream.php
+
25
−
3
View file @
70cb053e
...
...
@@ -34,6 +34,7 @@ class OC_CryptStream{
private
$readBuffer
;
//for streams that dont support seeking
private
$meta
=
array
();
//header/meta for source stream
private
$count
;
private
$writeCache
;
public
function
stream_open
(
$path
,
$mode
,
$options
,
&
$opened_path
){
$path
=
str_replace
(
'crypt://'
,
''
,
$path
);
...
...
@@ -57,6 +58,7 @@ class OC_CryptStream{
}
public
function
stream_seek
(
$offset
,
$whence
=
SEEK_SET
){
$this
->
flush
();
fseek
(
$this
->
source
,
$offset
,
$whence
);
}
...
...
@@ -67,6 +69,7 @@ class OC_CryptStream{
public
function
stream_read
(
$count
){
//$count will always be 8192 https://bugs.php.net/bug.php?id=21641
//This makes this function a lot simpler but will breake everything the moment it's fixed
$this
->
writeCache
=
''
;
if
(
$count
!=
8192
){
OCP\Util
::
writeLog
(
'files_encryption'
,
'php bug 21641 no longer holds, decryption will not work'
,
OCP\Util
::
FATAL
);
die
();
...
...
@@ -84,6 +87,10 @@ class OC_CryptStream{
$length
=
strlen
(
$data
);
$written
=
0
;
$currentPos
=
ftell
(
$this
->
source
);
if
(
$this
->
writeCache
){
$data
=
$this
->
writeCache
.
$data
;
$this
->
writeCache
=
''
;
}
if
(
$currentPos
%
8192
!=
0
){
//make sure we always start on a block start
fseek
(
$this
->
source
,
-
(
$currentPos
%
8192
),
SEEK_CUR
);
...
...
@@ -91,11 +98,17 @@ class OC_CryptStream{
fseek
(
$this
->
source
,
-
(
$currentPos
%
8192
),
SEEK_CUR
);
$block
=
OC_Crypt
::
decrypt
(
$encryptedBlock
);
$data
=
substr
(
$block
,
0
,
$currentPos
%
8192
)
.
$data
;
fseek
(
$this
->
source
,
-
(
$currentPos
%
8192
),
SEEK_CUR
);
}
while
(
strlen
(
$data
)
>
0
){
$encrypted
=
OC_Crypt
::
encrypt
(
substr
(
$data
,
0
,
8192
));
fwrite
(
$this
->
source
,
$encrypted
);
$data
=
substr
(
$data
,
8192
);
if
(
strlen
(
$data
)
<
8192
){
$this
->
writeCache
=
$data
;
$data
=
''
;
}
else
{
$encrypted
=
OC_Crypt
::
encrypt
(
substr
(
$data
,
0
,
8192
));
fwrite
(
$this
->
source
,
$encrypted
);
$data
=
substr
(
$data
,
8192
);
}
}
return
$length
;
}
...
...
@@ -129,7 +142,16 @@ class OC_CryptStream{
return
feof
(
$this
->
source
);
}
private
function
flush
(){
if
(
$this
->
writeCache
){
$encrypted
=
OC_Crypt
::
encrypt
(
$this
->
writeCache
);
fwrite
(
$this
->
source
,
$encrypted
);
$this
->
writeCache
=
''
;
}
}
public
function
stream_close
(){
$this
->
flush
();
if
(
$this
->
meta
[
'mode'
]
!=
'r'
and
$this
->
meta
[
'mode'
]
!=
'rb'
){
OC_FileCache
::
put
(
$this
->
path
,
array
(
'encrypted'
=>
true
));
}
...
...
This diff is collapsed.
Click to expand it.
apps/files_encryption/tests/encryption.php
+
11
−
0
View file @
70cb053e
...
...
@@ -38,5 +38,16 @@ class Test_Encryption extends UnitTestCase {
OC_Crypt
::
decryptfile
(
$tmpFileEncrypted
,
$tmpFileDecrypted
,
$key
);
$decrypted
=
file_get_contents
(
$tmpFileDecrypted
);
$this
->
assertEqual
(
$decrypted
,
$source
);
$file
=
OC
::
$SERVERROOT
.
'/core/img/weather-clear.png'
;
$source
=
file_get_contents
(
$file
);
//binary file
$encrypted
=
OC_Crypt
::
encrypt
(
$source
,
$key
);
$decrypted
=
OC_Crypt
::
decrypt
(
$encrypted
,
$key
);
$this
->
assertEqual
(
$decrypted
,
$source
);
$encrypted
=
OC_Crypt
::
blockEncrypt
(
$source
,
$key
);
$decrypted
=
OC_Crypt
::
blockDecrypt
(
$encrypted
,
$key
);
$this
->
assertEqual
(
$decrypted
,
$source
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment