Created
June 22, 2014 21:23
-
-
Save pascalduez/1bc449bcfa9aacefb372 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) | |
| // ---- | |
| // Return a charset string. | |
| // -------- | |
| // @param [string] $charset | |
| // -------- | |
| // @return [string] | |
| @function charset( | |
| $charset | |
| ) { | |
| @return map-get(( | |
| numeric: | |
| '0123456789', | |
| alphabetic: | |
| 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', | |
| alphanumeric: | |
| '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 range `$charset`. | |
| // -------- | |
| // @param [string] $str | |
| // @param [number] $index | |
| // @param [string] $charset | |
| // -------- | |
| // @return [number] | |
| @function char-code-at( | |
| $str, | |
| $index: 1, | |
| $charset: alphabetic | |
| ) { | |
| $chars: charset($charset); | |
| @return str-index($chars, char-at($str, $index)); | |
| } | |
| // Get char at `$index` in range `$charset`. | |
| // -------- | |
| // @param [number] $index | |
| // @param [string] $charset | |
| // -------- | |
| // @return [string] | |
| @function from-char-code( | |
| $index, | |
| $charset: alphabetic | |
| ) { | |
| $chars: charset($charset) or $charset; | |
| @return char-at($chars, $index); | |
| } | |
| // Return a filtered string. | |
| // -------- | |
| // @param [string] $str | |
| // -------- | |
| // @return [string] | |
| @function str-filter( | |
| $str, | |
| $charset: alphabetic | |
| ) { | |
| $chars: charset($charset); | |
| $result: ''; | |
| @for $i from 1 through str-length($str) { | |
| $c: char-at($str, $i); | |
| @if str-index($chars, $c) { | |
| $result: $result + $c; | |
| } | |
| } | |
| @return $result; | |
| } | |
| // Returns a `key autokey` key. | |
| // -------- | |
| // @param [string] $key | |
| // @param [string] $message | |
| // -------- | |
| // @return [string] | |
| @function key-autokey( | |
| $key, | |
| $message | |
| ) { | |
| $key: str-filter($key); | |
| $message: str-filter($message); | |
| $length: str-length($message); | |
| $result: ''; | |
| @while str-length($result) < $length { | |
| $result: $result + $key; | |
| } | |
| @return str-slice($result, 1, $length); | |
| } | |
| // Returns a `text autokey` key. | |
| // -------- | |
| // @param [string] $key | |
| // @param [string] $message | |
| // -------- | |
| // @return [string] | |
| @function text-autokey( | |
| $key, | |
| $message | |
| ) { | |
| $key: str-filter($key); | |
| $message: str-filter($message); | |
| $length: str-length($message); | |
| $result: $key + $message; | |
| @return str-slice($result, 1, $length); | |
| } | |
| // Filter and build a numeric key in range [0-25]. | |
| // -------- | |
| // @param [string] $key | |
| // -------- | |
| // @return [list] | |
| @function key( | |
| $key | |
| ) { | |
| $result: (); | |
| @for $i from 1 through str-length($key) { | |
| $c: char-code-at($key, $i); | |
| @if $c { | |
| $result: append($result, ($c - 1) % 26); | |
| } | |
| } | |
| @return $result; | |
| } | |
| // Vigenère cipher encoder/decoder. | |
| // -------- | |
| // @param [string] $message | |
| // @param [list] $key | |
| // -------- | |
| // @return [string] | |
| @function vigenere( | |
| $message, | |
| $key | |
| ) { | |
| $length: length($key); | |
| $result: ''; | |
| $j: 1; | |
| @for $i from 1 through str-length($message) { | |
| $col: char-code-at($message, $i); | |
| $match: ''; | |
| @if $col { | |
| // Uppercase $col <= 26 | |
| // Lowercase $col > 26 | |
| $pos: if($col > 26, 27, 1); | |
| $match: (($col - $pos) + nth($key, (($j - 1) % $length + 1))) % 26 + $pos; | |
| $match: from-char-code($match); | |
| $j: $j + 1; | |
| } | |
| @else { | |
| // Non [A-Za-z] char. | |
| $match: str-slice($message, $i, $i); | |
| } | |
| $result: $result + $match; | |
| } | |
| @return $result; | |
| } | |
| // Encrypt a `$message` with the vigenère cipher based on `$key`. | |
| // -------- | |
| // @param [string] $message | |
| // @param [string] $key | |
| // -------- | |
| // @return [string] | |
| @function vigenere-encrypt( | |
| $message, | |
| $key | |
| ) { | |
| $key: text-autokey($key, $message); | |
| @return vigenere($message, key($key)); | |
| } | |
| // Decrypt a vigenère encrypted `$message` based on `$key`. | |
| // -------- | |
| // @param [string] $message | |
| // @param [string] $key | |
| // -------- | |
| // @return [string] | |
| @function vigenere-decrypt( | |
| $message, | |
| $key | |
| ) { | |
| $result: ''; | |
| //$key: text-autokey($key, $message); | |
| $key: key($key); | |
| @for $i from 1 through length($key) { | |
| $key: set-nth($key, $i, (26 - nth($key, $i) % 26)); | |
| } | |
| @for $i from 1 through str-length($message) { | |
| $c: char-at($message, $i); | |
| $t: vigenere($c, nth($key, $i)); | |
| $key: append($key, key($t)); | |
| $result: $result + $t; | |
| } | |
| @return $result; | |
| } | |
| tests { | |
| t: vigenere-encrypt('amiens', 'gef'); | |
| t: vigenere-decrypt('gqneza', 'gef'); | |
| } |
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
| tests { | |
| t: "gqneza"; | |
| t: "amieli"; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment