Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package gerasenhahash;
- import com.google.gson.Gson;
- import com.google.gson.GsonBuilder;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.Reader;
- import java.io.UnsupportedEncodingException;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import java.util.Scanner;
- /**
- *
- * @author RenanTeles
- */
- public class GeraSenhaHash {
- String nomeUser,PassUser;
- /**
- * @param args the command line arguments
- * @throws java.security.NoSuchAlgorithmException
- * @throws java.io.UnsupportedEncodingException
- */
- public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException, IOException{
- // TODO code application logic here
- Scanner sc = new Scanner(System.in);
- System.out.println("Digite o usuário:");
- String newUser = sc.nextLine();
- System.out.println("Digite a senha:");
- String newPassword = sc.nextLine();
- String hashSenha = transformaHash(newPassword);
- leJson();
- }
- private static String transformaHash(String senha) throws NoSuchAlgorithmException{
- MessageDigest md = MessageDigest.getInstance("MD5");
- md.update(senha.getBytes());
- byte[] hashMd5 = md.digest();
- String hashString = paraStringHexa(hashMd5);
- //System.out.println(hashString);
- String HashFinal = hashString.substring(0, 5);
- //System.out.println(HashFinal);
- return HashFinal;
- }
- private static String paraStringHexa(byte[] bytes) {
- StringBuilder s = new StringBuilder();
- for (int i = 0; i < bytes.length; i++) {
- int parteAlta = ((bytes[i] >> 4) & 0xf) << 4;
- int parteBaixa = bytes[i] & 0xf;
- if (parteAlta == 0) s.append('0');
- s.append(Integer.toHexString(parteAlta | parteBaixa));
- }
- return s.toString();
- }
- private static void leJson() throws IOException{
- try(Reader reader = new InputStreamReader(GeraSenhaHash.class.getResourceAsStream("/cadastroUsuario.json"))){
- Gson gson = new GsonBuilder().create();
- Usuario p = gson.fromJson(reader, Usuario.class);
- System.out.println(p);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement