Skip to content

Instantly share code, notes, and snippets.

@pascalduez
Created June 12, 2014 16:25
Show Gist options
  • Save pascalduez/5c870fa37e6c715d8476 to your computer and use it in GitHub Desktop.
Save pascalduez/5c870fa37e6c715d8476 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
// ----
// Sass (v3.3.8)
// Compass (v1.0.0.alpha.19)
// ----
// Frequency analysis
// https://en.wikipedia.org/wiki/Frequency_analysis
// 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);
}
// Relative frequencies of letters in the English language
$standard-frequency: (
a: 8.2, b: 1.5, c: 2.8, d: 4.3,
e: 12.7, f: 2.2, g: 2.0, h: 6.1,
i: 7.0, j: 0.2, k: 0.8, l: 4.0,
m: 2.4, n: 6.7, o: 7.5, p: 1.9,
q: 0.1, r: 6.0, s: 6.3, t: 9.1,
u: 2.8, v: 1.0, w: 2.4, x: 0.2,
y: 2.0, z: 0.1
);
$standard-frequency-ordered: (
e: 12.7, t: 9.1, a: 8.2, o: 7.5,
i: 7.0, n: 6.7, s: 6.3, h: 6.1,
r: 6.0, d: 4.3, l: 4.0, c: 2.8,
u: 2.8, m: 2.4, w: 2.4, f: 2.2,
g: 2.0, y: 2.0, p: 1.9, b: 1.5,
v: 1.0, k: 0.8, j: 0.2, x: 0.2,
q: 0.1, z: 0.1
);
@function get-frequency-table($str) {
$frequency: ();
$counter: ();
$length: str-length($str);
@for $i from 1 through 26 {
$counter: map-merge($counter, (from-char-code($i + 26): 0));
}
@for $i from 1 through $length {
$char: to-lower-case(str-slice($str, $i, $i));
@if map-has-key($counter, $char) {
$counter: map-merge($counter, ($char: map-get($counter, $char) + 1));
}
}
@each $char, $count in $counter {
$frequency: map-merge($frequency, ($char: $count / $length));
}
@return $frequency;
}
@function index-of-coincidence($frequency) {
$idx: 0;
@for $i from 1 through 26 {
$c: from-char-code($i + 26);
$idx: $idx + map-get($frequency, $c) * map-get($standard-frequency, $c);
}
@return $idx;
}
#{'frequency analysis'} {
$str: 'In the first case, the cipher can be broken using the same techniques'
+ 'as for a general simple substitution cipher, such as frequency analysis'
+ 'or pattern words';
$frequency: get-frequency-table($str);
frequency: inspect($frequency);
index: index-of-coincidence($frequency);
}
frequency analysis {
frequency: ("a": 0.0641, "b": 0.01923, "c": 0.04487, "d": 0.00641, "e": 0.10897, "f": 0.01923, "g": 0.01282, "h": 0.04487, "i": 0.0641, "j": 0, "k": 0.00641, "l": 0.01923, "m": 0.01282, "n": 0.0641, "o": 0.03205, "p": 0.02564, "q": 0.01282, "r": 0.0641, "s": 0.08974, "t": 0.0641, "u": 0.03846, "v": 0, "w": 0.00641, "x": 0, "y": 0.01282, "z": 0);
index: 5.39679;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment