From: Jean Privat Date: Fri, 1 Jun 2018 17:10:04 +0000 (-0400) Subject: Merge: crypto: add some methods on Bytes, Char & String X-Git-Url: http://nitlanguage.org?hp=8982932dc5b83032ce08b5b16b8a60465dad7e1e Merge: crypto: add some methods on Bytes, Char & String Pull-Request: #2636 Reviewed-by: Alexis Laferrière --- diff --git a/lib/core/bytes.nit b/lib/core/bytes.nit index 84276ac..73802a7 100644 --- a/lib/core/bytes.nit +++ b/lib/core/bytes.nit @@ -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. diff --git a/lib/core/kernel.nit b/lib/core/kernel.nit index 6a07060..72b6fc9 100644 --- a/lib/core/kernel.nit +++ b/lib/core/kernel.nit @@ -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 diff --git a/lib/crypto/basic_ciphers.nit b/lib/crypto/basic_ciphers.nit index e4a133c..9796bf8 100644 --- a/lib/crypto/basic_ciphers.nit +++ b/lib/crypto/basic_ciphers.nit @@ -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 index 0000000..59f8f55 --- /dev/null +++ b/lib/crypto/bytes.nit @@ -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 diff --git a/lib/crypto/crypto.nit b/lib/crypto/crypto.nit index 4709403..97d7cee 100644 --- a/lib/crypto/crypto.nit +++ b/lib/crypto/crypto.nit @@ -17,3 +17,4 @@ module crypto import basic_ciphers import xor_ciphers +import bytes diff --git a/tests/sav/error_class_glob.res b/tests/sav/error_class_glob.res index d532535..389e33a 100644 --- a/tests/sav/error_class_glob.res +++ b/tests/sav/error_class_glob.res @@ -8,6 +8,6 @@ ../lib/core/kernel.nit:517,1--599,3: Error: `kernel$Float` does not specialize `module_0$Object`. Possible duplication of the root class `Object`? ../lib/core/kernel.nit:601,1--705,3: Error: `kernel$Byte` does not specialize `module_0$Object`. Possible duplication of the root class `Object`? ../lib/core/kernel.nit:707,1--885,3: Error: `kernel$Int` does not specialize `module_0$Object`. Possible duplication of the root class `Object`? -../lib/core/kernel.nit:887,1--1056,3: Error: `kernel$Char` does not specialize `module_0$Object`. Possible duplication of the root class `Object`? -../lib/core/kernel.nit:1058,1--1075,3: Error: `kernel$Pointer` does not specialize `module_0$Object`. Possible duplication of the root class `Object`? -../lib/core/kernel.nit:1077,1--1086,3: Error: `kernel$Task` does not specialize `module_0$Object`. Possible duplication of the root class `Object`? +../lib/core/kernel.nit:887,1--1074,3: Error: `kernel$Char` does not specialize `module_0$Object`. Possible duplication of the root class `Object`? +../lib/core/kernel.nit:1076,1--1093,3: Error: `kernel$Pointer` does not specialize `module_0$Object`. Possible duplication of the root class `Object`? +../lib/core/kernel.nit:1095,1--1104,3: Error: `kernel$Task` does not specialize `module_0$Object`. Possible duplication of the root class `Object`? diff --git a/tests/sav/nituml_args3.res b/tests/sav/nituml_args3.res index d98d744..de28bd9 100644 --- a/tests/sav/nituml_args3.res +++ b/tests/sav/nituml_args3.res @@ -63,7 +63,7 @@ Discrete -> Int [dir=back arrowtail=open style=dashed]; Numeric -> Int [dir=back arrowtail=open style=dashed]; Char [ - label = "{Char||+ to_i(): Int\l+ ascii(): Byte\l+ code_point(): Int\l+ is_ascii(): Bool\l+ to_lower(): Char\l+ to_upper(): Char\l+ is_digit(): Bool\l+ is_lower(): Bool\l+ is_upper(): Bool\l+ is_letter(): Bool\l+ is_whitespace(): Bool\l}" + label = "{Char||+ +(i: Int): Char\l+ -(i: Int): Char\l+ to_i(): Int\l+ ascii(): Byte\l+ code_point(): Int\l+ is_ascii(): Bool\l+ to_lower(): Char\l+ to_upper(): Char\l+ is_digit(): Bool\l+ is_lower(): Bool\l+ is_upper(): Bool\l+ is_letter(): Bool\l+ is_whitespace(): Bool\l}" ] Discrete -> Char [dir=back arrowtail=open style=dashed]; diff --git a/tests/sav/nituml_args4.res b/tests/sav/nituml_args4.res index 10ba4a8..53d63de 100644 --- a/tests/sav/nituml_args4.res +++ b/tests/sav/nituml_args4.res @@ -63,7 +63,7 @@ Discrete -> Int [dir=back arrowtail=open style=dashed]; Numeric -> Int [dir=back arrowtail=open style=dashed]; Char [ - label = "{Char||+ to_i(): Int\l+ ascii(): Byte\l+ code_point(): Int\l+ is_ascii(): Bool\l+ to_lower(): Char\l+ to_upper(): Char\l+ is_digit(): Bool\l+ is_lower(): Bool\l+ is_upper(): Bool\l+ is_letter(): Bool\l+ is_whitespace(): Bool\l}" + label = "{Char||+ +(i: Int): Char\l+ -(i: Int): Char\l+ to_i(): Int\l+ ascii(): Byte\l+ code_point(): Int\l+ is_ascii(): Bool\l+ to_lower(): Char\l+ to_upper(): Char\l+ is_digit(): Bool\l+ is_lower(): Bool\l+ is_upper(): Bool\l+ is_letter(): Bool\l+ is_whitespace(): Bool\l}" ] Discrete -> Char [dir=back arrowtail=open style=dashed];