Guest User

Untitled

a guest
Apr 13th, 2016
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.78 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 cadastrousuario;
  7.  
  8. import com.google.gson.Gson;
  9. import java.io.File;
  10. import java.io.FileWriter;
  11. import java.io.IOException;
  12. import java.security.MessageDigest;
  13. import java.security.NoSuchAlgorithmException;
  14. import java.util.Scanner;
  15. import org.json.JSONObject;
  16.  
  17. /**
  18.  *
  19.  * @author RenanTeles
  20.  */
  21. public class CadastroUsuario {
  22.  
  23.     /**
  24.      * @param args the command line arguments
  25.      */
  26.     public static void main(String[] args) throws NoSuchAlgorithmException {
  27.         // TODO code application logic here
  28.        
  29.         Scanner sc = new Scanner(System.in);
  30.         System.out.println("Cadastre nome do Usuário:");
  31.         String newUser = sc.nextLine();
  32.        
  33.         System.out.println("Cadastre Senha do Usuário:");
  34.         String newPassword = sc.nextLine();
  35.        
  36.         String hashSenha = transformaHash(newPassword);
  37.        
  38.         Usuario user = new Usuario();
  39.         user.setHashSenha(hashSenha);
  40.         user.setNome(newUser);
  41.        
  42.         JSONObject objeto = new JSONObject();
  43.         objeto.put("nome", user.getNome());
  44.         objeto.put("senha", user.getHashSenha());
  45.        
  46.         String cadastraUser = new Gson().toJson(objeto);
  47.        
  48.         saveFileJson(cadastraUser);
  49.        
  50.     }
  51.    
  52.    
  53.     private static String transformaHash(String senha) throws NoSuchAlgorithmException{
  54.        
  55.        MessageDigest md = MessageDigest.getInstance("MD5");
  56.        md.update(senha.getBytes());
  57.        byte[] hashMd5 = md.digest();
  58.        String hashString = paraStringHexa(hashMd5);
  59.        //System.out.println(hashString);
  60.        String HashFinal = hashString.substring(0, 5);
  61.        //System.out.println(HashFinal);
  62.  
  63.        return HashFinal;  
  64.        
  65.     }
  66.    
  67.     private static String paraStringHexa(byte[] bytes) {
  68.         StringBuilder s = new StringBuilder();
  69.         for (int i = 0; i < bytes.length; i++) {
  70.             int parteAlta = ((bytes[i] >> 4) & 0xf) << 4;
  71.             int parteBaixa = bytes[i] & 0xf;
  72.             if (parteAlta == 0) s.append('0');
  73.             s.append(Integer.toHexString(parteAlta | parteBaixa));
  74.         }
  75.         return s.toString();
  76.     }
  77.    
  78.     public static void saveFileJson (String stringJson){
  79.        
  80.         try {
  81.    
  82.             File file = new File("cadastroUser.json");
  83.             try (FileWriter fileWriter = new FileWriter(file)) {
  84.                 fileWriter.write(stringJson);
  85.                 fileWriter.flush();
  86.             }
  87.         } catch (IOException e) {
  88.         }
  89.    
  90.    
  91.     }
  92.    
  93. //    private static gravaTxt(){
  94. //    
  95. //    
  96. //    
  97. //    
  98. //    }
  99. //    
  100. }
Add Comment
Please, Sign In to add comment