1 package com
.easysw
.cups
;
4 * @version 1.00 06-NOV-2002
7 * Internet Printing Protocol definitions for the Common UNIX Printing
10 * Copyright 2007 by Apple Inc.
11 * Copyright 1997-2002 by Easy Software Products.
13 * These coded instructions, statements, and computer programs are the
14 * property of Apple Inc. and are protected by Federal copyright
15 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
16 * which should have been included with this file. If this file is
17 * file is missing or damaged, see the license at "http://www.cups.org/".
21 * Digest MD5 password routines.
27 import java
.security
.*;
31 public MessageDigest md
= null;
32 static private IPPMD5 md5
= null;
33 private static final char[] hexChars
= {'0','1','2','3','4','5','6','7',
34 '8','9','a','b','c','d','e','f'};
37 * Constructor is private so you must use the getInstance method
39 private IPPMD5() throws NoSuchAlgorithmException
41 md
= MessageDigest
.getInstance("MD5");
46 * This returns the singleton instance
48 public static IPPMD5
getInstance() throws NoSuchAlgorithmException
59 public String
hashData(byte[] dataToHash
)
61 return(hexStringFromBytes((calculateHash(dataToHash
))));
65 private byte[] calculateHash(byte[] dataToHash
)
67 md
.update(dataToHash
, 0, dataToHash
.length
);
72 public String
hexStringFromBytes(byte[] b
)
80 for (i
= 0; i
< b
.length
; i
++)
82 msb
= ((int)b
[i
] & 0x000000FF) / 16;
83 lsb
= ((int)b
[i
] & 0x000000FF) % 16;
84 // System.out.println("I: " + i + " B: " + b[i] + " MSB: " +
85 // msb + " LSB: " + lsb );
86 hex
= hex
+ hexChars
[msb
] + hexChars
[lsb
];
95 public String
MD5Digest( String user
, String passwd
, String realm
,
96 String method
, String resource
, String nonce
)
100 String auth_string
= "";
104 tmp
= user
+ ":" + realm
+ ":" + passwd
;
105 md
= MessageDigest
.getInstance("MD5");
106 A1
= hexStringFromBytes(md
.digest(tmp
.getBytes()));
108 tmp
= method
+ ":" + resource
;
109 md
= MessageDigest
.getInstance("MD5");
110 A2
= hexStringFromBytes(md
.digest(tmp
.getBytes()));
112 tmp
= A1
+ ":" + nonce
+ ":" + A2
;
113 md
= MessageDigest
.getInstance("MD5");
114 auth_string
= hexStringFromBytes(md
.digest(tmp
.getBytes()));
117 catch (NoSuchAlgorithmException e
)