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
3329e0f2
Commit
3329e0f2
authored
10 years ago
by
Lukas Reschke
Browse files
Options
Downloads
Patches
Plain Diff
Use DI
parent
d26a9c3c
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
lib/private/security/crypto.php
+13
-5
13 additions, 5 deletions
lib/private/security/crypto.php
lib/private/server.php
+1
-1
1 addition, 1 deletion
lib/private/server.php
tests/lib/security/crypto.php
+6
-6
6 additions, 6 deletions
tests/lib/security/crypto.php
with
20 additions
and
12 deletions
lib/private/security/crypto.php
+
13
−
5
View file @
3329e0f2
...
...
@@ -12,7 +12,9 @@ namespace OC\Security;
use
Crypt_AES
;
use
Crypt_Hash
;
use
OCP\Security\ICrypto
;
use
OCP\Security\ISecureRandom
;
use
OCP\Security\StringUtils
;
use
OCP\IConfig
;
/**
* Class Crypto provides a high-level encryption layer using AES-CBC. If no key has been provided
...
...
@@ -29,9 +31,15 @@ class Crypto implements ICrypto {
private
$cipher
;
/** @var int */
private
$ivLength
=
16
;
/** @var IConfig */
private
$config
;
/** @var ISecureRandom */
private
$random
;
function
__construct
()
{
function
__construct
(
IConfig
$config
,
ISecureRandom
$random
)
{
$this
->
cipher
=
new
Crypt_AES
();
$this
->
config
=
$config
;
$this
->
random
=
$random
;
}
/**
...
...
@@ -41,7 +49,7 @@ class Crypto implements ICrypto {
*/
public
function
calculateHMAC
(
$message
,
$password
=
''
)
{
if
(
$password
===
''
)
{
$password
=
\OC
::
$server
->
getC
onfig
()
->
getSystemValue
(
'secret'
);
$password
=
$this
->
c
onfig
->
getSystemValue
(
'secret'
);
}
$hash
=
new
Crypt_Hash
(
'sha512'
);
...
...
@@ -57,11 +65,11 @@ class Crypto implements ICrypto {
*/
public
function
encrypt
(
$plaintext
,
$password
=
''
)
{
if
(
$password
===
''
)
{
$password
=
\OC
::
$server
->
getC
onfig
()
->
getSystemValue
(
'secret'
);
$password
=
$this
->
c
onfig
->
getSystemValue
(
'secret'
);
}
$this
->
cipher
->
setPassword
(
$password
);
$iv
=
\OC
::
$server
->
getSecureR
andom
()
->
getLowStrengthGenerator
()
->
generate
(
$this
->
ivLength
);
$iv
=
$this
->
r
andom
->
getLowStrengthGenerator
()
->
generate
(
$this
->
ivLength
);
$this
->
cipher
->
setIV
(
$iv
);
$ciphertext
=
bin2hex
(
$this
->
cipher
->
encrypt
(
$plaintext
));
...
...
@@ -79,7 +87,7 @@ class Crypto implements ICrypto {
*/
public
function
decrypt
(
$authenticatedCiphertext
,
$password
=
''
)
{
if
(
$password
===
''
)
{
$password
=
\OC
::
$server
->
getC
onfig
()
->
getSystemValue
(
'secret'
);
$password
=
$this
->
c
onfig
->
getSystemValue
(
'secret'
);
}
$this
->
cipher
->
setPassword
(
$password
);
...
...
This diff is collapsed.
Click to expand it.
lib/private/server.php
+
1
−
1
View file @
3329e0f2
...
...
@@ -205,7 +205,7 @@ class Server extends SimpleContainer implements IServerContainer {
return
new
SecureRandom
();
});
$this
->
registerService
(
'Crypto'
,
function
(
$c
)
{
return
new
Crypto
();
return
new
Crypto
(
\OC
::
$server
->
getConfig
(),
\OC
::
$server
->
getSecureRandom
()
);
});
$this
->
registerService
(
'Db'
,
function
(
$c
)
{
return
new
Db
();
...
...
This diff is collapsed.
Click to expand it.
tests/lib/security/crypto.php
+
6
−
6
View file @
3329e0f2
...
...
@@ -12,7 +12,7 @@ class CryptoTest extends \PHPUnit_Framework_TestCase {
function
testDefaultEncrypt
()
{
$stringToEncrypt
=
'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt.'
;
$crypto
=
new
Crypto
();
$crypto
=
new
Crypto
(
\OC
::
$server
->
getConfig
(),
\OC
::
$server
->
getSecureRandom
()
);
$ciphertext
=
$crypto
->
encrypt
(
$stringToEncrypt
);
$this
->
assertEquals
(
$stringToEncrypt
,
$crypto
->
decrypt
(
$ciphertext
));
...
...
@@ -27,17 +27,17 @@ class CryptoTest extends \PHPUnit_Framework_TestCase {
*/
function
testWrongPassword
()
{
$stringToEncrypt
=
'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt.'
;
$encryptCrypto
=
new
Crypto
();
$encryptCrypto
=
new
Crypto
(
\OC
::
$server
->
getConfig
(),
\OC
::
$server
->
getSecureRandom
()
);
$ciphertext
=
$encryptCrypto
->
encrypt
(
$stringToEncrypt
);
$decryptCrypto
=
new
Crypto
();
$decryptCrypto
=
new
Crypto
(
\OC
::
$server
->
getConfig
(),
\OC
::
$server
->
getSecureRandom
()
);
$this
->
assertFalse
(
$decryptCrypto
->
decrypt
(
$ciphertext
,
'A wrong password!'
));
}
function
testLaterDecryption
()
{
$stringToEncrypt
=
'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt.'
;
$encryptedString
=
'560f5436ba864b9f12f7f7ca6d41c327554a6f2c0a160a03316b202af07c65163274993f3a46e7547c07ba89304f00594a2f3bd99f83859097c58049c39d0d4ade10e0de914ff0604961e7c849d0271ed6c0b23f984ba16e7d033e3305fb0910e7b6a2a65c988d17dbee71d8f953684d|d2kdFUspVjC0Y0sr|1a5feacf87eaa6869a6abdfba9a296e7bbad45b6ad89f7dce67cdc98e2da5dc4379cc672cc655e52bbf19599bf59482fbea13a73937697fa656bf10f3fc4f1aa'
;
$crypto
=
new
Crypto
();
$crypto
=
new
Crypto
(
\OC
::
$server
->
getConfig
(),
\OC
::
$server
->
getSecureRandom
()
);
$this
->
assertEquals
(
$stringToEncrypt
,
$crypto
->
decrypt
(
$encryptedString
,
'ThisIsAVeryS3cur3P4ssw0rd'
));
}
...
...
@@ -47,7 +47,7 @@ class CryptoTest extends \PHPUnit_Framework_TestCase {
*/
function
testWrongIV
()
{
$encryptedString
=
'560f5436ba864b9f12f7f7ca6d41c327554a6f2c0a160a03316b202af07c65163274993f3a46e7547c07ba89304f00594a2f3bd99f83859097c58049c39d0d4ade10e0de914ff0604961e7c849d0271ed6c0b23f984ba16e7d033e3305fb0910e7b6a2a65c988d17dbee71d8f953684d|d2kdFUspVjC0o0sr|1a5feacf87eaa6869a6abdfba9a296e7bbad45b6ad89f7dce67cdc98e2da5dc4379cc672cc655e52bbf19599bf59482fbea13a73937697fa656bf10f3fc4f1aa'
;
$crypto
=
new
Crypto
();
$crypto
=
new
Crypto
(
\OC
::
$server
->
getConfig
(),
\OC
::
$server
->
getSecureRandom
()
);
$crypto
->
decrypt
(
$encryptedString
,
'ThisIsAVeryS3cur3P4ssw0rd'
);
}
...
...
@@ -57,7 +57,7 @@ class CryptoTest extends \PHPUnit_Framework_TestCase {
*/
function
testWrongParameters
()
{
$encryptedString
=
'1|2'
;
$crypto
=
new
Crypto
();
$crypto
=
new
Crypto
(
\OC
::
$server
->
getConfig
(),
\OC
::
$server
->
getSecureRandom
()
);
$crypto
->
decrypt
(
$encryptedString
,
'ThisIsAVeryS3cur3P4ssw0rd'
);
}
}
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