Created
April 13, 2020 22:13
-
-
Save ironijunior/fd52cfb4329da93659574ec4ad189bae to your computer and use it in GitHub Desktop.
AceleraDEV
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.ironijunior; | |
import com.fasterxml.jackson.annotation.JsonProperty; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import lombok.Getter; | |
import lombok.Setter; | |
import lombok.ToString; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.http.HttpEntity; | |
import org.springframework.http.MediaType; | |
import org.springframework.http.client.MultipartBodyBuilder; | |
import org.springframework.util.MultiValueMap; | |
import org.springframework.web.reactive.function.BodyInserters; | |
import org.springframework.web.reactive.function.client.WebClient; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.nio.file.attribute.FileAttribute; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
/** | |
* Application's main class | |
*/ | |
@SpringBootApplication | |
public class Application { | |
private static final String TOKEN = "29895a1a0cdb9051322421ddc10a6a1673fb498b"; | |
private static final int A_ASCII = 97; | |
private static final int Z_ASCII = 122; | |
public static void main(String[] args) { | |
SpringApplication.run(Application.class, args); | |
WebClient client = WebClient.builder().build(); | |
System.out.println(">>>> Running"); | |
client.get().uri("https://api.codenation.dev/v1/challenge/dev-ps/generate-data?token=" + TOKEN) | |
.exchange() | |
.subscribe(clientResponse -> { | |
clientResponse.bodyToMono(Answer.class) | |
.map(answer -> { | |
String decypher = decypherAnswer(answer); | |
answer.setDecifrado(decypher); | |
return answer; | |
}) | |
.map(answer -> { | |
answer.setResumoCriptografico(generateSha1(answer.getDecifrado())); | |
return answer; | |
}) | |
.map(answer -> { | |
MultiValueMap<String, HttpEntity<?>> map = null; | |
try(FileWriter file = new FileWriter("C://Temp//answer.json")) { | |
file.write(new ObjectMapper().writeValueAsString(answer)); | |
file.flush(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return answer; | |
}) | |
.subscribe(System.out::println); | |
}); | |
} | |
private static String decypherAnswer(Answer answer) { | |
StringBuilder sb = new StringBuilder(); | |
int limit = Z_ASCII - answer.getNumeroCasas(); | |
for (char c: answer.getCifrado().toCharArray()) { | |
int ascii = (int)c; | |
if (isLetter(ascii)) { | |
if (ascii > limit) { | |
int rest = answer.getNumeroCasas() - (Z_ASCII - ascii) - 1; //-1 bc should consider the a | |
ascii = A_ASCII + rest; | |
} else { | |
ascii += answer.getNumeroCasas(); | |
} | |
} | |
sb.append((char) ascii); | |
} | |
return sb.toString(); | |
} | |
private static boolean isLetter(int ascii) { | |
return ascii >= A_ASCII && ascii <= Z_ASCII; | |
} | |
private static String generateSha1(String value) { | |
MessageDigest mDigest = null; | |
try { | |
mDigest = MessageDigest.getInstance("SHA1"); | |
} catch (NoSuchAlgorithmException e) { | |
e.printStackTrace(); | |
} | |
byte[] result = mDigest.digest(value.getBytes()); | |
StringBuilder sb = new StringBuilder(); | |
for (byte b : result) { | |
sb.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1)); | |
} | |
return sb.toString(); | |
} | |
} | |
@Getter | |
@Setter | |
@ToString | |
class Answer { | |
@JsonProperty("numero_casas") | |
private Integer numeroCasas; | |
private String token; | |
private String cifrado; | |
private String decifrado; | |
@JsonProperty("resumo_criptografico") | |
private String resumoCriptografico; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment