1
package org.postgresql.util;
4
* MD5-based utility function to obfuscate passwords before network transmission
7
* $Id: MD5Digest.java,v 1.3 2001/11/25 23:26:59 barry Exp $
10
import java.security.*;
12
public class MD5Digest
19
* Encodes user/password/salt information in the following way:
20
* MD5(MD5(password + user) + salt)
22
* @param user The connecting user.
23
* @param password The connecting user's password.
24
* @param salt A four-character string sent by the server.
26
* @return A 35-byte array, comprising the string "md5" and an MD5 digest.
28
public static byte[] encode(String user, String password, String salt)
31
byte[] temp_digest, pass_digest;
32
byte[] hex_digest = new byte[35];
36
md = MessageDigest.getInstance("MD5");
38
md.update(password.getBytes());
39
md.update(user.getBytes());
40
temp_digest = md.digest();
42
bytesToHex(temp_digest, hex_digest, 0);
43
md.update(hex_digest, 0, 32);
44
md.update(salt.getBytes());
45
pass_digest = md.digest();
47
bytesToHex(pass_digest, hex_digest, 3);
48
hex_digest[0] = (byte) 'm';
49
hex_digest[1] = (byte) 'd';
50
hex_digest[2] = (byte) '5';
54
; // "MessageDigest failure; " + e
62
* Turn 16-byte stream into a human-readable 32-byte hex string
64
private static void bytesToHex(byte[] bytes, byte[] hex, int offset)
66
final char lookup[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
67
'a', 'b', 'c', 'd', 'e', 'f' };
69
int i, c, j, pos = offset;
71
for (i = 0; i < 16; i++)
75
hex[pos++] = (byte) lookup[j];
77
hex[pos++] = (byte) lookup[j];