Advertisement
Guest User

GeraSenhaHash

a guest
Apr 13th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package gerasenhahash;
  7.  
  8. import com.google.gson.Gson;
  9. import com.google.gson.GsonBuilder;
  10. import java.io.IOException;
  11. import java.io.InputStreamReader;
  12. import java.io.Reader;
  13. import java.io.UnsupportedEncodingException;
  14.  import java.security.MessageDigest;
  15.  import java.security.NoSuchAlgorithmException;
  16. import java.util.Scanner;
  17. /**
  18.  *
  19.  * @author RenanTeles
  20.  */
  21. public class GeraSenhaHash {
  22.  
  23.     String nomeUser,PassUser;
  24.     /**
  25.      * @param args the command line arguments
  26.      * @throws java.security.NoSuchAlgorithmException
  27.      * @throws java.io.UnsupportedEncodingException
  28.      */
  29.     public static void main(String[] args)  throws NoSuchAlgorithmException, UnsupportedEncodingException, IOException{
  30.         // TODO code application logic here
  31.       Scanner sc = new Scanner(System.in);
  32.         System.out.println("Digite o usuário:");
  33.         String newUser = sc.nextLine();
  34.        
  35.         System.out.println("Digite a senha:");
  36.         String newPassword = sc.nextLine();
  37.        
  38.         String hashSenha = transformaHash(newPassword);
  39.        
  40.         leJson();
  41.        
  42.        
  43.        
  44.     }
  45.    
  46.    
  47.     private static String transformaHash(String senha) throws NoSuchAlgorithmException{
  48.        
  49.        MessageDigest md = MessageDigest.getInstance("MD5");
  50.        md.update(senha.getBytes());
  51.        byte[] hashMd5 = md.digest();
  52.        String hashString = paraStringHexa(hashMd5);
  53.        //System.out.println(hashString);
  54.        String HashFinal = hashString.substring(0, 5);
  55.        //System.out.println(HashFinal);
  56.  
  57.        return HashFinal;  
  58.        
  59.     }
  60.    
  61.     private static String paraStringHexa(byte[] bytes) {
  62.         StringBuilder s = new StringBuilder();
  63.         for (int i = 0; i < bytes.length; i++) {
  64.             int parteAlta = ((bytes[i] >> 4) & 0xf) << 4;
  65.             int parteBaixa = bytes[i] & 0xf;
  66.             if (parteAlta == 0) s.append('0');
  67.             s.append(Integer.toHexString(parteAlta | parteBaixa));
  68.         }
  69.         return s.toString();
  70.     }
  71.    
  72.     private static void leJson() throws IOException{
  73.        
  74.      try(Reader reader = new InputStreamReader(GeraSenhaHash.class.getResourceAsStream("/cadastroUsuario.json"))){
  75.             Gson gson = new GsonBuilder().create();
  76.             Usuario p = gson.fromJson(reader, Usuario.class);
  77.             System.out.println(p);
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement