Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. package Diffi;
  2.  
  3. import java.io.InputStream;
  4. import java.security.KeyFactory;
  5. import java.security.PublicKey;
  6. import java.security.spec.X509EncodedKeySpec;
  7. import java.sql.*;
  8. import org.mindrot.jbcrypt.BCrypt;
  9.  
  10. public class conectBD {
  11. private Connection con;
  12. private Statement st;
  13. private ResultSet rs;
  14. private static final int logRounds=12;
  15.  
  16. public static String hashpw(String password) {
  17. return BCrypt.hashpw(password, BCrypt.gensalt(logRounds));
  18. }
  19.  
  20. public static boolean verifyHash(String password, String hash) {
  21. return BCrypt.checkpw(password, hash);
  22. }
  23.  
  24. public conectBD(){
  25. try{
  26. Class.forName("com.mysql.jdbc.Driver");
  27. con= DriverManager.getConnection("jdbc:mysql://localhost:3306/si_test","root","");
  28. st= con.createStatement();
  29. }catch(Exception e){
  30. System.out.println("Error"+e);
  31. }
  32. }
  33. public boolean confirmLogin(String name,String password){
  34. String query="Select hash from user where name='"+name+"'";
  35. try{
  36. rs = st.executeQuery(query);
  37. if(rs.next()){
  38. String sHash = rs.getString("hash");
  39. return verifyHash(password, sHash);
  40. }
  41. }catch(Exception e){
  42. System.out.println("Error"+e);
  43. }
  44. return false;
  45. }
  46.  
  47. public PublicKey userPublic(String name){
  48. String query="Select pubKey from user where name='"+name+"'";
  49. try{
  50. rs=st.executeQuery(query);
  51. if(rs.next()){
  52. byte[] bytes=rs.getBytes(1);
  53. KeyFactory kf=KeyFactory.getInstance("RSA");
  54. X509EncodedKeySpec pubKeySpec=new X509EncodedKeySpec(bytes);
  55. PublicKey publicKey=kf.generatePublic(pubKeySpec);
  56. return publicKey;
  57. }
  58. }catch(Exception e){
  59. System.out.println("Error"+e);
  60. }
  61. return null;
  62. }
  63. public void inserirUser(String nome, String hash, PublicKey key){
  64. byte[] chave=key.getEncoded();
  65. System.out.println("hello from insert");
  66. try {
  67. String insertSql = "INSERT INTO user(name,hash,pubKey) VALUES"
  68. + "( ? ,"
  69. + " ?,"
  70. + " ?);";
  71.  
  72. PreparedStatement pstmt = con.prepareStatement(insertSql);
  73. pstmt.setString(1, nome);
  74. pstmt.setString(2,hash);
  75. pstmt.setBytes(3,chave);
  76. System.out.println(nome+"//"+chave+"//"+hash);
  77. pstmt.executeUpdate();
  78. }catch(Exception e){
  79. System.out.println("escrever na BD esta fdd");
  80. }
  81. }
  82.  
  83. public void changePw(String name,String password){
  84. String query="Select hash from user where name='"+name+"'";
  85. try{
  86. rs = st.executeQuery(query);
  87. if(rs.next()){
  88. String sHash = rs.getString("hash");
  89. if (verifyHash(password, sHash))
  90. query="UPDATE user SET hash= ? where name='?'";
  91. PreparedStatement pstmt = con.prepareStatement(query);
  92. pstmt.setString(1, hashpw(password));
  93. pstmt.setString(2,name);
  94. pstmt.executeUpdate();
  95. }
  96. }catch(Exception e){
  97. System.out.println("Error"+e);
  98. }
  99. }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement