Skip to content

Commit

Permalink
V5 (#2)
Browse files Browse the repository at this point in the history
v5
  • Loading branch information
miladrahimi committed Oct 17, 2020
1 parent 0d22d03 commit c4e0557
Show file tree
Hide file tree
Showing 22 changed files with 292 additions and 394 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ php:
- 7.1
- 7.2
- 7.3
- 7.4

branches:
only:
Expand Down
58 changes: 26 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ $symmetric = new Symmetric($key);
// ...
```

And if you want to generate a key:
If you want to generate a key:

```php
use MiladRahimi\PhpCrypt\Symmetric;
Expand All @@ -71,21 +71,8 @@ use MiladRahimi\PhpCrypt\Exceptions\MethodNotSupportedException;
use MiladRahimi\PhpCrypt\Symmetric;

try {
$symmetric = new Symmetric('YOUR-KEY', 'aria-256-ctr');
// ...
} catch (MethodNotSupportedException $e) {
// You method is not supported.
}
```

If you still like to have an auto-generated key for your custom method, pass null for the first argument.

```php
use MiladRahimi\PhpCrypt\Exceptions\MethodNotSupportedException;
use MiladRahimi\PhpCrypt\Symmetric;

try {
$symmetric = new Symmetric(null, 'aria-256-ctr');
$symmetric = new Symmetric();
$symmetric->setMethod('aria-256-ctr');
// ...
} catch (MethodNotSupportedException $e) {
// You method is not supported.
Expand All @@ -94,7 +81,7 @@ try {

### Supported Methods

If you want to know which methods are currently supported by your installed OpenSSL extension, see the snippet below:
If you want to know which methods do your installed OpenSSL extension support, see the snippet below:

```php
use MiladRahimi\PhpCrypt\Symmetric;
Expand All @@ -111,40 +98,47 @@ The examples below illustrates how to encrypt/decrypt data using the RSA algorit
In this example, we encrypt with a private key and decrypt with the related public key.

```php
use MiladRahimi\PhpCrypt\Rsa;
use MiladRahimi\PhpCrypt\PrivateRsa;
use MiladRahimi\PhpCrypt\PublicRsa;

$rsa = new Rsa('./private_key.pem', './public_key.pem');
$privateRsa = new PrivateRsa('private_key.pem');
$publicRsa = new PublicRsa('public_key.pem');

$result = $rsa->encryptWithPrivate('secret');
echo $rsa->decryptWithPublic($result); // secret
$result = $privateRsa->encrypt('secret');
echo $publicRsa->decrypt($result); // secret
```

### Encryption with public

In this example, we encrypt with a public key and decrypt with the related private key.

```php
use MiladRahimi\PhpCrypt\Rsa;
use MiladRahimi\PhpCrypt\PrivateRsa;
use MiladRahimi\PhpCrypt\PublicRsa;

$rsa = new Rsa('./private_key.pem', './public_key.pem');
$privateRsa = new PrivateRsa('private_key.pem');
$publicRsa = new PublicRsa('public_key.pem');

$result = $rsa->encryptWithPublic('secret');
echo $rsa->decryptWithPrivate($result); // secret
$result = $publicRsa->encrypt('secret');
echo $privateRsa->decrypt($result); // secret
```

### Base64 Encoding

In default, the encrypted data returned by `RSA::encryptWithPrivate()` and `RSA::encryptWithPublic()` will be Base64 encoded. You can disable if you pass `false` for `base64` argument.
In default, the encrypted data returned by `PrivateRsa::encrypt()` and `PublicRsa::encrypt()` will be Base64 encoded. You can disable if you pass `false` for `base64` argument.

```php
use MiladRahimi\PhpCrypt\Rsa;
use MiladRahimi\PhpCrypt\PrivateRsa;
use MiladRahimi\PhpCrypt\PublicRsa;

$rsa = new Rsa('./private_key.pem', './public_key.pem');
$privateRsa = new PrivateRsa('private_key.pem');
$publicRsa = new PublicRsa('public_key.pem');

// For public encryption
$result = $rsa->encryptWithPublic('secret', false);
$result = $publicRsa->encrypt('secret', false);

// And for private encryption
$result = $rsa->encryptWithPrivate('secret', false);
$result = $privateRsa->encrypt('secret', false);
```

## Hashing
Expand All @@ -163,13 +157,13 @@ echo $hash->verify('AnotherPassword', $hashedPassword); // false

## Error Handling

The `Symmetric`, `RSA`, and `Hash` classes may throw these exceptions:
The `Symmetric`, `PrivateRsa`, `PublicRsa`, and `Hash` classes may throw these exceptions:

* `EncryptionException`: When it cannot encrypt.
* `DecryptionException`: When it cannot decrypt.
* `HashingException`: When it cannot hash.
* `MethodNotSupportedException`: When the passed method to the `Symmetric` class is not supported.
* `InvalidKeyException`: When the passed key to `RSA` class is not valid.
* `InvalidKeyException`: When the passed key to `PrivateRsa` or `PublicRsa` classes is not valid.

## License

Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
{
"name": "miladrahimi/phpcrypt",
"description": "Encryption, decryption, and password hashing tools for PHP projects",
"description": "Encryption, decryption, and hashing tools for PHP projects",
"keywords": [
"Crypt",
"Cryptography",
"Encrypt",
"Decrypt",
"Hash",
"Password",
"Symmetric",
"Asymmetric",
"OpenSSL",
"RSA",
"AES"
],
"homepage": "http://miladrahimi.github.io",
"homepage": "https://github.com/miladrahimi/phpcrypt",
"type": "library",
"license": "MIT",
"authors": [
Expand Down
9 changes: 0 additions & 9 deletions examples/example-01.php

This file was deleted.

11 changes: 0 additions & 11 deletions examples/example-02.php

This file was deleted.

10 changes: 0 additions & 10 deletions examples/example-03.php

This file was deleted.

17 changes: 0 additions & 17 deletions examples/example-04.php

This file was deleted.

15 changes: 0 additions & 15 deletions examples/example-05.php

This file was deleted.

8 changes: 0 additions & 8 deletions examples/example-06.php

This file was deleted.

13 changes: 0 additions & 13 deletions examples/example-07.php

This file was deleted.

13 changes: 0 additions & 13 deletions examples/example-08.php

This file was deleted.

11 changes: 0 additions & 11 deletions examples/example-09.php

This file was deleted.

20 changes: 10 additions & 10 deletions src/Hash.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@

/**
* Class Hash
* It hashes and verifies passwords.
* It hashes and verifies hashed data (e.g. passwords).
*
* @package MiladRahimi\PhpCrypt
*/
class Hash
{
/**
* Make a hash from the given password
* Make a hash from the given plain data
*
* @param string $password
* @param string $plain
* @return string
* @throws HashingException
*/
public function make(string $password): string
public function make(string $plain): string
{
$result = password_hash($password, PASSWORD_BCRYPT);
$result = password_hash($plain, PASSWORD_BCRYPT);
if ($result === false) {
throw new HashingException();
}
Expand All @@ -30,14 +30,14 @@ public function make(string $password): string
}

/**
* Verify the given plain password with the given hashed password
* Verify the given plain with the given hashed value
*
* @param string $plainPassword
* @param string $hashedPassword
* @param string $plain
* @param string $hashed
* @return bool
*/
public function verify(string $plainPassword, string $hashedPassword): bool
public function verify(string $plain, string $hashed): bool
{
return password_verify($plainPassword, $hashedPassword);
return password_verify($plain, $hashed);
}
}

0 comments on commit c4e0557

Please sign in to comment.