- chr: documentation ( source)
- preg_match: documentation ( source)
- ctype_upper: documentation ( source)
- ord: documentation ( source)
<?php
class Model {
/**
* Calculate the encryption give a static OTP
*/
public static function encrypt($string) {
// Static pad
$pad = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmno';
$encrypted_string = "";
for ($i = 0; $i < strlen($string); $i++) {
$char = $string[$i];
if ($i < strlen($pad) && preg_match("/[a-z]/i", $char)) {
$shift_char = $pad[$i];
$ord_shift = ctype_upper($shift_char) ? 65 : 97;
$shift_amount = ord($shift_char) - $ord_shift;
if ($shift_amount) {
$ord_char = ord($char) + $shift_amount;
while ($ord_char > $ord_shift + 26) {
$ord_char -= 26;
}
$char = chr($ord_char);
}
}
$encrypted_string .= $char;
}
return $encrypted_string;
}
}
echo Model::encrypt('thiS is a test');