Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
5a429857
Commit
5a429857
authored
Jun 27, 2014
by
Paul Klimov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Option `Security::useDeriveKeyUniqueSalt` added
parent
772667fa
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
7 deletions
+41
-7
Security.php
framework/base/Security.php
+25
-5
SecurityTest.php
tests/unit/framework/base/SecurityTest.php
+16
-2
No files found.
framework/base/Security.php
View file @
5a429857
...
...
@@ -76,6 +76,12 @@ class Security extends Component
* - 'crypt' - use PHP `crypt()` function.
*/
public
$passwordHashStrategy
=
'crypt'
;
/**
* @var boolean whether to generate unique salt while deriving encryption key.
* If enabled (recommended) this option increases encrypted text length, but provide more security.
* If disabled this option reduces encrypted text length, but also reduce security.
*/
public
$useDeriveKeyUniqueSalt
=
true
;
/**
* Encrypts data.
...
...
@@ -89,10 +95,18 @@ class Security extends Component
{
$module
=
$this
->
openCryptModule
();
$data
=
$this
->
addPadding
(
$data
);
$iv
=
mcrypt_create_iv
(
mcrypt_enc_get_iv_size
(
$module
),
MCRYPT_DEV_URANDOM
);
$key
=
$this
->
deriveKey
(
$password
,
$iv
);
$ivSize
=
mcrypt_enc_get_iv_size
(
$module
);
$iv
=
mcrypt_create_iv
(
$ivSize
,
MCRYPT_DEV_URANDOM
);
if
(
$this
->
useDeriveKeyUniqueSalt
)
{
$keySalt
=
mcrypt_create_iv
(
$ivSize
,
MCRYPT_DEV_URANDOM
);
$encrypted
=
$keySalt
;
}
else
{
$keySalt
=
$iv
;
$encrypted
=
''
;
}
$key
=
$this
->
deriveKey
(
$password
,
$keySalt
);
mcrypt_generic_init
(
$module
,
$key
,
$iv
);
$encrypted
=
$iv
.
mcrypt_generic
(
$module
,
$data
);
$encrypted
.
=
$iv
.
mcrypt_generic
(
$module
,
$data
);
mcrypt_generic_deinit
(
$module
);
mcrypt_module_close
(
$module
);
...
...
@@ -115,9 +129,15 @@ class Security extends Component
$module
=
$this
->
openCryptModule
();
$ivSize
=
mcrypt_enc_get_iv_size
(
$module
);
$iv
=
StringHelper
::
byteSubstr
(
$data
,
0
,
$ivSize
);
$key
=
$this
->
deriveKey
(
$password
,
$iv
);
$keySalt
=
$iv
;
$encrypted
=
StringHelper
::
byteSubstr
(
$data
,
$ivSize
,
StringHelper
::
byteLength
(
$data
));
if
(
$this
->
useDeriveKeyUniqueSalt
)
{
$iv
=
StringHelper
::
byteSubstr
(
$encrypted
,
0
,
$ivSize
);
$encrypted
=
StringHelper
::
byteSubstr
(
$encrypted
,
$ivSize
,
StringHelper
::
byteLength
(
$encrypted
));
}
$key
=
$this
->
deriveKey
(
$password
,
$keySalt
);
mcrypt_generic_init
(
$module
,
$key
,
$iv
);
$decrypted
=
mdecrypt_generic
(
$module
,
StringHelper
::
byteSubstr
(
$data
,
$ivSize
,
StringHelper
::
byteLength
(
$data
))
);
$decrypted
=
mdecrypt_generic
(
$module
,
$encrypted
);
mcrypt_generic_deinit
(
$module
);
mcrypt_module_close
(
$module
);
...
...
tests/unit/framework/base/SecurityTest.php
View file @
5a429857
...
...
@@ -83,10 +83,22 @@ class SecurityTest extends TestCase
return
[
[
'hmac'
,
false
true
,
false
,
],
[
'hmac'
,
false
,
false
,
],
[
'pbkdf2'
,
true
,
!
function_exists
(
'hash_pbkdf2'
)
],
[
'pbkdf2'
,
false
,
!
function_exists
(
'hash_pbkdf2'
)
],
];
...
...
@@ -96,15 +108,17 @@ class SecurityTest extends TestCase
* @dataProvider dataProviderEncrypt
*
* @param string $deriveKeyStrategy
* @param boolean $useDeriveKeyUniqueSalt
* @param boolean $isSkipped
*/
public
function
testEncrypt
(
$deriveKeyStrategy
,
$isSkipped
)
public
function
testEncrypt
(
$deriveKeyStrategy
,
$
useDeriveKeyUniqueSalt
,
$
isSkipped
)
{
if
(
$isSkipped
)
{
$this
->
markTestSkipped
(
"Unable to test '
{
$deriveKeyStrategy
}
' derive key strategy"
);
return
;
}
$this
->
security
->
deriveKeyStrategy
=
$deriveKeyStrategy
;
$this
->
security
->
useDeriveKeyUniqueSalt
=
$useDeriveKeyUniqueSalt
;
$data
=
'known data'
;
$key
=
'secret'
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment