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.
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
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
--- /dev/null
+# 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
import basic_ciphers
import xor_ciphers
+import bytes