-
-
Notifications
You must be signed in to change notification settings - Fork 469
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into SentinelSearch
- Loading branch information
Showing
32 changed files
with
1,380 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,12 +8,12 @@ jobs: | |
name: DIRECTORY.md | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v1 # v2 is broken for git diff | ||
- uses: actions/setup-python@v2 | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-python@v5 | ||
- name: Setup Git Specs | ||
run: | | ||
git config --global user.name github-actions | ||
git config --global user.email '${GITHUB_ACTOR}@users.noreply.github.com' | ||
git config --global user.name "$GITHUB_ACTOR" | ||
git config --global user.email "$[email protected]" | ||
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY | ||
- name: Update DIRECTORY.md | ||
shell: python | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
FROM gitpod/workspace-full:2022-05-08-14-31-53 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
image: | ||
file: .gitpod.Dockerfile | ||
|
||
tasks: | ||
- init: | | ||
echo "Welcome to TheAlgorithms/PHP" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/** | ||
* Encrypt a message using the Atbash Cipher. | ||
* The Atbash Cipher is a simple substitution cipher where each letter in the plaintext is | ||
* replaced with its corresponding letter from the end of the alphabet (reverse alphabet). | ||
* Non-alphabet characters are not modified. | ||
* | ||
* @param string $plainText The plaintext to encrypt. | ||
* @return string The encrypted message. | ||
*/ | ||
function atbash_encrypt($plainText) | ||
{ | ||
$result = ''; | ||
$plainText = strtoupper($plainText); | ||
for ($i = 0; $i < strlen($plainText); $i++) { | ||
$char = $plainText[$i]; | ||
if (ctype_alpha($char)) { | ||
$offset = ord('Z') - ord($char); | ||
$encryptedChar = chr(ord('A') + $offset); | ||
} else { | ||
$encryptedChar = $char; // Non-alphabet characters remain unchanged | ||
} | ||
$result .= $encryptedChar; | ||
} | ||
return $result; | ||
} | ||
|
||
/** | ||
* Decrypt a message encrypted using the Atbash Cipher. | ||
* Since the Atbash Cipher is its own inverse, decryption is the same as encryption. | ||
* | ||
* @param string $cipherText The ciphertext to decrypt. | ||
* @return string The decrypted message. | ||
*/ | ||
function atbash_decrypt($cipherText) | ||
{ | ||
return atbash_encrypt($cipherText); // Decryption is the same as encryption | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
/** | ||
* Encode a message using the Rail Fence Cipher. | ||
* (https://en.wikipedia.org/wiki/Rail_fence_cipher) | ||
* | ||
* @param string $plainMessage The message to encode. | ||
* @param int $rails The number of rails or rows in the rail fence. | ||
* | ||
* @return string The encoded message. | ||
*/ | ||
function Railencode($plainMessage, $rails): string | ||
{ | ||
$cipherMessage = []; | ||
$position = ($rails * 2) - 2; | ||
// Iterate through the characters of the plain message | ||
for ($index = 0; $index < strlen($plainMessage); $index++) { | ||
for ($step = 0; $step < $rails; $step++) { | ||
if (!isset($cipherMessage[$step])) { | ||
$cipherMessage[$step] = ''; | ||
} | ||
// Check if the character should go in the rail | ||
if ($index % $position == $step || $index % $position == $position - $step) { | ||
$cipherMessage[$step] .= $plainMessage[$index]; | ||
} else { | ||
// Add a placeholder for empty spaces | ||
$cipherMessage[$step] .= "."; | ||
} | ||
} | ||
} | ||
// Combine and remove placeholders to form the cipher message | ||
return implode('', str_replace('.', '', $cipherMessage)); | ||
} | ||
|
||
/** | ||
* Decode a message encoded using the Rail Fence Cipher. | ||
* | ||
* @param string $cipherMessage The encoded message. | ||
* @param int $rails The number of rails or rows used for encoding. | ||
* | ||
* @return string The decoded plain message. | ||
*/ | ||
function Raildecode($cipherMessage, $rails): string | ||
{ | ||
$position = ($rails * 2) - 2; | ||
$textLength = strlen($cipherMessage); | ||
$minLength = floor($textLength / $position); | ||
$balance = $textLength % $position; | ||
$lengths = []; | ||
$strings = []; | ||
$totalLengths = 0; | ||
// Calculate the number of characters in each row | ||
for ($rowIndex = 0; $rowIndex < $rails; $rowIndex++) { | ||
$lengths[$rowIndex] = $minLength; | ||
if ($rowIndex != 0 && $rowIndex != ($rails - 1)) { | ||
$lengths[$rowIndex] += $minLength; | ||
} | ||
if ($balance > $rowIndex) { | ||
$lengths[$rowIndex]++; | ||
} | ||
if ($balance > ($rails + ($rails - $rowIndex) - 2)) { | ||
$lengths[$rowIndex]++; | ||
} | ||
$strings[] = substr($cipherMessage, $totalLengths, $lengths[$rowIndex]); | ||
$totalLengths += $lengths[$rowIndex]; | ||
} | ||
// Convert the rows of characters to plain message | ||
$plainText = ''; | ||
while (strlen($plainText) < $textLength) { | ||
for ($charIndex = 0; $charIndex < $position; $charIndex++) { | ||
if (isset($strings[$charIndex])) { | ||
$index = $charIndex; | ||
} else { | ||
$index = $position - $charIndex; | ||
} | ||
$plainText .= substr($strings[$index], 0, 1); | ||
$strings[$index] = substr($strings[$index], 1); | ||
} | ||
} | ||
return $plainText; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
/** | ||
* Encrypts a plaintext using the Vigenère cipher. | ||
* (https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher) | ||
* | ||
* @param string $plaintext The plaintext to be encrypted. | ||
* @param string $key The encryption key. | ||
* @return string The encrypted text. | ||
*/ | ||
function vigenere_encrypt($plaintext, $key): string | ||
{ | ||
// Convert the input to uppercase for consistency | ||
$plaintext = strtoupper($plaintext); | ||
$key = strtoupper($key); | ||
$keyLength = strlen($key); | ||
$encryptedText = ""; | ||
for ($i = 0; $i < strlen($plaintext); $i++) { | ||
$char = $plaintext[$i]; | ||
if (ctype_alpha($char)) { | ||
// Calculate the shift based on the key | ||
$shift = ord($key[$i % $keyLength]) - ord('A'); | ||
// Apply the Vigenère encryption formula | ||
$encryptedChar = chr(((ord($char) - ord('A') + $shift) % 26) + ord('A')); | ||
// Append the encrypted character to the result | ||
$encryptedText .= $encryptedChar; | ||
} else { | ||
// If the character is not alphabetic, leave it unchanged | ||
$encryptedText .= $char; | ||
} | ||
} | ||
return $encryptedText; | ||
} | ||
|
||
/** | ||
* Decrypts a ciphertext using the Vigenère cipher. | ||
* | ||
* @param string $ciphertext The ciphertext to be decrypted. | ||
* @param string $key The decryption key. | ||
* @return string The decrypted text. | ||
*/ | ||
function vigenere_decrypt($ciphertext, $key): string | ||
{ | ||
$ciphertext = strtoupper($ciphertext); | ||
$key = strtoupper($key); | ||
$keyLength = strlen($key); | ||
$decryptedText = ""; | ||
for ($i = 0; $i < strlen($ciphertext); $i++) { | ||
$char = $ciphertext[$i]; | ||
if (ctype_alpha($char)) { | ||
// Calculate the shift based on the key | ||
$shift = ord($key[$i % $keyLength]) - ord('A'); | ||
// Apply the Vigenère decryption formula | ||
$decryptedChar = chr(((ord($char) - ord('A') - $shift + 26) % 26) + ord('A')); | ||
// Append the decrypted character to the result | ||
$decryptedText .= $decryptedChar; | ||
} else { | ||
// If the character is not alphabetic, leave it unchanged | ||
$decryptedText .= $char; | ||
} | ||
} | ||
return $decryptedText; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.