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
90a62501
Commit
90a62501
authored
Jun 29, 2014
by
Paul Klimov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Result check at `Security::generateRandomKey()` added
parent
1085f1bd
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
16 additions
and
1 deletion
+16
-1
Security.php
framework/base/Security.php
+9
-1
SecurityTest.php
tests/unit/framework/base/SecurityTest.php
+7
-0
No files found.
framework/base/Security.php
View file @
90a62501
...
...
@@ -309,11 +309,19 @@ class Security extends Component
* Generates a random key.
* Note the generated key is a binary string with the specified number of bytes in it.
* @param integer $length the length of the key that should be generated
* @throws Exception on failure.
* @return string the generated random key
*/
public
function
generateRandomKey
(
$length
=
32
)
{
return
mcrypt_create_iv
(
$length
,
MCRYPT_DEV_URANDOM
);
if
(
!
extension_loaded
(
'mcrypt'
))
{
throw
new
InvalidConfigException
(
'The mcrypt PHP extension is not installed.'
);
}
$key
=
mcrypt_create_iv
(
$length
,
MCRYPT_DEV_URANDOM
);
if
(
$key
===
false
)
{
throw
new
Exception
(
'Unable to generate random key.'
);
}
return
$key
;
}
/**
...
...
tests/unit/framework/base/SecurityTest.php
View file @
90a62501
...
...
@@ -131,4 +131,11 @@ class SecurityTest extends TestCase
$decryptedData
=
$this
->
security
->
decrypt
(
$encryptedData
,
$key
);
$this
->
assertEquals
(
$data
,
$decryptedData
);
}
public
function
testGenerateRandomKey
()
{
$keyLength
=
20
;
$key
=
$this
->
security
->
generateRandomKey
(
$keyLength
);
$this
->
assertEquals
(
$keyLength
,
strlen
(
$key
));
}
}
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