]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | package com.easysw.cups; |
2 | ||
3 | /** | |
4 | * @version 1.00 06-NOV-2002 | |
5 | * @author Easy Software Products | |
6 | * | |
7 | * Internet Printing Protocol definitions for the Common UNIX Printing | |
8 | * System (CUPS). | |
9 | * | |
10 | * Copyright 1997-2002 by Easy Software Products. | |
11 | * | |
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 | |
17 | * at: | |
18 | * | |
19 | * Attn: CUPS Licensing Information | |
20 | * Easy Software Products | |
21 | * 44141 Airport View Drive, Suite 204 | |
22 | * Hollywood, Maryland 20636-3111 USA | |
23 | * | |
24 | * Voice: (301) 373-9603 | |
25 | * EMail: cups-info@cups.org | |
26 | * WWW: http://www.cups.org | |
27 | */ | |
28 | ||
29 | /** | |
30 | * Digest MD5 password routines. | |
31 | * | |
32 | * @author TDB | |
33 | * @version 1.0 | |
34 | * @since JDK1.3 | |
35 | */ | |
36 | import java.security.*; | |
37 | ||
38 | public class IPPMD5 | |
39 | { | |
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'}; | |
44 | ||
45 | /** | |
46 | * Constructor is private so you must use the getInstance method | |
47 | */ | |
48 | private IPPMD5() throws NoSuchAlgorithmException | |
49 | { | |
50 | md = MessageDigest.getInstance("MD5"); | |
51 | } | |
52 | ||
53 | ||
54 | /** | |
55 | * This returns the singleton instance | |
56 | */ | |
57 | public static IPPMD5 getInstance() throws NoSuchAlgorithmException | |
58 | { | |
59 | ||
60 | if (md5 == null) | |
61 | { | |
62 | md5 = new IPPMD5(); | |
63 | } | |
64 | return (md5); | |
65 | } | |
66 | ||
67 | ||
68 | public String hashData(byte[] dataToHash) | |
69 | { | |
70 | return(hexStringFromBytes((calculateHash(dataToHash)))); | |
71 | } | |
72 | ||
73 | ||
74 | private byte[] calculateHash(byte[] dataToHash) | |
75 | { | |
76 | md.update(dataToHash, 0, dataToHash.length); | |
77 | return(md.digest()); | |
78 | } | |
79 | ||
80 | ||
81 | public String hexStringFromBytes(byte[] b) | |
82 | { | |
83 | String hex = ""; | |
84 | int msb; | |
85 | int lsb = 0; | |
86 | int i; | |
87 | ||
88 | // MSB maps to idx 0 | |
89 | for (i = 0; i < b.length; i++) | |
90 | { | |
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]; | |
96 | } | |
97 | return(hex); | |
98 | } | |
99 | ||
100 | ||
101 | ||
102 | ||
103 | ||
104 | public String MD5Digest( String user, String passwd, String realm, | |
105 | String method, String resource, String nonce ) | |
106 | { | |
107 | String tmp; | |
108 | String A1, A2; | |
109 | String auth_string = ""; | |
110 | ||
111 | try | |
112 | { | |
113 | tmp = user + ":" + realm + ":" + passwd; | |
114 | md = MessageDigest.getInstance("MD5"); | |
115 | A1 = hexStringFromBytes(md.digest(tmp.getBytes())); | |
116 | ||
117 | tmp = method + ":" + resource; | |
118 | md = MessageDigest.getInstance("MD5"); | |
119 | A2 = hexStringFromBytes(md.digest(tmp.getBytes())); | |
120 | ||
121 | tmp = A1 + ":" + nonce + ":" + A2; | |
122 | md = MessageDigest.getInstance("MD5"); | |
123 | auth_string = hexStringFromBytes(md.digest(tmp.getBytes())); | |
124 | return(auth_string); | |
125 | } | |
126 | catch (NoSuchAlgorithmException e) | |
127 | { | |
128 | } | |
129 | return(""); | |
130 | ||
131 | } | |
132 | ||
133 | ||
134 | } |