title: Converting VARBINARY values into String in a Select Query in MySQL?
body: <p>According to this <a href="https://stackoverflow.com/questions/5881169/what-column-type-length-should-i-use-for-storing-a-bcrypt-hashed-password-in-a-d">answer</a>, I should save my <strong>String</strong> BCrypt hashed password, <code>passHash</code>, as a <code>BINARY</code> or <code>BINARY(60)</code> (I chose <code>BINARY(60)</code>) when storing it in my MySQL table (I stored it in a column named <code>passHash</code>).</p>
  
  <p>Now when I select and retrieve that <code>passHash</code> column value from my table into Java, it is now a <code>byte[]</code> data type.</p>
  
  <p>How do I then convert it back to its <strong>String</strong> form so that I can validate it using my <code>validateLogin()</code> method below:</p>
  
  <pre><code>//Validate username and password login
      public boolean validateLogin(String username, String userpass) 
      {
          boolean status = false;  
          PreparedStatement pst = null; 
          ResultSet rs = null;  
  
          User user = new User(); //This is my Java bean class
  
          try(Connection connect= DBConnection.getConnection())
          {
              //Here, passHash is stored as VARBINARY(60) 
              pst = connect.prepareStatement("SELECT * FROM user WHERE username=? and passHash=?;"); 
  
              pst.setString(1, username);  
              pst.setString(2, userpass);  
              //Here's where I'm having difficulty because `passHash` column in my user table is VARBINARY(60) while `userpass` is a String
  
              rs = pst.executeQuery(); 
              status = rs.next();  
          } 
  
          catch (SQLException e) 
          {
              e.printStackTrace();
          }
              return status;  //status will return `true` if `username` and `userpass` matches what's in the columns
      }
  </code></pre>
  
  <p>The parameters <code>username</code> and <code>userpass</code> are being used to get the user's input in my <code>Login.jsp</code> form:</p>
  
  <pre><code>String username = request.getParameter("username");
  String userpass = request.getParameter("userpass");
  </code></pre>
  
  <p><strong>EDIT</strong>:  My BCrypt code is as follows:</p>
  
  <pre><code>//returns a hashed String value
  public static String bCrypt (String passPlain) {
          return BCrypt.hashpw(passPlain, BCrypt.gensalt(10));
      }
  
  //Returns a true if plain password is a match with hashed password
  public static Boolean isMatch(String passPlain, String passHash){
          return (BCrypt.checkpw(passPlain, passHash));
      }
  </code></pre>