Skip to content

Instantly share code, notes, and snippets.

@pascalduez
Created June 12, 2014 20:26
Show Gist options
  • Save pascalduez/66d8663ab77792b648e3 to your computer and use it in GitHub Desktop.
Save pascalduez/66d8663ab77792b648e3 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
// ----
// Sass (v3.3.8)
// Compass (v1.0.0.alpha.19)
// SassyBitwise (v1.1.1)
// ----
@import "SassyBitwise";
// Work in progres...
// XOR cipher
// https://en.wikipedia.org/wiki/XOR_cipher
// JavaScript `toString` and `parseInt` implementation in Sass.
@function pow($num, $exp) {
$result: 1;
@if $exp > 0 {
@for $i from 1 through $exp {
$result: $result * $num;
}
}
@else {
@for $i from $exp to 0 {
$result: $result / $num;
}
}
@return $result;
}
@function chars-from-base($base: 10) {
$chars:(
// Binary
2: '01',
// Octal
8: '01234567',
// Decimal
10: '0123456789',
// Hexadecimal
16: '0123456789abcdef',
// Base 36
36: '0123456789abcdefghijklmnopqrstuvwxyz',
// Base 64
64: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/='
);
@if not map-has-key($chars, $base) {
@warn "There is no base `#{$base}` available.";
}
@return map-get($chars, $base);
}
@function to-string($num, $radix: 10) {
$chars: chars-from-base($radix);
$result: '';
$sign: '';
@if $num < 0 {
$sign: '-';
$num: abs($num);
}
@if $num >= 0 and $num < $radix {
@return $sign + str-slice($chars, ($num + 1), ($num + 1));
}
$q: $num;
@while $q != 0 {
$r: $q % $radix;
$q: floor($q / $radix);
$result: str-slice($chars, ($r + 1), ($r + 1)) + $result;
}
@return $sign + $result;
}
@function parse-int($str, $radix: 10) {
$chars: chars-from-base($radix);
$result: 0;
$is-negative: str-index($str, '-') == 1;
@if $is-negative {
$str: str-slice($str, 2);
}
@for $i from 1 through str-length($str) {
$char: str-slice($str, -$i, -$i);
$value: str-index($chars, $char) - 1;
$result: $result + ($value * pow($radix, ($i - 1)));
}
@return if($is-negative, -$result, $result);
}
// Get char code from `$str` at `$index` in range [1-52][A-Za-z].
// ––––––––
// @param [string] $str
// @param [number] $index
// ––––––––
// @return [number]
@function char-code-at($str, $index: 1) {
$chars: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
@return str-index($chars, str-slice($str, $index, $index));
}
// Get char at `$index` in range [1-52][A-Za-z].
// ––––––––
// @param [number] $index
// ––––––––
// @return [string]
@function from-char-code($index) {
$chars: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
@return str-slice($chars, $index, $index);
}
@function key-char-at($key, $index) {
@return char-code-at($key, floor($index % length($key)));
}
@function xor-encrypt($message, $key) {
$length: str-length($message);
$result: '';
@for $i from 1 through $length {
// $result: $result + bw(char-code-at($message, $i) '^' key-char-at($key, $i));
$result: $result + to-string(char-code-at($message, $i), 2) + ' ';
}
@return $result;
}
@function xor-decrypt($message, $key) {
$length: str-length($message);
$result: '';
@for $i from 1 through $length {
$c: parse-int(str-slice($message, $i, $i));
$result: $result + from-char-code(bw($c '^' key-char-at($key, $i)));
}
@return $result;
}
xor {
test: xor-encrypt('A', 'berlin');
//test: xor-decrypt('40', 'berlin');
t: to-string(87, 2);
}
xor {
test: "1 ";
t: "1010111";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment