March 2014 update

This commit is contained in:
Ingy döt Net 2014-04-02 16:56:35 +00:00
parent 09687c4926
commit a25938f123
1846 changed files with 21876 additions and 5203 deletions

View file

@ -1,49 +1,55 @@
fn uppercase_and_filter(in: &str) -> ~[u8] {
let mut result = ~[];
use std::ascii::AsciiCast;
use std::str::from_utf8;
for in.each_char |c| {
if char::is_ascii(c) {
if char::is_lowercase(c) {
// We know it's ascii, so just do the math directly
result.push((c + ('A' - 'a')) as u8)
} else if char::is_uppercase(c) {
result.push(c as u8);
}
}
static A: u8 = 'A' as u8;
static a: u8 = 'a' as u8;
fn uppercase_and_filter(input: &str) -> ~[u8] {
let mut result = ~[];
for b in input.bytes() {
if b.is_ascii() {
let ascii = b.to_ascii();
if ascii.is_lower() {
// We know it's ascii, so just do the math directly
result.push((b + (A - a)))
} else if ascii.is_upper() {
result.push(b);
}
}
}
return result;
return result;
}
fn vigenere(key: &str, text: &str, is_encoding: bool) -> ~str {
const A: u8 = 'A' as u8;
let key_bytes = uppercase_and_filter(key);
let text_bytes = uppercase_and_filter(text);
let key_bytes = uppercase_and_filter(key);
let text_bytes = uppercase_and_filter(text);
let mut result_bytes = ~[];
let mut result_bytes = ~[];
for text_bytes.eachi |i, &c| {
let c2 = if is_encoding {
(c + key_bytes[i % key_bytes.len()] - 2 * A) % 26 + A
} else {
(c - key_bytes[i % key_bytes.len()] + 26) % 26 + A
};
result_bytes.push(c2);
}
for (i, c) in text_bytes.iter().enumerate() {
let c2 = if is_encoding {
(c + key_bytes[i % key_bytes.len()] - 2 * A) % 26 + A
} else {
(c - key_bytes[i % key_bytes.len()] + 26) % 26 + A
};
result_bytes.push(c2);
}
return str::from_bytes(result_bytes);
return from_utf8(result_bytes).to_owned();
}
fn main() {
let text = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
let key = "VIGENERECIPHER";
let text = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
let key = "VIGENERECIPHER";
io::println(fmt!("Text: %s", text));
io::println(fmt!("Key: %s", key));
println!("Text: {:s}", text);
println!("Key: {:s}", key);
let encoded = vigenere(key, text, true);
io::println(fmt!("Code: %s", encoded));
let decoded = vigenere(key, encoded, false);
io::println(fmt!("Back: %s", decoded));
let encoded = vigenere(key, text, true);
println!("Code: {:s}", encoded);
let decoded = vigenere(key, encoded, false);
println!("Back: {:s}", decoded);
}

View file

@ -0,0 +1,52 @@
class Vigenere(val key: String) {
private def rotate(p: Int, s: IndexedSeq[Char]) = s.drop(p) ++ s.take(p)
private val chars = 'A' to 'Z'
private val vSquare = (chars ++
rotate(1, chars) ++
rotate(2, chars) ++
rotate(3, chars) ++
rotate(4, chars) ++
rotate(5, chars) ++
rotate(6, chars) ++
rotate(7, chars) ++
rotate(8, chars) ++
rotate(9, chars) ++
rotate(10, chars) ++
rotate(11, chars) ++
rotate(12, chars) ++
rotate(13, chars) ++
rotate(14, chars) ++
rotate(15, chars) ++
rotate(16, chars) ++
rotate(17, chars) ++
rotate(18, chars) ++
rotate(19, chars) ++
rotate(20, chars) ++
rotate(21, chars) ++
rotate(22, chars) ++
rotate(23, chars) ++
rotate(24, chars) ++
rotate(25, chars))
private var encIndex = -1
private var decIndex = -1
def encode(c: Char) = {
encIndex += 1
if (encIndex == key.length) encIndex = 0
if (chars.contains(c)) vSquare((c - 'A') * 26 + key(encIndex) - 'A') else c
}
def decode(c: Char) = {
decIndex += 1
if (decIndex == key.length) decIndex = 0
if (chars.contains(c)) {
val baseIndex = (key(decIndex) - 'A') * 26
val nextIndex = vSquare.indexOf(c, baseIndex)
chars(nextIndex - baseIndex)
} else c
}
}

View file

@ -0,0 +1,6 @@
val text = "ATTACKATDAWN"
val myVigenere = new Vigenere("LEMON")
val encoded = text.map(c => myVigenere.encode(c))
println("Plaintext => " + text)
println("Ciphertext => " + encoded)
println("Decrypted => " + encoded.map(c => myVigenere.decode(c)))

View file

@ -1,11 +1,10 @@
@(next :args)
@(do
(defun letter-mod26-op (func) ;; add or subtract capital letters modulo 26
(lambda (a b) (+ #\A (mod (call func (- a #\A) (- b #\A)) 26))))
(defun vig-op (plus-or-minus)
(op + #\A [mod [plus-or-minus (- @1 #\A) (- @2 #\A)] 26]))
(defun vig (msg key encrypt)
(cat-str (mapcar (letter-mod26-op (if encrypt (fun +) (fun -)))
(list-str msg)
(repeat (list-str key))) "")))
(mapcar (vig-op [if encrypt + -]) msg (repeat key))))
@(coll)@{key /[A-Za-z]/}@(end)
@(coll)@{msg /[A-Za-z]/}@(end)
@(cat key "")