public static String hashpw(byte[] password0,final String salt){
  BCrypt B;
  String real_salt;
  byte passwordb[]=null, saltb[], hashed[];
  char minor=(char)0;
  int rounds, off=0;
  final StringBuffer rs=new StringBuffer();
  if (salt.charAt(0) != '$' || salt.charAt(1) != '2') {
    throw new IllegalArgumentException("Invalid salt version");
  }
  if (salt.charAt(2) == '$') {
    off=3;
  }
 else {
    minor=salt.charAt(2);
    if (minor != 'a' || salt.charAt(3) != '$') {
      throw new IllegalArgumentException("Invalid salt revision");
    }
    off=4;
  }
  if (salt.charAt(off + 2) > '$') {
    throw new IllegalArgumentException("Missing salt rounds");
  }
  rounds=Integer.parseInt(salt.substring(off,off + 2));
  real_salt=salt.substring(off + 3,off + 25);
  try {
    if (minor >= (byte)'a') {
      passwordb=Arrays.copyOf(password0,password0.length + 1);
      passwordb[passwordb.length - 1]=(byte)0;
    }
 else {
      passwordb=Arrays.copyOf(password0,password0.length);
    }
    saltb=decode_base64(real_salt,BCRYPT_SALT_LEN);
    B=new BCrypt();
    hashed=B.crypt_raw(passwordb,saltb,rounds,bf_crypt_ciphertext.clone());
  }
  finally {
    if (password0 != null) {
      Arrays.fill(password0,(byte)'\0');
      password0=null;
    }
    if (passwordb != null) {
      Arrays.fill(passwordb,(byte)'\0');
      passwordb=null;
    }
  }
  rs.append("$2");
  if (minor >= 'a') {
    rs.append(minor);
  }
  rs.append("$");
  if (rounds < 10) {
    rs.append("0");
  }
  if (rounds > 30) {
    throw new IllegalArgumentException("rounds exceeds maximum (30)");
  }
  rs.append(Integer.toString(rounds));
  rs.append("$");
  rs.append(encode_base64(saltb,saltb.length));
  rs.append(encode_base64(hashed,bf_crypt_ciphertext.length * 4 - 1));
  return rs.toString();
}

public static String hashpw ( byte [ ] password0 , final String salt ) { BCrypt B ; String real_salt ; byte passwordb [ ] = null , saltb [ ] , hashed [ ] ; char minor = ( char ) 0 ; int rounds , off = 0 ; final StringBuffer rs = new StringBuffer ( ) ; if ( salt . charAt ( 0 ) != '$' || salt . charAt ( 1 ) != '2' ) { throw new IllegalArgumentException ( "Invalid salt version" ) ; } if ( salt . charAt ( 2 ) == '$' ) { off = 3 ; } else { minor = salt . charAt ( 2 ) ; if ( minor != 'a' || salt . charAt ( 3 ) != '$' ) { throw new IllegalArgumentException ( "Invalid salt revision" ) ; } off = 4 ; } if ( salt . charAt ( off + 2 ) > '$' ) { throw new IllegalArgumentException ( "Missing salt rounds" ) ; } rounds = Integer . parseInt ( salt . substring ( off , off + 2 ) ) ; real_salt = salt . substring ( off + 3 , off + 25 ) ; try { if ( minor >= ( byte ) 'a' ) { passwordb = Arrays . copyOf ( password0 , password0 . length + 1 ) ; passwordb [ passwordb . length - 1 ] = ( byte ) 0 ; } else { passwordb = Arrays . copyOf ( password0 , password0 . length ) ; } saltb = decode_base64 ( real_salt , BCRYPT_SALT_LEN ) ; B = new BCrypt ( ) ; hashed = B . crypt_raw ( passwordb , saltb , rounds , bf_crypt_ciphertext . clone ( ) ) ; } finally { if ( password0 != null ) { Arrays . fill ( password0 , ( byte ) '\0' ) ; password0 = null ; } if ( passwordb != null ) { Arrays . fill ( passwordb , ( byte ) '\0' ) ; passwordb = null ; } } rs . append ( "$2" ) ; if ( minor >= 'a' ) { rs . append ( minor ) ; } rs . append ( "$" ) ; if ( rounds < 10 ) { rs . append ( "0" ) ; } if ( rounds > 30 ) { throw new IllegalArgumentException ( "rounds exceeds maximum (30)" ) ; } rs . append ( Integer . toString ( rounds ) ) ; rs . append ( "$" ) ; rs . append ( encode_base64 ( saltb , saltb . length ) ) ; rs . append ( encode_base64 ( hashed , bf_crypt_ciphertext . length * 4 - 1 ) ) ; return rs . toString ( ) ; }
hash a password using the openbsd bcrypt scheme
