Skip to content

Instantly share code, notes, and snippets.

@robertdevore
Last active August 6, 2022 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertdevore/8bba7aef2f98f64f05878c15753aa9f3 to your computer and use it in GitHub Desktop.
Save robertdevore/8bba7aef2f98f64f05878c15753aa9f3 to your computer and use it in GitHub Desktop.
<?php
/**
* Example cipher function
*
* @param string $quote - the quote used for the cipher base
* @param string $secret - the secret message
* @param bool $with_symbols - add random symbols to the string
* @param bool $lowercase - should the quote have all lowercase lettering?
*
* @return string
*/
function acme_testing_cipher_idea( $quote, $secret, $with_symbols = true, $lowercase = true ) {
// Bail early?
if ( empty( $quote ) || empty( $secret ) ) {
return 'Cipher failed. Please include a string and message';
}
$quote = ucwords( $quote );
if ( $lowercase ) {
$quote = strtolower( $quote );
}
$quote = explode( ' ', $quote );
$quote = preg_replace( '/[^A-Za-z0-9]/', '', $quote );
$secret = str_shuffle( $secret );
$secret = str_split( $secret );
$randomized = array();
foreach ( $quote as $word ) {
$randomized[] = str_shuffle( $word );
}
$symbols = '!@#$%^&*()_+-={}[]:;,./?<>~`';
$final = array();
// Loop through randomized array.
for ( $i=0; $i < count( $randomized ); $i++ ) {
// Should we add symbols?
if ( $with_symbols ) {
$maybe_symbols = substr( str_shuffle( str_repeat( $symbols, rand( 1, 3 ) ) ), 0, rand( 1, 3 ) );
}
// Randomized word string.
$final[] = $randomized[$i];
// Maybe add symbols.
$final[] = $maybe_symbols;
// Add first letter from secret.
$final[] = $secret[$i];
}
// Remove spaces from cipher.
$cipher = join( '', $final );
// Randomize capitalization of letters in the cipher.
$cipher = acme_randomize_capitalization( $cipher );
return $cipher;
}
<?php
/**
* Randomly capitalize characters in provided string
*
* @param string $text - the string of text to apply random capitalization to.
*
* @return string
*/
function acme_randomize_capitalization( $text ) {
$firstpass = str_split( $text );
$outstring = "";
for ( $i = 0; $i < strlen( $text ); $i++ ) {
if ( $i == 0 ) {
$outstring[$i] = strtoupper( $firstpass[$i] );
} elseif ( ctype_upper( $outstring[$i-1] ) ) {
if ( mt_rand( 0, 3 ) >= 2 ) {
$outstring[$i] = strtoupper( $firstpass[$i] );
} else {
$outstring[$i] = strtolower( $firstpass[$i] );
}
} else {
if ( mt_rand( 0, 3 ) >= 2 ) {
$outstring[$i] = strtolower( $firstpass[$i] );
} else {
$outstring[$i] = strtoupper($firstpass[$i]);
}
}
}
return $outstring;
}
<?php
/**
* Example cipher usage
*
* @return string
*/
function acme_cipher_example_usage() {
$quote = 'Tilmaan: Waxaa jirta oraah ay kelmad walba xuruuftiisa isku ruxruxday, ka dibna kelmad kasta oo la ruxruxo u dhexaysa waa xaraf. Warqadahaas dheeriga ah waa waxa farriinta sirta ah ay tahay.';
$secret = 'hiddenmessage';
print_r( acme_testing_cipher_idea( $quote, $secret, true, false ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment