crypto: add some methods on Bytes, Char & String
authorJean Privat <jean@pryen.org>
Tue, 1 May 2018 17:04:35 +0000 (13:04 -0400)
committerJean Privat <jean@pryen.org>
Thu, 31 May 2018 12:47:32 +0000 (08:47 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

lib/core/bytes.nit
lib/core/kernel.nit
lib/crypto/basic_ciphers.nit
lib/crypto/bytes.nit [new file with mode: 0644]
lib/crypto/crypto.nit

index 84276ac..73802a7 100644 (file)
@@ -251,6 +251,21 @@ class Bytes
                return slice(from, length)
        end
 
+       # Reverse the byte array in place
+       #
+       #     var b = "abcd".to_bytes
+       #     b.reverse
+       #     assert b.to_s == "dcba"
+       fun reverse
+       do
+               var l = length
+               for i in [0..l/2[ do
+                       var tmp = self[i]
+                       self[i] = self[l-i-1]
+                       self[l-i-1] = tmp
+               end
+       end
+
        # Returns self as an hexadecimal digest.
        #
        # Also known as plain hexdump or postscript hexdump.
index 6a07060..72b6fc9 100644 (file)
@@ -918,6 +918,24 @@ universal Char
        redef fun successor(i) is intern
        redef fun predecessor(i) is intern
 
+       # The `i`-th char after self (in code point)
+       #
+       # ~~~
+       # assert 'A' + 5 == 'F'
+       # ~~~
+       #
+       # Alias of `successor`.
+       fun +(i: Int): Char do return successor(i)
+
+       # The `i`-th char before self (in code point)
+       #
+       # ~~~
+       # assert 'F' - 5 == 'A'
+       # ~~~
+       #
+       # Alias of `predecessor`.
+       fun -(i: Int): Char do return predecessor(i)
+
        redef fun distance(c)
        do
                var d = self.code_point - c.code_point
index e4a133c..9796bf8 100644 (file)
@@ -154,6 +154,56 @@ redef class Text
                end
                return arr.to_s
        end
+
+       # Vigenere encoder on ASCII letters.
+       #
+       # Only the letters in '[A-Za-z]' are encoded while keeping the case.
+       #
+       #     assert "Hello, World!".vigenere("abc") == "Hfnlp, Yosnd!"
+       #
+       # REQUIRE `key` contains only lowercases '[a-z]'
+       fun vigenere(key: String): String
+       do
+               var res = new Buffer
+               res.enlarge(length)
+               var i = 0
+               for c in self do
+                       var k = key[i%key.length]
+                       assert k >= 'a' and k <= 'z'
+                       if c.is_letter then
+                               var d = k.code_point - 'a'.code_point
+                               c = c.rot(d)
+                               i += 1
+                       end
+                       res.add c
+               end
+               return res.to_s
+       end
+
+       # Vigenere decoder on ASCII letters.
+       #
+       # Only the letters in '[A-Za-z]' are decoded while keeping the case.
+       #
+       #     assert "Hfnlp, Yosnd!".uvigenere("abc") == "Hello, World!"
+       #
+       # REQUIRE `key` contains only lowercases '[a-z]'
+       fun uvigenere(key: String): String
+       do
+               var res = new Buffer
+               res.enlarge(length)
+               var i = 0
+               for c in self do
+                       var k = key[i%key.length]
+                       assert k >= 'a' and k <= 'z'
+                       if c.is_letter then
+                               var d = k.code_point - 'a'.code_point
+                               c = c.rot(-d)
+                               i += 1
+                       end
+                       res.add c
+               end
+               return res.to_s
+       end
 end
 
 redef class Bytes
diff --git a/lib/crypto/bytes.nit b/lib/crypto/bytes.nit
new file mode 100644 (file)
index 0000000..59f8f55
--- /dev/null
@@ -0,0 +1,33 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Mix of utilities and services related to bytes
+module bytes
+
+redef class Bytes
+       # PKCS#7 padding.
+       #
+       # Extends `self` by appending the number of bytes of padding to the end of the block.
+       #
+       #     assert "YELLOW SUBMARINE".to_bytes.pkcs7(20) == "YELLOW SUBMARINE\x04\x04\x04\x04".to_bytes
+       fun pkcs7(blocksize: Int): Bytes
+       do
+               var mod = length % blocksize
+               if mod == 0 then return self
+               var pad = blocksize - mod
+               var byte = pad.to_b
+               for i in [0..pad[ do add byte
+               return self
+       end
+end
index 4709403..97d7cee 100644 (file)
@@ -17,3 +17,4 @@ module crypto
 
 import basic_ciphers
 import xor_ciphers
+import bytes