Skip to content

Commit 96b9c35

Browse files
committed
md5 to sha256 and removed defuse-encryption, extra tests
1 parent b8afafe commit 96b9c35

File tree

4 files changed

+3
-137
lines changed

4 files changed

+3
-137
lines changed

README.md

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@ All functions are static public functions right now so you can simply call the f
3737
### compareStrings($string1, $string2)
3838
Compare strings while preventing timed attacks
3939

40-
### decrypt($input, $key)
41-
Returns the decryped output as a string using [defuse/php-encryption](https://github.com/defuse/php-encryption)'s library.
42-
43-
### encrypt($input, $key = false)
44-
Encrypt a string, if no key is given one will be generated for you (Recommended) using [defuse/php-encryption](https://github.com/defuse/php-encryption)'s library.
45-
4640
### getFormToken('form_token_id', $form_token, $limit = 300)
4741
Verify a form token for the given id. The $limit is optional andm ust be given in seconds, if the limit is 300 and the token is used after 300 seconds it will be considered invalid.
4842

@@ -58,9 +52,6 @@ Returns a random hexadecimal number for the given length
5852
### randomInt($min, $max)
5953
Returns the a secure random integer within the given range.
6054

61-
### randomSecureKey()
62-
Return a random key using [defuse/php-encryption](https://github.com/defuse/php-encryption)'s library.
63-
6455
### randomString($length)
6556
Returns a random string for the given length
6657

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
}
1010
],
1111
"require": {
12-
"php": ">=5.6.0",
13-
"defuse/php-encryption": "^1.2"
12+
"php": ">=5.6.0"
1413
},
1514
"autoload": {
1615
"psr-4": {

composer.lock

Lines changed: 0 additions & 66 deletions
This file was deleted.

src/SecureFuncs.php

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -9,50 +9,6 @@ class SecureFuncs
99
*/
1010
private static $_keyLength = 32;
1111

12-
/**
13-
* @param $input
14-
* @param $key
15-
* @return string
16-
* @throws \CannotPerformOperationException
17-
* @throws \InvalidCiphertextException
18-
*/
19-
public static function decrypt($input, $key)
20-
{
21-
try {
22-
return \Crypto::decrypt($input, $key);
23-
} catch (\Defuse\Crypto\Exception\InvalidCiphertextException $ex) {
24-
die('DANGER! DANGER! The ciphertext has been tampered with!');
25-
} catch (\Defuse\Crypto\Exception\CryptoTestFailedException $ex) {
26-
die('Cannot safely perform decryption');
27-
} catch (\Defuse\Crypto\Exception\CannotPerformOperationException $ex) {
28-
die('Cannot safely perform decryption');
29-
}
30-
}
31-
32-
/**
33-
* @param $input
34-
* @param bool $key
35-
* @return array
36-
* @throws CannotPerformOperationException
37-
* @throws \CannotPerformOperationException
38-
*/
39-
public static function encrypt($input, $key = false)
40-
{
41-
if ($key === false || GenericFuncs::strlen($key) !== Crypto::KEY_BYTE_SIZE) {
42-
$key = self::randomSecureKey();
43-
}
44-
45-
try {
46-
$ciphertext = \Crypto::encrypt($input, $key);
47-
} catch (\Defuse\Crypto\Exception\CryptoTestFailedException $ex) {
48-
die('Cannot safely perform encryption');
49-
} catch (\Defuse\Crypto\Exception\CannotPerformOperationException $ex) {
50-
die('Cannot safely perform encryption');
51-
}
52-
53-
return array('Key' => $key, 'Encrypted' => $ciphertext);
54-
}
55-
5612
/**
5713
* Checks if the given id and token match > If not the form has been sent twice or the ID is incorrect
5814
* @param $id
@@ -65,7 +21,7 @@ public static function getFormToken($id, $token, $limit_time = 300)
6521
// Check if isset
6622
if (!empty($_SESSION['formtoken'][$id]) && !empty($_SESSION['formtoken_time'][$id])) {
6723
// Check if token is correct
68-
if (md5($_SESSION['formtoken'][$id]) === $token) {
24+
if (hash('sha256', $_SESSION['formtoken'][$id]) === $token) {
6925
$valid = true;
7026
// If time limit is set, check if isset
7127
if ($limit_time !== false) {
@@ -136,20 +92,6 @@ public static function randomInt($min, $max)
13692
return $min + $randDiff;
13793
}
13894

139-
/**
140-
* @return string
141-
*/
142-
public static function randomSecureKey()
143-
{
144-
try {
145-
return \Crypto::createNewRandomKey();
146-
} catch (\Defuse\Crypto\Exception\CryptoTestFailedException $ex) {
147-
die('Cannot safely create a key');
148-
} catch (\Defuse\Crypto\Exception\CannotPerformOperationException $ex) {
149-
die('Cannot safely create a key');
150-
}
151-
}
152-
15395
/**
15496
* @param $length
15597
* @return string
@@ -180,7 +122,7 @@ public static function setFormToken($id)
180122
{
181123
$_SESSION['formtoken'][$id] = self::randomString(100);
182124
$_SESSION['formtoken_time'][$id] = time();
183-
return md5($_SESSION['formtoken'][$id]);
125+
return hash('sha256', $_SESSION['formtoken'][$id]);
184126
}
185127

186128
/**

0 commit comments

Comments
 (0)