// Path: src/main/java/us/monoid/psql/async/auth/MD5Digest.java
// public class MD5Digest {
// 	static final char lookup[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
// 	
// 	private MD5Digest() {
// 	}
// 
// 	/**
// 	 * Encodes user/password/salt information in the following way: MD5(MD5(password + user) + salt)
// 	 * 
// 	 * @param aUser The connecting user.
// 	 * 
// 	 * @param aPassword The connecting user's password.
// 	 * 
// 	 * @param salt A four-salt sent by the server.
// 	 * 
// 	 * @return A 35-byte array, comprising the string "md5" and an MD5 digest.
// 	 */
// 	public static byte[] encode(String aUser, char[] aPassword, byte salt[]) {
// 		MessageDigest md;
// 		byte[] temp_digest, pass_digest;
// 		byte[] hex_digest = new byte[35];
// 		byte[] user = toUTF8(aUser);
// 		byte[] password = toUTF8(new String(aPassword));
// 		
// 		try {
// 			md = MessageDigest.getInstance("MD5");
// 
// 			md.update(password);
// 			md.update(user);
// 			temp_digest = md.digest();
// 
// 			bytesToHex(temp_digest, hex_digest, 0);
// 			md.update(hex_digest, 0, 32);
// 			md.update(salt);
// 			pass_digest = md.digest();
// 
// 			bytesToHex(pass_digest, hex_digest, 3);
// 			hex_digest[0] = (byte) 'm';
// 			hex_digest[1] = (byte) 'd';
// 			hex_digest[2] = (byte) '5';
// 		} catch (Exception e) {
// 			; // "MessageDigest failure; " + e
// 		}
// 
// 		return hex_digest;
// 	}
// 	
// 	private static byte[] toUTF8(String aString) {
// 		try {
// 			return aString.getBytes("UTF-8");
// 		} catch (UnsupportedEncodingException e) {
// 		}
// 		throw new IllegalArgumentException("UTF8 encoding not found");
// 	}
// 
// 	/*
// 	 * Turn 16-byte stream into a human-readable 32-byte hex string
// 	 */
// 	private static void bytesToHex(byte[] bytes, byte[] hex, int offset) {
// 		int pos = offset;
// 
// 		for (int i = 0; i < 16; i++) {
// 			int c = bytes[i] & 0xFF;
// 			int j = c >> 4;
// 			hex[pos++] = (byte) lookup[j];
// 			j = (c & 0xF);
// 			hex[pos++] = (byte) lookup[j];
// 		}
// 	}
// }


import static org.junit.Assert.*;
import org.junit.Test;
import org.vertx.java.core.buffer.Buffer;
import us.monoid.psql.async.auth.MD5Digest;
package us.monoid.psql.async.message;




public class PasswordTest {

	@Test
	public void test() {
		Buffer buffer = new Buffer();
		Password p = new Password(buffer);
		char[] pwd = { 't','e','s','t' };
		Buffer b = p.write(pwd);
		assertNotNull(b);
		assertTrue(b.getByte(0) == 'p');
		assertTrue(b.toString().contains("test"));
		assertTrue(b.getByte(b.length() - 1) == 0);
		assertTrue(b.length() > 0);
	}

	@Test
	public void testMD5() {
		Buffer buffer = new Buffer();
		Password p = new Password(buffer);
		char[] pwd = { 't','e','s','t' };
		byte[] salt = { 1, 2, 3, 4 };
// Path: src/main/java/us/monoid/psql/async/auth/MD5Digest.java
// public class MD5Digest {
// 	static final char lookup[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
// 	
// 	private MD5Digest() {
// 	}
// 
// 	/**
// 	 * Encodes user/password/salt information in the following way: MD5(MD5(password + user) + salt)
// 	 * 
// 	 * @param aUser The connecting user.
// 	 * 
// 	 * @param aPassword The connecting user's password.
// 	 * 
// 	 * @param salt A four-salt sent by the server.
// 	 * 
// 	 * @return A 35-byte array, comprising the string "md5" and an MD5 digest.
// 	 */
// 	public static byte[] encode(String aUser, char[] aPassword, byte salt[]) {
// 		MessageDigest md;
// 		byte[] temp_digest, pass_digest;
// 		byte[] hex_digest = new byte[35];
// 		byte[] user = toUTF8(aUser);
// 		byte[] password = toUTF8(new String(aPassword));
// 		
// 		try {
// 			md = MessageDigest.getInstance("MD5");
// 
// 			md.update(password);
// 			md.update(user);
// 			temp_digest = md.digest();
// 
// 			bytesToHex(temp_digest, hex_digest, 0);
// 			md.update(hex_digest, 0, 32);
// 			md.update(salt);
// 			pass_digest = md.digest();
// 
// 			bytesToHex(pass_digest, hex_digest, 3);
// 			hex_digest[0] = (byte) 'm';
// 			hex_digest[1] = (byte) 'd';
// 			hex_digest[2] = (byte) '5';
// 		} catch (Exception e) {
// 			; // "MessageDigest failure; " + e
// 		}
// 
// 		return hex_digest;
// 	}
// 	
// 	private static byte[] toUTF8(String aString) {
// 		try {
// 			return aString.getBytes("UTF-8");
// 		} catch (UnsupportedEncodingException e) {
// 		}
// 		throw new IllegalArgumentException("UTF8 encoding not found");
// 	}
// 
// 	/*
// 	 * Turn 16-byte stream into a human-readable 32-byte hex string
// 	 */
// 	private static void bytesToHex(byte[] bytes, byte[] hex, int offset) {
// 		int pos = offset;
// 
// 		for (int i = 0; i < 16; i++) {
// 			int c = bytes[i] & 0xFF;
// 			int j = c >> 4;
// 			hex[pos++] = (byte) lookup[j];
// 			j = (c & 0xF);
// 			hex[pos++] = (byte) lookup[j];
// 		}
// 	}
// }


// Path: src/test/java/us/monoid/psql/async/message/PasswordTest.java
import static org.junit.Assert.*;
import org.junit.Test;
import org.vertx.java.core.buffer.Buffer;
import us.monoid.psql.async.auth.MD5Digest;

package us.monoid.psql.async.message;




public class PasswordTest {

	@Test
	public void test() {
		Buffer buffer = new Buffer();
		Password p = new Password(buffer);
		char[] pwd = { 't','e','s','t' };
		Buffer b = p.write(pwd);
		assertNotNull(b);
		assertTrue(b.getByte(0) == 'p');
		assertTrue(b.toString().contains("test"));
		assertTrue(b.getByte(b.length() - 1) == 0);
		assertTrue(b.length() > 0);
	}

	@Test
	public void testMD5() {
		Buffer buffer = new Buffer();
		Password p = new Password(buffer);
		char[] pwd = { 't','e','s','t' };
		byte[] salt = { 1, 2, 3, 4 };
		Buffer b = p.write(MD5Digest.encode("test", pwd, salt));