Created
April 14, 2020 00:38
-
-
Save viniciusss/ea1430bbdd797cfaa6426f9c6a64cc7d to your computer and use it in GitHub Desktop.
Desafio codenation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company | |
class Krypto { | |
fun decrypt (textoOriginal: String, numeroCasas: Int = 10): String { | |
return apply(textoOriginal, (numeroCasas * -1)); | |
} | |
fun encrypt (textoOriginal: String, numeroCasas: Int = 10): String { | |
return apply(textoOriginal, numeroCasas); | |
} | |
private fun apply(texto: String, fator: Int): String | |
{ | |
val encriptedText = StringBuilder(); | |
for (x in texto.decapitalize().toCharArray()) { | |
encriptedText.append( | |
applyToChar(x, fator) | |
); | |
} | |
return encriptedText.toString(); | |
} | |
private fun applyToChar(char: Char, fator: Int): Char { | |
if (!char.isLetter()) { | |
return char; | |
} | |
val asciiCharStartsIn = 97; | |
val minValue = 97; | |
val maxValue = 122; | |
var novoChar = ((char.toInt()-asciiCharStartsIn+fator)); | |
if (0 > novoChar) { | |
novoChar += maxValue; | |
} else if (novoChar < minValue) { | |
novoChar += minValue | |
} | |
return novoChar.toChar(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company | |
import java.io.File | |
import java.net.URL | |
import org.json.JSONObject | |
import java.security.MessageDigest | |
// references: | |
// https://rosettacode.org/wiki/SHA-1#Kotlin | |
// https://www.javacodemonk.com/md5-and-sha256-in-java-kotlin-and-android-96ed9628 | |
// http://www.asciitable.com/ | |
// http://stleary.github.io/JSON-java/index.html | |
fun String.sha1(): String { | |
val digestBytes = MessageDigest.getInstance("SHA-1").digest(this.toByteArray()) | |
return digestBytes.toHex() | |
} | |
fun ByteArray.toHex(): String { | |
return joinToString("") { "%02x".format(it) } | |
} | |
object Main { | |
@JvmStatic | |
fun main(args: Array<String>) { | |
val token = args[0] ?: ""; | |
val data = getData(token) | |
val jsonData = JSONObject(data) | |
val krypto = Krypto() | |
println("## encriptando....") | |
println(krypto.encrypt("a ligeira raposa marrom saltou sobre o cachorro cansado", 3)) | |
println("## decriptando....") | |
println(krypto.decrypt("d oljhlud udsrvd pduurp vdowrx vreuh r fdfkruur fdqvdgr", 3)) | |
println("## descriptando pra valer....") | |
val valorCifrado = jsonData.getString("cifrado") | |
val numeroCasas = jsonData.getInt("numero_casas") | |
val valorDecifrado = krypto.decrypt(valorCifrado, numeroCasas) | |
println(valorCifrado) | |
println(numeroCasas) | |
println(valorDecifrado) | |
println(valorDecifrado.sha1()) | |
jsonData.put("decifrado", valorDecifrado) | |
jsonData.put("resumo_criptografico", valorDecifrado.sha1()) | |
println(jsonData.toString()) | |
val repo = RetrofitInitializer().codeNationService().generateData("1231231") | |
repo.execute(); | |
} | |
fun getData(token: String): String { | |
val answerFile = File("answer.json") | |
if (!answerFile.exists()) { | |
val url = URL("https://api.codenation.dev/v1/challenge/dev-ps/generate-data?token=$token") | |
answerFile.writeText(url.readText()) | |
} | |
return answerFile.readText() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment