private void secureAuth(Buffer packet,int packLength,String user,String password,String database,boolean writeClientParams) throws SQLException {
  if (packet == null) {
    packet=new Buffer(packLength);
  }
  if (writeClientParams) {
    if (this.use41Extensions) {
      if (versionMeetsMinimum(4,1,1)) {
        packet.writeLong(this.clientParam);
        packet.writeLong(this.maxThreeBytes);
        packet.writeByte((byte)8);
        packet.writeBytesNoNull(new byte[23]);
      }
 else {
        packet.writeLong(this.clientParam);
        packet.writeLong(this.maxThreeBytes);
      }
    }
 else {
      packet.writeInt((int)this.clientParam);
      packet.writeLongInt(this.maxThreeBytes);
    }
  }
  packet.writeString(user,CODE_PAGE_1252,this.connection);
  if (password.length() != 0) {
    packet.writeString(FALSE_SCRAMBLE,CODE_PAGE_1252,this.connection);
  }
 else {
    packet.writeString("",CODE_PAGE_1252,this.connection);
  }
  if (this.useConnectWithDb) {
    packet.writeString(database,CODE_PAGE_1252,this.connection);
  }
  send(packet,packet.getPosition());
  if (password.length() > 0) {
    Buffer b=readPacket();
    b.setPosition(0);
    byte[] replyAsBytes=b.getByteBuffer();
    if ((replyAsBytes.length == 24) && (replyAsBytes[0] != 0)) {
      if (replyAsBytes[0] != '*') {
        try {
          byte[] buff=Security.passwordHashStage1(password);
          byte[] passwordHash=new byte[buff.length];
          System.arraycopy(buff,0,passwordHash,0,buff.length);
          passwordHash=Security.passwordHashStage2(passwordHash,replyAsBytes);
          byte[] packetDataAfterSalt=new byte[replyAsBytes.length - 4];
          System.arraycopy(replyAsBytes,4,packetDataAfterSalt,0,replyAsBytes.length - 4);
          byte[] mysqlScrambleBuff=new byte[SEED_LENGTH];
          Security.xorString(packetDataAfterSalt,mysqlScrambleBuff,passwordHash,SEED_LENGTH);
          Security.xorString(mysqlScrambleBuff,buff,buff,SEED_LENGTH);
          Buffer packet2=new Buffer(25);
          packet2.writeBytesNoNull(buff);
          this.packetSequence++;
          send(packet2,24);
        }
 catch (        NoSuchAlgorithmException nse) {
          throw SQLError.createSQLException(Messages.getString("MysqlIO.91") + Messages.getString("MysqlIO.92"),SQLError.SQL_STATE_GENERAL_ERROR,getExceptionInterceptor());
        }
      }
 else {
        try {
          byte[] passwordHash=Security.createKeyFromOldPassword(password);
          byte[] netReadPos4=new byte[replyAsBytes.length - 4];
          System.arraycopy(replyAsBytes,4,netReadPos4,0,replyAsBytes.length - 4);
          byte[] mysqlScrambleBuff=new byte[SEED_LENGTH];
          Security.xorString(netReadPos4,mysqlScrambleBuff,passwordHash,SEED_LENGTH);
          String scrambledPassword=Util.scramble(StringUtils.toString(mysqlScrambleBuff),password);
          Buffer packet2=new Buffer(packLength);
          packet2.writeString(scrambledPassword,CODE_PAGE_1252,this.connection);
          this.packetSequence++;
          send(packet2,24);
        }
 catch (        NoSuchAlgorithmException nse) {
          throw SQLError.createSQLException(Messages.getString("MysqlIO.91") + Messages.getString("MysqlIO.92"),SQLError.SQL_STATE_GENERAL_ERROR,getExceptionInterceptor());
        }
      }
    }
  }
}

private void secureAuth ( Buffer packet , int packLength , String user , String password , String database , boolean writeClientParams ) throws SQLException { if ( packet == null ) { packet = new Buffer ( packLength ) ; } if ( writeClientParams ) { if ( this . use41Extensions ) { if ( versionMeetsMinimum ( 4 , 1 , 1 ) ) { packet . writeLong ( this . clientParam ) ; packet . writeLong ( this . maxThreeBytes ) ; packet . writeByte ( ( byte ) 8 ) ; packet . writeBytesNoNull ( new byte [ 23 ] ) ; } else { packet . writeLong ( this . clientParam ) ; packet . writeLong ( this . maxThreeBytes ) ; } } else { packet . writeInt ( ( int ) this . clientParam ) ; packet . writeLongInt ( this . maxThreeBytes ) ; } } packet . writeString ( user , CODE_PAGE_1252 , this . connection ) ; if ( password . length ( ) != 0 ) { packet . writeString ( FALSE_SCRAMBLE , CODE_PAGE_1252 , this . connection ) ; } else { packet . writeString ( "" , CODE_PAGE_1252 , this . connection ) ; } if ( this . useConnectWithDb ) { packet . writeString ( database , CODE_PAGE_1252 , this . connection ) ; } send ( packet , packet . getPosition ( ) ) ; if ( password . length ( ) > 0 ) { Buffer b = readPacket ( ) ; b . setPosition ( 0 ) ; byte [ ] replyAsBytes = b . getByteBuffer ( ) ; if ( ( replyAsBytes . length == 24 ) && ( replyAsBytes [ 0 ] != 0 ) ) { if ( replyAsBytes [ 0 ] != '*' ) { try { byte [ ] buff = Security . passwordHashStage1 ( password ) ; byte [ ] passwordHash = new byte [ buff . length ] ; System . arraycopy ( buff , 0 , passwordHash , 0 , buff . length ) ; passwordHash = Security . passwordHashStage2 ( passwordHash , replyAsBytes ) ; byte [ ] packetDataAfterSalt = new byte [ replyAsBytes . length - 4 ] ; System . arraycopy ( replyAsBytes , 4 , packetDataAfterSalt , 0 , replyAsBytes . length - 4 ) ; byte [ ] mysqlScrambleBuff = new byte [ SEED_LENGTH ] ; Security . xorString ( packetDataAfterSalt , mysqlScrambleBuff , passwordHash , SEED_LENGTH ) ; Security . xorString ( mysqlScrambleBuff , buff , buff , SEED_LENGTH ) ; Buffer packet2 = new Buffer ( 25 ) ; packet2 . writeBytesNoNull ( buff ) ; this . packetSequence ++ ; send ( packet2 , 24 ) ; } catch ( NoSuchAlgorithmException nse ) { throw SQLError . createSQLException ( Messages . getString ( "MysqlIO.91" ) + Messages . getString ( "MysqlIO.92" ) , SQLError . SQL_STATE_GENERAL_ERROR , getExceptionInterceptor ( ) ) ; } } else { try { byte [ ] passwordHash = Security . createKeyFromOldPassword ( password ) ; byte [ ] netReadPos4 = new byte [ replyAsBytes . length - 4 ] ; System . arraycopy ( replyAsBytes , 4 , netReadPos4 , 0 , replyAsBytes . length - 4 ) ; byte [ ] mysqlScrambleBuff = new byte [ SEED_LENGTH ] ; Security . xorString ( netReadPos4 , mysqlScrambleBuff , passwordHash , SEED_LENGTH ) ; String scrambledPassword = Util . scramble ( StringUtils . toString ( mysqlScrambleBuff ) , password ) ; Buffer packet2 = new Buffer ( packLength ) ; packet2 . writeString ( scrambledPassword , CODE_PAGE_1252 , this . connection ) ; this . packetSequence ++ ; send ( packet2 , 24 ) ; } catch ( NoSuchAlgorithmException nse ) { throw SQLError . createSQLException ( Messages . getString ( "MysqlIO.91" ) + Messages . getString ( "MysqlIO.92" ) , SQLError . SQL_STATE_GENERAL_ERROR , getExceptionInterceptor ( ) ) ; } } } } }
secure authentication for 4 . 1 and newer servers .
