Skip to content

Test #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Build

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
php-versions: ['8.1', '8.2', '8.3']

steps:
- name: Checkout source
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
ini-values: memory_limit=2048M

- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ matrix.php-versions }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ matrix.php-versions }}-composer-

- name: Install dependencies
run: composer install --prefer-dist

- name: Check code standards
run: vendor/bin/phpcs --standard=PSR1,PSR2 -n src

- name: Run useragent string tests
run: php bin/runner.php --show check

- name: Run unit tests
run: vendor/bin/phpunit --no-coverage tests/unit
35 changes: 35 additions & 0 deletions source/AlnumVigenereCipher.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
namespace amculin\cryptography\classic;

use amculin\cryptography\classic\enums\ProcessType;
use amculin\cryptography\classic\exceptions\InvalidAlnumException;

/**
* This file is the main class for alpha-numric mode vigenere cipher algortithm
*
Expand All @@ -17,4 +20,36 @@ class AlnumVigenereCipher extends VigenereCipherBlueprint
* @var string
*/
public $tabulaRecta = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

public function isValid(): bool
{
try {
$pattern = '/^[a-zA-Z0-9]*$/';
$isValid = preg_match($pattern, $this->key);

if (! $isValid) {
throw new InvalidAlnumException('Key');
}

if ($this->process == ProcessType::ENCRYPT->value) {
$isValid = preg_match($pattern, $this->plainText) && $isValid;

if (! $isValid) {
throw new InvalidAlnumException('Plain text');
}
} else {
$isValid = preg_match($pattern, $this->cipherText) && $isValid;

if (! $isValid) {
throw new InvalidAlnumException('Cipher text');
}
}
} catch(InvalidAlnumException $e) {
echo $e->errorMessage();

return false;
}

return true;
}
}
35 changes: 35 additions & 0 deletions source/BasicVigenereCipher.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
namespace amculin\cryptography\classic;

use amculin\cryptography\classic\enums\ProcessType;
use amculin\cryptography\classic\exceptions\InvalidBasicException;

/**
* This file is the main class for basic vigenere cipher algortithm
*
Expand All @@ -14,4 +17,36 @@ class BasicVigenereCipher extends VigenereCipherBlueprint
* @inheritdoc
*/
public $tabulaRecta = 'abcdefghijklmnopqrstuvwxyz';

public function isValid(): bool
{
try {
$pattern = '/^[a-z]*$/';
$isValid = preg_match($pattern, $this->key);

if (! $isValid) {
throw new InvalidBasicException('Key');
}

if ($this->process == ProcessType::ENCRYPT->value) {
$isValid = preg_match($pattern, $this->plainText) && $isValid;

if (! $isValid) {
throw new InvalidBasicException('Plain text');
}
} else {
$isValid = preg_match($pattern, $this->cipherText) && $isValid;

if (! $isValid) {
throw new InvalidBasicException('Cipher text');
}
}
} catch(InvalidBasicException $e) {
echo $e->errorMessage();

return false;
}

return true;
}
}
4 changes: 2 additions & 2 deletions source/VigenereCipher.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static function getClassName(string $mode): string
}
}

