1 package com
.easysw
.cups
;
4 * @version 1.00 06-NOV-2002
5 * @author Easy Software Products
7 * Internet Printing Protocol definitions for the Common UNIX Printing
10 * Copyright 1997-2002 by Easy Software Products.
12 * These coded instructions, statements, and computer programs are the
13 * property of Easy Software Products and are protected by Federal
14 * copyright law. Distribution and use rights are outlined in the file
15 * "LICENSE.txt" which should have been included with this file. If this
16 * file is missing or damaged please contact Easy Software Products
19 * Attn: CUPS Licensing Information
20 * Easy Software Products
21 * 44141 Airport View Drive, Suite 204
22 * Hollywood, Maryland 20636-3111 USA
24 * Voice: (301) 373-9603
25 * EMail: cups-info@cups.org
26 * WWW: http://www.cups.org
30 * Digest MD5 password routines.
36 import java
.security
.*;
40 public MessageDigest md
= null;
41 static private IPPMD5 md5
= null;
42 private static final char[] hexChars
= {'0','1','2','3','4','5','6','7',
43 '8','9','a','b','c','d','e','f'};
46 * Constructor is private so you must use the getInstance method
48 private IPPMD5() throws NoSuchAlgorithmException
50 md
= MessageDigest
.getInstance("MD5");
55 * This returns the singleton instance
57 public static IPPMD5
getInstance() throws NoSuchAlgorithmException
68 public String
hashData(byte[] dataToHash
)
70 return(hexStringFromBytes((calculateHash(dataToHash
))));
74 private byte[] calculateHash(byte[] dataToHash
)
76 md
.update(dataToHash
, 0, dataToHash
.length
);
81 public String
hexStringFromBytes(byte[] b
)
89 for (i
= 0; i
< b
.length
; i
++)
91 msb
= ((int)b
[i
] & 0x000000FF) / 16;
92 lsb
= ((int)b
[i
] & 0x000000FF) % 16;
93 // System.out.println("I: " + i + " B: " + b[i] + " MSB: " +
94 // msb + " LSB: " + lsb );
95 hex
= hex
+ hexChars
[msb
] + hexChars
[lsb
];
104 public String
MD5Digest( String user
, String passwd
, String realm
,
105 String method
, String resource
, String nonce
)
109 String auth_string
= "";
113 tmp
= user
+ ":" + realm
+ ":" + passwd
;
114 md
= MessageDigest
.getInstance("MD5");
115 A1
= hexStringFromBytes(md
.digest(tmp
.getBytes()));
117 tmp
= method
+ ":" + resource
;
118 md
= MessageDigest
.getInstance("MD5");
119 A2
= hexStringFromBytes(md
.digest(tmp
.getBytes()));
121 tmp
= A1
+ ":" + nonce
+ ":" + A2
;
122 md
= MessageDigest
.getInstance("MD5");
123 auth_string
= hexStringFromBytes(md
.digest(tmp
.getBytes()));
126 catch (NoSuchAlgorithmException e
)