Skip to content

Commit

Permalink
no message
Browse files Browse the repository at this point in the history
  • Loading branch information
mmeyer2k committed Jul 3, 2019
1 parent 80dd47b commit 445017f
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 105 deletions.
6 changes: 3 additions & 3 deletions src/OneTimePad.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class OneTimePad
/**
* Encrypt or decrypt a binary input string.
*
* @param string $input Input data to encrypt
* @param string $key Encryption/decryption key to use on input
* @param string $algo Hashing algo to generate keystream
* @param string $input Input data to encrypt
* @param string $key Encryption/decryption key to use on input
* @param string $algo Hashing algo to generate keystream
* @return string
*/
public static function crypt(string $input, string $key, string $algo = 'sha3-512'): string
Expand Down
2 changes: 2 additions & 0 deletions src/OpensslBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class OpensslBridge
*
* @param string $data Ciphertext to decrypt
* @param string $key Key which will be used to decrypt data
*
* @return string
*/
public static function decrypt(string $data, string $key): string
Expand All @@ -42,6 +43,7 @@ public static function decrypt(string $data, string $key): string
*
* @param string $data Plaintext string to encrypt.
* @param string $key Key which will be used to encrypt data
*
* @return string
*/
public static function encrypt(string $data, string $key): string
Expand Down
189 changes: 98 additions & 91 deletions src/OpensslStack.php
Original file line number Diff line number Diff line change
@@ -1,91 +1,98 @@
<?php declare(strict_types=1);

/**
* OpensslStack.php
*
* PHP version 7
*
* @category Dcrypt
* @package Dcrypt
* @author Michael Meyer (mmeyer2k) <m.meyer2k@gmail.com>
* @license http://opensource.org/licenses/MIT The MIT License (MIT)
* @link https://github.com/mmeyer2k/dcrypt
*/

namespace Dcrypt;

/**
* A factory class to build and use custom encryption stacks.
*
* @category Dcrypt
* @package Dcrypt
* @author Michael Meyer (mmeyer2k) <m.meyer2k@gmail.com>
* @license http://opensource.org/licenses/MIT The MIT License (MIT)
* @link https://github.com/mmeyer2k/dcrypt
*/
class OpensslStack
{
/**
* @var array
*/
private $stack = [];

/**
* @var string
*/
private $key;

/**
* OpensslStack constructor.
*
* @param string $passkey Password or key
*/
public function __construct(string $key)
{
$this->key = $key;
}

/**
* Add a new cipher/algo combo to the execution stack
*
* @param string $cipher Cipher mode to use
* @param string $algo Hashing algo to use
* @return OpensslStack
*/
public function add(string $cipher, string $algo): self
{
$this->stack[] = [$cipher, $algo];

return $this;
}

/**
* Encrypt data using custom stack
*
* @param string $data Data to encrypt
* @return string
*/
public function encrypt(string $data): string
{
foreach ($this->stack as $s) {
$data = OpensslStatic::encrypt($data, $this->key, $s[0], $s[1]);
}

return $data;
}

/**
* Decrypt data using custom stack
*
* @param string $data Data to decrypt
* @return string
*/
public function decrypt(string $data): string
{
foreach (\array_reverse($this->stack) as $s) {
$data = OpensslStatic::decrypt($data, $this->key, $s[0], $s[1]);
}

return $data;
}
}
<?php declare(strict_types=1);

/**
* OpensslStack.php
*
* PHP version 7
*
* @category Dcrypt
* @package Dcrypt
* @author Michael Meyer (mmeyer2k) <m.meyer2k@gmail.com>
* @license http://opensource.org/licenses/MIT The MIT License (MIT)
* @link https://github.com/mmeyer2k/dcrypt
*/

namespace Dcrypt;

