Created
June 18, 2014 19:18
-
-
Save pascalduez/0f5daa44f6456eb8c6f3 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
This file contains hidden or 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
| // ---- | |
| // Sass (v3.3.8) | |
| // Compass (v1.0.0.alpha.19) | |
| // ---- | |
| // Keyed Caesar cipher | |
| // https://en.wikipedia.org/wiki/Caesar_cipher | |
| // http://rumkin.com/tools/cipher/caesar-keyed.php | |
| // Test whether `$value` is containedd between `$min` and `$max`. | |
| // -------- | |
| // @param [number] $min: minimum range value | |
| // @param [number] $max: maximum range value | |
| // @param [number] $value: the value to be tested | |
| // -------- | |
| // @return [boolean] | |
| @function is-in-range($min, $max, $value) { | |
| @return ($value >= $min) and ($value <= $max); | |
| } | |
| // Return a charset string. | |
| // -------- | |
| // @param [string] $charset: alpha | alphanum | ascii | |
| // -------- | |
| // @return [string] | |
| @function charset($charset) { | |
| @return map-get(( | |
| alpha: | |
| 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', | |
| alphanum: | |
| '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', | |
| ascii: | |
| '!"#$%&' + "'" + | |
| '()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~' | |
| ), $charset); | |
| } | |
| // Get char at `$index` in `$str`. | |
| // -------- | |
| // @param [string] $str | |
| // @param [number] $index | |
| // -------- | |
| // @return [string] | |
| @function char-at($str, $index: 1) { | |
| @return str-slice($str, $index, $index); | |
| } | |
| // Get char code from `$str` at `$index` in `$charset`. | |
| // -------- | |
| // @param [string] $str | |
| // @param [number] $index | |
| // @param [string] $charset | |
| // -------- | |
| // @return [number] | |
| @function char-code-at($str, $index: 1, $charset: alpha) { | |
| $chars: charset($charset); | |
| @return str-index($chars, char-at($str, $index)); | |
| } | |
| // Get char at `$index` in `$charset`. | |
| // -------- | |
| // @param [number] $index | |
| // @param [string] $charset | |
| // -------- | |
| // @return [string] | |
| @function from-char-code($index, $charset: alpha) { | |
| $chars: if(index((alpha alphanum ascii), $charset), charset($charset), $charset); | |
| @return char-at($chars, $index); | |
| } | |
| // Return a keyed alphabet. | |
| // -------- | |
| // @param [string] $key | |
| // -------- | |
| // @return [string] | |
| @function keyed-alpha($key) { | |
| $alpha: str-slice(charset(alpha), -26); | |
| $key: to-lower-case($key) + $alpha; | |
| $result: ''; | |
| @for $i from 1 through str-length($key) { | |
| $c: char-at($key, $i); | |
| @if str-index($alpha, $c) and not str-index($result, $c) { | |
| $result: $result + $c; | |
| } | |
| } | |
| @return to-upper-case($result) + $result; | |
| } | |
| // Caesar cipher encoder/decoder. | |
| // -------- | |
| // @param [string] $message | |
| // @param [number] $shift | |
| // @param [string] $op: encrypt | decrypt | |
| // @param [string] $charset | |
| // -------- | |
| // @return [string] | |
| @function caesar( | |
| $message, | |
| $shift: 1, | |
| $op: 'encrypt', | |
| $charset: alpha | |
| ) { | |
| @if type-of($shift) != number { | |
| @warn '`$shift` must be a number, type-of($shift) given.'; | |
| } | |
| @if not is-in-range(0, 26, abs($shift)) { | |
| @warn '`$shift` must in range [0-26].'; | |
| } | |
| $result: ''; | |
| @if $op == 'decrypt' { | |
| $shift: (26 - $shift); | |
| } | |
| @for $i from 1 through str-length($message) { | |
| $code: char-code-at($message, $i); | |
| @if $code { | |
| // Uppercase $col <= 26 | |
| // Lowercase $col > 26 | |
| $start: if($code > 26, 27, 1); | |
| $match: ((($code - $start) + $shift) % 26) + $start; | |
| $result: $result + from-char-code($match, $charset); | |
| } | |
| @else { | |
| // Non [A-Za-z] char. | |
| $result: $result + char-at($message, $i); | |
| } | |
| } | |
| @return $result; | |
| } | |
| // Keyed Caesar cipher encoder/decoder. | |
| // -------- | |
| // @param [string] $message | |
| // @param [string] $key | |
| // @param [number] $shift | |
| // @param [string] $op: encrypt | decrypt | |
| // -------- | |
| // @return [string] | |
| @function caesar-keyed( | |
| $message, | |
| $key, | |
| $shift: 1, | |
| $op: 'encrypt' | |
| ) { | |
| $charset: keyed-alpha($key); | |
| @return caesar($message, $shift, $op, $charset); | |
| } | |
| // Tests | |
| caesar { | |
| $fixture: ( | |
| ('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', 'berlin', 13, 'KMOPQSTUVWXYZBERLINACDFGHJkmopqstuvwxyzberlinacdfghj'), | |
| ('Caesar cipher', 'Amiens', 3, 'Secveu shrgcu'), | |
| ('Keyed Caesar cipher', 'Cayeux', 6, 'Nhuhg Fbhwbv flskhv') | |
| ); | |
| /* Encrypt */ | |
| $tests: ''; | |
| @each $test in $fixture { | |
| $pass: caesar-keyed(nth($test, 1), nth($test, 2), nth($test, 3)) == nth($test, 4); | |
| $tests: $tests + if($pass, '✔ ', 'X '); | |
| } | |
| tests: $tests; | |
| /* Decrypt */ | |
| $tests: ''; | |
| @each $test in $fixture { | |
| $pass: caesar-keyed(nth($test, 4), nth($test, 2), nth($test, 3), 'decrypt') == nth($test, 1); | |
| $tests: $tests + if($pass, '✔ ', 'X '); | |
| } | |
| tests: $tests; | |
| t: caesar-keyed('a', 'b', 3); | |
| t: caesar-keyed('d', 'b', 3, 'decrypt'); | |
| } |
This file contains hidden or 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
| @charset "UTF-8"; | |
| caesar { | |
| /* Encrypt */ | |
| tests: "✔ ✔ ✔ "; | |
| /* Decrypt */ | |
| tests: "X X X "; | |
| t: "d"; | |
| t: "b"; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment