Skip to content

Test #15

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

Merged
merged 4 commits into from
Feb 27, 2025
Merged

Test #15

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
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ jobs:

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

- name: Run PHPStan
run: vendor/bin/phpstan analyse -l 10 source/ tests/

- name: Run unit tests
run: vendor/bin/phpunit --no-coverage tests/
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
}
},
"require-dev": {
"phpstan/phpstan": "^2.1",
"phpunit/phpunit": "^11.5"
}
}
60 changes: 59 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 54 additions & 15 deletions source/AlnumVigenereCipher.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,77 @@
* This file is the main class for alpha-numric mode vigenere cipher algortithm
*
* @author Fahmi Auliya Tsani <amixcustomlinux@gmail.com>
* @version 0.1
* @version 1.1
*/

class AlnumVigenereCipher extends VigenereCipherBlueprint
{
public function __construct(
public string $data,
public string $key,
public string $process = 'encrypt'
) {
$this->process = $process;
$this->tabulaRecta = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

if ($process == ProcessType::ENCRYPT->value) {
$this->plainText = $data;
$this->key = $this->generateKey($key);

if ($this->isValid()) {
$this->encrypt();
}
} else {
$this->cipherText = $data;
$this->key = $this->generateKey($key);

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

/**
* Default list of acceptable character to be used in vigenere cipher algorithm
* This list cointains alpha-numeric characters including the capitalized
*
* @var string
* @inheritdoc
*/
public $tabulaRecta = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
public function isValidKey(string $pattern): bool
{
return preg_match($pattern, $this->key) == 1;
}

/**
* @inheritdoc
*/
public function isValidPlainText(string $pattern): bool
{
return preg_match($pattern, $this->plainText) == 1;
}

/**
* @inheritdoc
*/
public function isValidCipherText(string $pattern): bool
{
return preg_match($pattern, $this->cipherText) == 1;
}

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

if (! $isValid) {
if (! $this->isValidKey($pattern)) {
throw new InvalidAlnumException('Key');
}

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

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

if (! $isValid) {
if (! $this->isValidCipherText($pattern)) {
throw new InvalidAlnumException('Cipher text');
}
}
Expand All @@ -50,6 +88,7 @@ public function isValid(): bool
return false;
}

$this->setIsValid(true);
return true;
}
}
66 changes: 55 additions & 11 deletions source/BasicVigenereCipher.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,78 @@
* This file is the main class for basic vigenere cipher algortithm
*
* @author Fahmi Auliya Tsani <amixcustomlinux@gmail.com>
* @version 0.2
* @version 1.1
*/

#[\AllowDynamicProperties]
class BasicVigenereCipher extends VigenereCipherBlueprint
{
public function __construct(
public string $data,
public string $key,
public string $process = 'encrypt'
) {
$this->process = $process;
$this->tabulaRecta = 'abcdefghijklmnopqrstuvwxyz';

if ($process == ProcessType::ENCRYPT->value) {
$this->plainText = $data;
$this->key = $this->generateKey($key);

if ($this->isValid()) {
$this->encrypt();
}
} else {
$this->cipherText = $data;
$this->key = $this->generateKey($key);

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

/**
* @inheritdoc
*/
public $tabulaRecta = 'abcdefghijklmnopqrstuvwxyz';
public function isValidKey(string $pattern): bool
{
return preg_match($pattern, $this->key) == 1;
}

/**
* @inheritdoc
*/
public function isValidPlainText(string $pattern): bool
{
return preg_match($pattern, $this->plainText) == 1;
}

/**
* @inheritdoc
*/
public function isValidCipherText(string $pattern): bool
{
return preg_match($pattern, $this->cipherText) == 1;
}

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

if (! $isValid) {
if (! $this->isValidKey($pattern)) {
throw new InvalidBasicException('Key');
}

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

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

if (! $isValid) {
if (! $this->isValidCipherText($pattern)) {
throw new InvalidBasicException('Cipher text');
}
}
Expand All @@ -47,6 +89,8 @@ public function isValid(): bool
return false;
}

$this->setIsValid(true);

return true;
}
}
24 changes: 14 additions & 10 deletions source/VigenereCipher.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace amculin\cryptography\classic;

use amculin\cryptography\classic\VigenereCipherBlueprint;
use amculin\cryptography\classic\enums\ProcessType;
use amculin\cryptography\classic\enums\VigenereMode;

Expand All @@ -9,33 +10,36 @@ class VigenereCipher
public static function getClassName(string $mode): string
{
$path = 'amculin\cryptography\classic\\';
$className = 'BasicVigenereCipher';

if ($mode == VigenereMode::BASIC->value) {
return $path . 'BasicVigenereCipher';
} elseif ($mode == VigenereMode::ALPHA_NUMERIC->value) {
return $path . 'AlnumVigenereCipher';
if ($mode == VigenereMode::ALPHA_NUMERIC->value) {
$className = 'AlnumVigenereCipher';
}

return $path . $className;
}

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

$processName = ProcessType::ENCRYPT->value;

$encrypt = new $className($processName, $data, $key);
/** @var VigenereCipherBlueprint $encrypt */
$encrypt = new $className($data, $key, $processName);

return $encrypt->getCipherText();
return $encrypt->cipherText;
}

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

$processName = ProcessType::DECRYPT->value;

$decrypt = new $className($processName, $data, $key);
/** @var VigenereCipherBlueprint $decrypt */
$decrypt = new $className($data, $key, $processName);

return $decrypt->getPlainText();
return $decrypt->plainText;
}
}
Loading
Loading