~ubuntu-branches/ubuntu/feisty/libpgjava/feisty

« back to all changes in this revision

Viewing changes to src/interfaces/jdbc/org/postgresql/util/MD5Digest.java

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Gybas
  • Date: 2002-02-06 23:43:06 UTC
  • Revision ID: james.westby@ubuntu.com-20020206234306-hsg7suqr8q56qg40
Tags: upstream-7.2
ImportĀ upstreamĀ versionĀ 7.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.postgresql.util;
 
2
 
 
3
/*
 
4
 * MD5-based utility function to obfuscate passwords before network transmission
 
5
 *
 
6
 * @author Jeremy Wohl
 
7
 * $Id: MD5Digest.java,v 1.3 2001/11/25 23:26:59 barry Exp $
 
8
 */
 
9
 
 
10
import java.security.*;
 
11
 
 
12
public class MD5Digest
 
13
{
 
14
        private MD5Digest()
 
15
        {}
 
16
 
 
17
 
 
18
        /*
 
19
         * Encodes user/password/salt information in the following way:
 
20
         *       MD5(MD5(password + user) + salt)
 
21
         *
 
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.
 
25
         *
 
26
         * @return      A 35-byte array, comprising the string "md5" and an MD5 digest.
 
27
         */
 
28
        public static byte[] encode(String user, String password, String salt)
 
29
        {
 
30
                MessageDigest md;
 
31
                byte[] temp_digest, pass_digest;
 
32
                byte[] hex_digest = new byte[35];
 
33
 
 
34
                try
 
35
                {
 
36
                        md = MessageDigest.getInstance("MD5");
 
37
 
 
38
                        md.update(password.getBytes());
 
39
                        md.update(user.getBytes());
 
40
                        temp_digest = md.digest();
 
41
 
 
42
                        bytesToHex(temp_digest, hex_digest, 0);
 
43
                        md.update(hex_digest, 0, 32);
 
44
                        md.update(salt.getBytes());
 
45
                        pass_digest = md.digest();
 
46
 
 
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';
 
51
                }
 
52
                catch (Exception e)
 
53
                {
 
54
                        ; // "MessageDigest failure; " + e
 
55
                }
 
56
 
 
57
                return hex_digest;
 
58
        }
 
59
 
 
60
 
 
61
        /*
 
62
         * Turn 16-byte stream into a human-readable 32-byte hex string
 
63
         */
 
64
        private static void bytesToHex(byte[] bytes, byte[] hex, int offset)
 
65
        {
 
66
                final char lookup[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 
67
                                                                'a', 'b', 'c', 'd', 'e', 'f' };
 
68
 
 
69
                int i, c, j, pos = offset;
 
70
 
 
71
                for (i = 0; i < 16; i++)
 
72
                {
 
73
                        c = bytes[i] & 0xFF;
 
74
                        j = c >> 4;
 
75
                        hex[pos++] = (byte) lookup[j];
 
76
                        j = (c & 0xF);
 
77
                        hex[pos++] = (byte) lookup[j];
 
78
                }
 
79
        }
 
80
}