/**
* A factory class to build and use custom encryption stacks.
*
* @category Dcrypt
* @package Dcrypt
* @author Michael Meyer (mmeyer2k) <m.meyer2k@gmail.com>
* @license http://opensource.org/licenses/MIT The MIT License (MIT)
* @link https://github.com/mmeyer2k/dcrypt
*/
class OpensslStack
{
/**
* Stack of cipher/algo combos
*
* @var array
*/
private $stack = [];

/**
* High entropy key
*
* @var string
*/
private $key;

/**
* OpensslStack constructor.
*
* @param string $key Password or key
*/
public function __construct(string $key)
{
$this->key = $key;
}

/**
* Add a new cipher/algo combo to the execution stack
*
* @param string $cipher Cipher mode to use
* @param string $algo Hashing algo to use
*
* @return OpensslStack
*/
public function add(string $cipher, string $algo): self
{
$this->stack[] = [$cipher, $algo];

return $this;
}

/**
* Encrypt data using custom stack
*
* @param string $data Data to encrypt
*
* @return string
*/
public function encrypt(string $data): string
{
foreach ($this->stack as $s) {
$data = OpensslStatic::encrypt($data, $this->key, $s[0], $s[1]);
}

return $data;
}

/**
* Decrypt data using custom stack
*
* @param string $data Data to decrypt
*
* @return string
*/
public function decrypt(string $data): string
{
foreach (\array_reverse($this->stack) as $s) {
$data = OpensslStatic::decrypt($data, $this->key, $s[0], $s[1]);
}

return $data;
}
}
2 changes: 2 additions & 0 deletions src/OpensslStatic.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ final class OpensslStatic extends OpensslWrapper
* @param string $key Key material
* @param string $cipher OpenSSL cipher name
* @param string $algo Hashing and key derivation algo name
*
* @return string
* @throws \Exception
*/
Expand Down Expand Up @@ -82,6 +83,7 @@ public static function decrypt(string $data, string $key, string $cipher, string
* @param string $key Key material
* @param string $cipher OpenSSL cipher name
* @param string $algo Hashing and key derivation algo name
*
* @return string
* @throws \Exception
*/
Expand Down
20 changes: 13 additions & 7 deletions src/OpensslWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,20 @@ class OpensslWrapper
/**
* OpenSSL encrypt wrapper function
*
* @param string $input Data to decrypt
* @param string $method Cipher method to use
* @param string $key Key string
* @param string $iv Initialization vector
* @param string $data Data to decrypt
* @param string $method Cipher method to use
* @param string $key Key string
* @param string $iv Initialization vector
* @param string $tag AAD tag
*
* @return string
*/
protected static function openssl_encrypt(string $input, string $method, string $key, string $iv, string &$tag): string
protected static function openssl_encrypt(string $data, string $method, string $key, string $iv, string &$tag): string
{
if (OpensslStatic::tagRequired($method)) {
return \openssl_encrypt($input, $method, $key, OPENSSL_RAW_DATA, $iv, $tag, '', 4);
return \openssl_encrypt($data, $method, $key, OPENSSL_RAW_DATA, $iv, $tag, '', 4);
} else {
return \openssl_encrypt($input, $method, $key, OPENSSL_RAW_DATA, $iv);
return \openssl_encrypt($data, $method, $key, OPENSSL_RAW_DATA, $iv);
}
}

Expand All @@ -50,6 +52,7 @@ protected static function openssl_encrypt(string $input, string $method, string
* @param string $method Cipher method to use
* @param string $key Key string
* @param string $iv Initialization vector
*
* @return string
*/
protected static function openssl_decrypt(string $input, string $method, string $key, string $iv, string $tag): string
Expand All @@ -65,6 +68,7 @@ protected static function openssl_decrypt(string $input, string $method, string
* Get IV size for specified CIPHER.
*
* @param string $cipher
*
* @return int
*/
protected static function ivSize(string $cipher): int
Expand All @@ -78,6 +82,7 @@ protected static function ivSize(string $cipher): int
* Get a correctly sized IV for the specified cipher
*
* @param string $cipher
*
* @return string
* @throws \Exception
*/
Expand All @@ -96,6 +101,7 @@ protected static function ivGenerate(string $cipher): string
* Determines if the provided cipher requires a tag
*
* @param string $cipher
*
* @return bool
*/
protected static function tagRequired(string $cipher): bool
Expand Down
11 changes: 7 additions & 4 deletions src/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ final class Str
*
* @param string $known The string of known length to compare against
* @param string $given The string that the user can control
*
* @return bool
*/
public static function equal(string $known, string $given): bool
Expand All @@ -53,8 +54,9 @@ public static function equal(string $known, string $given): bool

/**
* Determine the length of the output of a given hash algorithm in bytes.
*
*
* @param string $algo Name of algorithm to look up
*
* @return int
*/
public static function hashSize(string $algo): int
Expand All @@ -66,6 +68,7 @@ public static function hashSize(string $algo): int
* Returns the number of bytes in a string.
*
* @param string $string The string whose length we wish to obtain
*
* @return int
*/
public static function strlen(string $string): int
Expand All @@ -77,10 +80,10 @@ public static function strlen(string $string): int
* Returns part of a string.
*
* @param string $string The string whose length we wish to obtain
* @param int $start
* @param int $length
* @param int $start Offset to start gathering output
* @param int $length Distance from starting offset to gather
*
* @return string the extracted part of string; or FALSE on failure, or an empty string.
* @return string
*/
public static function substr(string $string, int $start, int $length = null): string
{
Expand Down

0 comments on commit 445017f

Please sign in to comment.