Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Diffi;
- import java.io.InputStream;
- import java.security.KeyFactory;
- import java.security.PublicKey;
- import java.security.spec.X509EncodedKeySpec;
- import java.sql.*;
- import org.mindrot.jbcrypt.BCrypt;
- public class conectBD {
- private Connection con;
- private Statement st;
- private ResultSet rs;
- private static final int logRounds=12;
- public static String hashpw(String password) {
- return BCrypt.hashpw(password, BCrypt.gensalt(logRounds));
- }
- public static boolean verifyHash(String password, String hash) {
- return BCrypt.checkpw(password, hash);
- }
- public conectBD(){
- try{
- Class.forName("com.mysql.jdbc.Driver");
- con= DriverManager.getConnection("jdbc:mysql://localhost:3306/si_test","root","");
- st= con.createStatement();
- }catch(Exception e){
- System.out.println("Error"+e);
- }
- }
- public boolean confirmLogin(String name,String password){
- String query="Select hash from user where name='"+name+"'";
- try{
- rs = st.executeQuery(query);
- if(rs.next()){
- String sHash = rs.getString("hash");
- return verifyHash(password, sHash);
- }
- }catch(Exception e){
- System.out.println("Error"+e);
- }
- return false;
- }
- public PublicKey userPublic(String name){
- String query="Select pubKey from user where name='"+name+"'";
- try{
- rs=st.executeQuery(query);
- if(rs.next()){
- byte[] bytes=rs.getBytes(1);
- KeyFactory kf=KeyFactory.getInstance("RSA");
- X509EncodedKeySpec pubKeySpec=new X509EncodedKeySpec(bytes);
- PublicKey publicKey=kf.generatePublic(pubKeySpec);
- return publicKey;
- }
- }catch(Exception e){
- System.out.println("Error"+e);
- }
- return null;
- }
- public void inserirUser(String nome, String hash, PublicKey key){
- byte[] chave=key.getEncoded();
- System.out.println("hello from insert");
- try {
- String insertSql = "INSERT INTO user(name,hash,pubKey) VALUES"
- + "( ? ,"
- + " ?,"
- + " ?);";
- PreparedStatement pstmt = con.prepareStatement(insertSql);
- pstmt.setString(1, nome);
- pstmt.setString(2,hash);
- pstmt.setBytes(3,chave);
- System.out.println(nome+"//"+chave+"//"+hash);
- pstmt.executeUpdate();
- }catch(Exception e){
- System.out.println("escrever na BD esta fdd");
- }
- }
- public void changePw(String name,String password){
- String query="Select hash from user where name='"+name+"'";
- try{
- rs = st.executeQuery(query);
- if(rs.next()){
- String sHash = rs.getString("hash");
- if (verifyHash(password, sHash))
- query="UPDATE user SET hash= ? where name='?'";
- PreparedStatement pstmt = con.prepareStatement(query);
- pstmt.setString(1, hashpw(password));
- pstmt.setString(2,name);
- pstmt.executeUpdate();
- }
- }catch(Exception e){
- System.out.println("Error"+e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement