Skip to content

Commit ade46cb

Browse files
NoMan2000bfentiman
authored andcommitted
Switched IV vector to openssl and added a test to verify it works as before (#4)
* Update cryptor.php * Simple Assert test Added a simple assert test to prove it works. Signed-off-by: Michael Soileau <webart.video@gmail.com>
1 parent 283a056 commit ade46cb

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

cryptor.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ public function encryptString($in, $key, $fmt = null)
7474
}
7575

7676
// Build an initialisation vector
77-
$iv = mcrypt_create_iv($this->iv_num_bytes, MCRYPT_DEV_URANDOM);
77+
$iv = openssl_random_pseudo_bytes($this->iv_num_bytes, $isStrongCrypto);
78+
if (!$isStrongCrypto) {
79+
throw new \Exception("Not a strong key");
80+
}
7881

7982
// Hash the key
8083
$keyhash = openssl_digest($key, $this->hash_algo, true);

tests/BasicCryptTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../cryptor.php';
4+
5+
$data = 'Good things come in small packages.';
6+
$key = '9901:io=[<>602vV03&Whb>9J&M~Oq';
7+
8+
$encrypted = Cryptor::Encrypt($data, $key);
9+
$decrypted = Cryptor::Decrypt($encrypted, $key);
10+
11+
assert(strlen($data) === strlen($decrypted), 'The decrypted data will match the binary size of the encrypted data');
12+
13+
assert($data === $decrypted, 'The encrypted data will match the decrypted data');

0 commit comments

Comments
 (0)