From cbb4eaad94fe5b18346ac3566d2a9ea33e7c0504 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Wed, 3 May 2017 03:07:35 +0900 Subject: [PATCH] more commits before the havoc --- src/net/torvald/terrarum/utils/CipherROT.kt | 65 -------------- .../torvald/terrarum/utils/PasswordBase32.kt | 2 + src/net/torvald/terrarum/utils/ROTCipher.kt | 84 +++++++++++++++++++ 3 files changed, 86 insertions(+), 65 deletions(-) delete mode 100644 src/net/torvald/terrarum/utils/CipherROT.kt create mode 100644 src/net/torvald/terrarum/utils/ROTCipher.kt diff --git a/src/net/torvald/terrarum/utils/CipherROT.kt b/src/net/torvald/terrarum/utils/CipherROT.kt deleted file mode 100644 index 49444eb52..000000000 --- a/src/net/torvald/terrarum/utils/CipherROT.kt +++ /dev/null @@ -1,65 +0,0 @@ -package net.torvald.terrarum.utils - -/** - * Created by minjaesong on 16-03-20. - */ -object CipherROT { - - const val CODE_CAP_A = 'A'.toInt() - const val CODE_LOW_A = 'a'.toInt() - - /** - * ROT encryption, default setting - * - * * Latin alph: removes diacritics and do ROT13, string reverse, capitalised. - * Ligatures are disassembled. Even if the target language does not have - * certain alphabet (e.g. C in Icelandic), such alphabet will be printed anyway. - * * Numeric: no encrypt - */ - fun encrypt(plaintext: String): String { - - fun removeDiacritics(c: Char): Char { - val normaliseMap = hashMapOf( - Pair('À', "A"), - Pair('Á', "A"), - Pair('Â', "A"), - Pair('À', "A"), - Pair('Ã', "A"), - Pair('Å', "A"), - Pair('Æ', "AE"), - Pair('Ç', "C"), - Pair('È', "E"), - Pair('É', "E"), - Pair('Ê', "E"), - Pair('Ë', "E"), - Pair('Ì', "I"), - Pair('Í', "I"), - Pair('Î', "I"), - Pair('Ï', "I"), - Pair('Ð', "D") - ) - throw NotImplementedError("Feature WIP") - } - - throw NotImplementedError("Feature WIP") - } - - /** - * Note; range starts with zero - * @param number to rotate - * @param rotation - * @param range size of the rotation table. 4 means (0,1,2,3) - */ - fun rotN(number: Int, rotation: Int, range: Int): Int { - return if (number < range - rotation + 1) number + rotation - else number - (range - rotation + 1) - } - - fun rot13(c: Char): Char { - return if (c in 'a'..'z') - (rotN((c.toInt() - CODE_LOW_A), 13, 26) + CODE_LOW_A).toChar() - else if (c in 'A'..'Z') - (rotN((c.toInt() - CODE_CAP_A), 13, 26) + CODE_CAP_A).toChar() - else c - } -} \ No newline at end of file diff --git a/src/net/torvald/terrarum/utils/PasswordBase32.kt b/src/net/torvald/terrarum/utils/PasswordBase32.kt index 004247d2f..d35522976 100644 --- a/src/net/torvald/terrarum/utils/PasswordBase32.kt +++ b/src/net/torvald/terrarum/utils/PasswordBase32.kt @@ -111,6 +111,8 @@ object PasswordBase32 { } /** + * @param input password input from the user. Will be automatically converted to uppercase and + * will correct common mistakes. * @param outByteLength expected length of your decoded password. It is always a good idea to * suspect user inputs and sanitise them. */ diff --git a/src/net/torvald/terrarum/utils/ROTCipher.kt b/src/net/torvald/terrarum/utils/ROTCipher.kt new file mode 100644 index 000000000..30623173f --- /dev/null +++ b/src/net/torvald/terrarum/utils/ROTCipher.kt @@ -0,0 +1,84 @@ +package net.torvald.terrarum.utils + +/** + * Created by minjaesong on 16-03-20. + */ +object ROTCipher { + + const val CODE_CAP_A = 'A'.toInt() + const val CODE_LOW_A = 'a'.toInt() + + private val substituteSet = hashMapOf( + Pair('À', "A"), + Pair('Á', "A"), + Pair('Â', "A"), + Pair('À', "A"), + Pair('Ã', "A"), + Pair('Å', "A"), + Pair('Æ', "AE"), + Pair('Ç', "C"), + Pair('È', "E"), + Pair('É', "E"), + Pair('Ê', "E"), + Pair('Ë', "E"), + Pair('Ì', "I"), + Pair('Í', "I"), + Pair('Î', "I"), + Pair('Ï', "I"), + Pair('Ð', "D"), + Pair('Ñ', "N"), + Pair('Ò', "O"), + Pair('Ó', "O"), + Pair('Ô', "O"), + Pair('Õ', "O"), + Pair('Ö', "OE"), + Pair('Ø', "OE"), + Pair('Ù', "U"), + Pair('Ú', "U"), + Pair('Û', "U"), + Pair('Ü', "Y"), + Pair('Ý', "TH"), + Pair('Þ', "TH"), + Pair('ß', "SS") + ) + + /** + * ROT encryption, default setting + * + * * Latin alph: removes diacritics and do ROT13, string reverse, capitalised. + * Ligatures are disassembled. Even if the target language does not have + * certain alphabet (e.g. C in Icelandic), such alphabet will be printed anyway. + * * Numeric: no encrypt + */ + fun encrypt(plaintext: String): String { + var plaintext = plaintext.toUpperCase() + substituteSet.forEach { from, to -> + plaintext = plaintext.replace(from.toString(), to) + } + plaintext = plaintext.reversed() + + val sb = StringBuilder() + plaintext.forEach { sb.append(rot13(it)) } + + return sb.toString() + } + + /** + * Note; range starts with zero + * @param number to rotate + * @param rotation + * @param range size of the rotation table. 4 means (0,1,2,3) + */ + fun rotN(number: Int, rotation: Int, range: Int): Int { + return if (number < range - rotation + 1) number + rotation + else number - (range - rotation + 1) + } + + fun rot13(c: Char): Char { + return if (c in 'a'..'z') + (rotN((c.toInt() - CODE_LOW_A), 13, 26) + CODE_LOW_A).toChar() + else if (c in 'A'..'Z') + (rotN((c.toInt() - CODE_CAP_A), 13, 26) + CODE_CAP_A).toChar() + else c + } +} \ No newline at end of file