X-Git-Url: http://nitlanguage.org diff --git a/lib/crypto.nit b/lib/crypto.nit index 8071a9d..2abe5d7 100644 --- a/lib/crypto.nit +++ b/lib/crypto.nit @@ -20,17 +20,17 @@ redef class Char # # NOTE: works on letters only # - # assert 'x'.rot(6) == 'd' - # assert 'T'.rot(15) == 'I' - # assert '1'.rot(10) == '1' - # assert '$'.rot(10) == '$' - # assert 'z'.rot(-2) == 'x' + # assert 'x'.rot(6) == 'd' + # assert 'T'.rot(15) == 'I' + # assert '1'.rot(10) == '1' + # assert '$'.rot(10) == '$' + # assert 'z'.rot(-2) == 'x' fun rot(x: Int): Char do if not is_letter then return self x = x % 26 if x < 0 then x += 26 var up = false - var val = ascii + var val = code_point if is_upper then up = true val += 32 @@ -38,11 +38,11 @@ redef class Char val += x if val > 122 then val -= 26 if up then val -= 32 - return val.ascii + return val.code_point end end -redef class String +redef class Text # Performs a Rotation of `x` on each letter of self # # Works by replacing every character in `self` by its @@ -60,12 +60,12 @@ redef class String # We then replace every letter in our original string by # their rotated representations, therefore yielding : "dbedewx" # - # assert "All your base are belong to us".rot(13) == "Nyy lbhe onfr ner orybat gb hf" - # assert "This is no moon.".rot(4).rot(22) == "This is no moon." + # assert "All your base are belong to us".rot(13) == "Nyy lbhe onfr ner orybat gb hf" + # assert "This is no moon.".rot(4).rot(22) == "This is no moon." # # NOTE : Works on letters only # NOTE : This cipher is symmetrically decrypted with an `x` of 26-`x` - fun rot(x: Int): String do + fun rot(x: Int): Text do var rot = x % 26 if rot < 0 then rot += 26 var d = new FlatBuffer.with_capacity(length) @@ -92,8 +92,8 @@ redef class String # # Therefore, yielding the ciphertext : "fgounbmtcieehkh" # - # assert "fuckingbehemoth".railfence(4) == "fgounbmtcieehkh" - fun railfence(depth: Int): String do + # assert "fuckingbehemoth".railfence(4) == "fgounbmtcieehkh" + fun railfence(depth: Int): Text do var lines = new Array[FlatBuffer].with_capacity(depth) var up = false for i in [0..depth[ do @@ -127,10 +127,10 @@ redef class String return r.to_s end - # Transforms a rail-fence-encrypted String to its original + # Transforms a rail-fence-encrypted Text to its original # - # assert "fgounbmtcieehkh".unrail(4) == "fuckingbehemoth" - fun unrail(depth: Int): String do + # assert "fgounbmtcieehkh".unrail(4) == "fuckingbehemoth" + fun unrail(depth: Int): Text do var dots = "." * length var arr = new FlatBuffer.from(dots) var start = 0 @@ -156,6 +156,24 @@ redef class String end end +redef class Bytes + + # Returns `self` xored with `key` + # + # The key is cycled through until the `self` has been completely xored. + # + # assert "goodmorning".to_bytes.xorcipher(" ".to_bytes) == "GOODMORNING".bytes + fun xorcipher(key: Bytes): Bytes do + var xored = new Bytes.with_capacity(self.length) + + for i in self.length.times do + xored.add(self[i] ^ key[i % key.length]) + end + + return xored + end +end + redef class Int # Generates the paces for each depth. # @@ -188,7 +206,7 @@ redef class Int # # In the end, our string is read using the generated array # - # SEE: `String::unrail` for how the array is used + # SEE: `Text::unrail` for how the array is used private fun unrail_paces: Array[Couple[Int, Int]] do var ret = new Array[Couple[Int,Int]].with_capacity(self) var extremes = new Couple[Int, Int]((self - 1) * 2, (self - 1) * 2)