public static byte[] encryptPassword(final String password, final byte[] seed,
      String passwordCharacterEncoding)
      throws NoSuchAlgorithmException, UnsupportedEncodingException {

    if (password == null || password.isEmpty()) {
      return new byte[0];
    }

    final MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
    byte[] bytePwd;
    if (passwordCharacterEncoding != null && !passwordCharacterEncoding.isEmpty()) {
      bytePwd = password.getBytes(passwordCharacterEncoding);
    } else {
      bytePwd = password.getBytes();
    }

    final byte[] stage1 = messageDigest.digest(bytePwd);
    messageDigest.reset();

    final byte[] stage2 = messageDigest.digest(stage1);
    messageDigest.reset();

    messageDigest.update(seed);
    messageDigest.update(stage2);

    final byte[] digest = messageDigest.digest();
    final byte[] returnBytes = new byte[digest.length];
    for (int i = 0; i < digest.length; i++) {
      returnBytes[i] = (byte) (stage1[i] ^ digest[i]);
    }
    return returnBytes;
  }
public static byte [ ] encryptPassword ( final String password , final byte [ ] seed , String passwordCharacterEncoding ) throws NoSuchAlgorithmException , UnsupportedEncodingException { if ( password == null || password . isEmpty ( ) ) { return new byte [ 0 ] ; } final MessageDigest messageDigest = MessageDigest . getInstance ( "SHA-1" ) ; byte [ ] bytePwd ; if ( passwordCharacterEncoding != null && ! passwordCharacterEncoding . isEmpty ( ) ) { bytePwd = password . getBytes ( passwordCharacterEncoding ) ; } else { bytePwd = password . getBytes ( ) ; } final byte [ ] stage1 = messageDigest . digest ( bytePwd ) ; messageDigest . reset ( ) ; final byte [ ] stage2 = messageDigest . digest ( stage1 ) ; messageDigest . reset ( ) ; messageDigest . update ( seed ) ; messageDigest . update ( stage2 ) ; final byte [ ] digest = messageDigest . digest ( ) ; final byte [ ] returnBytes = new byte [ digest . length ] ; for ( int i = 0 ; i < digest . length ; i ++ ) { returnBytes [ i ] = ( byte ) ( stage1 [ i ] ^ digest [ i ] ) ; } return returnBytes ; }
Encrypts a password.

<p>protocol for authentication is like this: 1. Server sends a random array of bytes (the
seed) 2. client makes a sha1 digest of the password 3. client hashes the output of 2 4. client
digests the seed 5. client updates the digest with the output from 3 6. an xor of the output of
5 and 2 is sent to server 7. server does the same thing and verifies that the scrambled
passwords match</p>

@param password                  the password to encrypt
@param seed                      the seed to use
@param passwordCharacterEncoding password character encoding
@return a scrambled password
@throws NoSuchAlgorithmException     if SHA1 is not available on the platform we are using
@throws UnsupportedEncodingException if passwordCharacterEncoding is not a valid charset name