public static function encrypt(string $data, string $key, string $mode = 'basic'): string
public static function encrypt(string $data, string $key, string $mode = 'basic'): string|null
{
$className = self::getClassName($mode);

Expand All @@ -28,7 +28,7 @@ public static function encrypt(string $data, string $key, string $mode = 'basic'
return $encrypt->getCipherText();
}

public static function decrypt(string $data, string $key, string $mode = 'basic'): string
public static function decrypt(string $data, string $key, string $mode = 'basic'): string|null
{
$className = self::getClassName($mode);

Expand Down
16 changes: 12 additions & 4 deletions source/VigenereCipherBlueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,25 @@ public function __construct(string $process = 'encrypt', string $data = null, st
if (! is_null($data) && ! is_null($key)) {
$this->setPlainText($data);
$this->setKey($key);
$this->encrypt();

if ($this->isValid()) {
$this->encrypt();
}
}
} else {
if (! is_null($data) && ! is_null($key)) {
$this->setCipherText($data);
$this->setKey($key);
$this->decrypt();

if ($this->isValid()) {
$this->decrypt();
}
}
}
}

abstract public function isValid(): bool;

/**
* Method to get current process
*
Expand All @@ -86,7 +94,7 @@ public function setProcess(string $process): void
*
* @return string
*/
public function getPlainText(): string
public function getPlainText(): string|null
{
return $this->plainText;
}
Expand Down Expand Up @@ -130,7 +138,7 @@ public function setKey(string $key): void
*
* @return string
*/
public function getCipherText(): string
public function getCipherText(): string|null
{
return $this->cipherText;
}
Expand Down
12 changes: 12 additions & 0 deletions source/exceptions/InvalidAlnumException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
namespace amculin\cryptography\classic\exceptions;

class InvalidAlnumException extends \Exception
{
public function errorMessage() {
$message = 'Error on line ' . $this->getLine() . ' in ' . $this->getFile() . ': '. PHP_EOL;
$message .= $this->getMessage() . ' is invalid, must be combination of a-z, A-Z, and 0-9!' . PHP_EOL;

return $message;
}
}
12 changes: 12 additions & 0 deletions source/exceptions/InvalidBasicException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
namespace amculin\cryptography\classic\exceptions;

class InvalidBasicException extends \Exception
{
public function errorMessage() {
$message = 'Error on line ' . $this->getLine() . ' in ' . $this->getFile() . ': '. PHP_EOL;
$message .= $this->getMessage() . ' is invalid, must be combination of a-z!' . PHP_EOL;

return $message;
}
}
57 changes: 50 additions & 7 deletions tests/VIgenereCipherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

final class VigenereCipherTest extends TestCase
{
public function testCanEncryptWithBasicMode():void
const BASIC_ALLOWED_CHARS = '/[a-z]/';
const ALNUM_ALLOWED_CHARS = '/[a-zA-Z0-9]/';

public function testCanEncryptInBasicMode():void
{
$allowedChars = '/[a-z]/';
$allowedChars = $this::BASIC_ALLOWED_CHARS;
$data = 'encryptionprocess';
$key = 'thekey';

Expand All @@ -25,9 +28,19 @@ public function testCanEncryptWithBasicMode():void
$this->assertMatchesRegularExpression($allowedChars, $encrypted);
}

public function testCanDecryptWithBasicMode():void
public function testCanNotEncryptInBasicModeWithInvalidKey():void
{
$data = 'encryptionprocess';
$key = 'thekey-';

$encrypted = VigenereCipher::encrypt($data, $key, VigenereMode::BASIC->value);

$this->assertNull($encrypted);
}

public function testCanDecryptInBasicMode():void
{
$allowedChars = '/[a-z]/';
$allowedChars = $this::BASIC_ALLOWED_CHARS;
$data = 'xugbcnmpsxtphjicw';
$key = 'thekey';

Expand All @@ -44,9 +57,19 @@ public function testCanDecryptWithBasicMode():void
$this->assertMatchesRegularExpression($allowedChars, $decrypted);
}

public function testCanEncryptWithAlphaNumericMode():void
public function testCanNotDecryptInBasicModeWithInvalidKey():void
{
$data = 'xugbcnmpsxtphjicw';
$key = 'thekey-';

$encrypted = VigenereCipher::decrypt($data, $key, VigenereMode::BASIC->value);

$this->assertNull($encrypted);
}

public function testCanEncryptInAlphaNumericMode():void
{
$allowedChars = '/[a-zA-Z0-9]/';
$allowedChars = $this::ALNUM_ALLOWED_CHARS;
$data = 'Encrypti0nProC3s5';
$key = 'th3kEy';

Expand All @@ -63,9 +86,19 @@ public function testCanEncryptWithAlphaNumericMode():void
$this->assertMatchesRegularExpression($allowedChars, $encrypted);
}

public function testCanNotEncryptInAlphaNumericModeWithInvalidKey():void
{
$data = 'Encrypti0nProC3s5';
$key = 'th3kEy-';

$encrypted = VigenereCipher::encrypt($data, $key, VigenereMode::ALPHA_NUMERIC->value);

$this->assertNull($encrypted);
}

public function testCanDecryptWithAlphaNumericMode():void
{
$allowedChars = '/[a-zA-Z0-9]/';
$allowedChars = $this::ALNUM_ALLOWED_CHARS;
$data = 'Xu5B2NMpTxjPHJWCz';
$key = 'th3kEy';

Expand All @@ -82,6 +115,16 @@ public function testCanDecryptWithAlphaNumericMode():void
$this->assertMatchesRegularExpression($allowedChars, $decrypted);
}

public function testCanNotDecryptInAlphaNumericModeWithInvalidKey():void
{
$data = 'Xu5B2NMpTxjPHJWCz';
$key = 'th3kEy-';

$encrypted = VigenereCipher::decrypt($data, $key, VigenereMode::ALPHA_NUMERIC->value);

$this->assertNull($encrypted);
}

public function testCanGetBasicVigenereClass(): void
{
$path = 'amculin\cryptography\classic\\';
Expand Down
Loading