Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Χάρης Παπαδόπουλος
e-epal
Commits
f3f4798e
Commit
f3f4798e
authored
Jun 07, 2017
by
Σταύρος Παπαδάκης
Browse files
Add crypt lib
parent
a74c5ffb
Changes
1
Hide whitespace changes
Inline
Side-by-side
drupal/modules/epal/src/Crypt.php
0 → 100644
View file @
f3f4798e
<?php
namespace
Drupal\epal
;
use
Defuse\Crypto\Key
;
use
Defuse\Crypto\Crypto
;
use
\
Exception
;
class
Crypt
{
private
$fname
;
// store
public
function
__construct
(
$fname
)
{
if
(
is_string
(
$fname
))
{
$this
->
fname
=
$fname
;
}
else
{
$this
->
fname
=
__DIR__
.
"/../../../../../app.txt"
;
}
}
/**
* @throws \Exception If key is not found
* @return string The key to use
*/
protected
function
loadKey
()
{
if
(
!
is_readable
(
$this
->
fname
)
||
!
is_file
(
$this
->
fname
))
{
throw
new
\
Exception
(
__METHOD__
.
':: No file'
);
}
$keystring
=
file_get_contents
(
$this
->
fname
);
if
(
$keystring
===
false
)
{
throw
new
\
Exception
(
__METHOD__
.
':: Cannot load file'
);
}
return
Key
::
loadFromAsciiSafeString
(
$keystring
);
}
/**
* Provided a string value, encrypt and return encrypted value
*
* @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException if encryption cannot be performed
* @throws \Exception If value to encode is not string
* @return hex encoded encrypted value
*/
public
function
encrypt
(
$data
)
{
if
(
!
is_string
(
$data
))
{
throw
new
\
Exception
(
"Data to be encoded can only be strings"
);
}
$key
=
$this
->
loadKey
();
$dataenc
=
Crypto
::
encrypt
(
$data
,
$key
);
return
$dataenc
;
}
/**
* Provided a hex encoded encrypted value, decrypt and return original value
*
* @throws \Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException if decrypt failed!
* @return string the original value
*/
public
function
decrypt
(
$dataenc
)
{
$key
=
$this
->
loadKey
();
$data
=
Crypto
::
decrypt
(
$dataenc
,
$key
);
return
$data
;
}
}
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