micheleriva / caesar-cipher

πŸ‘‘ Dead Simple Caesar Cipher written in Haskell

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Caesar Cipher

s i m p l e Β· h a s k e l l Β· i m p l e m e n t a t i o n


My goal with this project was to build a Caesar Cipher with the minimum effort.
Why? Because I strongly believe that an awesome language such as Haskell, has the potential to transform the following verbose code in something beautiful:

<?php
function encrypt($str, $offset) {
    $encrypted_text = "";
    $offset = $offset % 26;
    if($offset < 0) {
        $offset += 26;
    }
    $i = 0;
    while($i < strlen($str)) {
        $c = strtoupper($str{$i}); 
        if(($c >= "A") && ($c <= 'Z')) {
            if((ord($c) + $offset) > ord("Z")) {
                $encrypted_text .= chr(ord($c) + $offset - 26);
        } else {
            $encrypted_text .= chr(ord($c) + $offset);
        }
      } else {
          $encrypted_text .= " ";
      }
      $i++;
    }
    return $encrypted_text;
}

function decrypt($str, $offset) {
    $decrypted_text = "";
    $offset = $offset % 26;
    if($offset < 0) {
        $offset += 26;
    }
    $i = 0;
    while($i < strlen($str)) {
        $c = strtoupper($str{$i}); 
        if(($c >= "A") && ($c <= 'Z')) {
            if((ord($c) - $offset) < ord("A")) {
                $decrypted_text .= chr(ord($c) - $offset + 26);
        } else {
            $decrypted_text .= chr(ord($c) - $offset);
        }
      } else {
          $decrypted_text .= " ";
      }
      $i++;
    }
    return $decrypted_text;
}

why the hwem (caesar 2) should I write such an horrible php code when there's Haskell?

License

MIT

About

πŸ‘‘ Dead Simple Caesar Cipher written in Haskell

License:MIT License


Languages

Language:Haskell 100.0%