]> git.ipfire.org Git - thirdparty/cups.git/blob - scripting/java/src/com/easysw/cups/IPPMD5.java
Add missing files for test.
[thirdparty/cups.git] / scripting / java / src / com / easysw / cups / IPPMD5.java
1 package com.easysw.cups;
2
3 /**
4 * @version 1.00 06-NOV-2002
5 * @author Apple Inc.
6 *
7 * Internet Printing Protocol definitions for the Common UNIX Printing
8 * System (CUPS).
9 *
10 * Copyright 2007 by Apple Inc.
11 * Copyright 1997-2002 by Easy Software Products.
12 *
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/".
18 */
19
20 /**
21 * Digest MD5 password routines.
22 *
23 * @author TDB
24 * @version 1.0
25 * @since JDK1.3
26 */
27 import java.security.*;
28
29 public class IPPMD5
30 {
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'};
35
36 /**
37 * Constructor is private so you must use the getInstance method
38 */
39 private IPPMD5() throws NoSuchAlgorithmException
40 {
41 md = MessageDigest.getInstance("MD5");
42 }
43
44
45 /**
46 * This returns the singleton instance
47 */
48 public static IPPMD5 getInstance() throws NoSuchAlgorithmException
49 {
50
51 if (md5 == null)
52 {
53 md5 = new IPPMD5();
54 }
55 return (md5);
56 }
57
58
59 public String hashData(byte[] dataToHash)
60 {
61 return(hexStringFromBytes((calculateHash(dataToHash))));
62 }
63
64
65 private byte[] calculateHash(byte[] dataToHash)
66 {
67 md.update(dataToHash, 0, dataToHash.length);
68 return(md.digest());
69 }
70
71
72 public String hexStringFromBytes(byte[] b)
73 {
74 String hex = "";
75 int msb;
76 int lsb = 0;
77 int i;
78
79 // MSB maps to idx 0
80 for (i = 0; i < b.length; i++)
81 {
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];
87 }
88 return(hex);
89 }
90
91
92
93
94
95 public String MD5Digest( String user, String passwd, String realm,
96 String method, String resource, String nonce )
97 {
98 String tmp;
99 String A1, A2;
100 String auth_string = "";
101
102 try
103 {
104 tmp = user + ":" + realm + ":" + passwd;
105 md = MessageDigest.getInstance("MD5");
106 A1 = hexStringFromBytes(md.digest(tmp.getBytes()));
107
108 tmp = method + ":" + resource;
109 md = MessageDigest.getInstance("MD5");
110 A2 = hexStringFromBytes(md.digest(tmp.getBytes()));
111
112 tmp = A1 + ":" + nonce + ":" + A2;
113 md = MessageDigest.getInstance("MD5");
114 auth_string = hexStringFromBytes(md.digest(tmp.getBytes()));
115 return(auth_string);
116 }
117 catch (NoSuchAlgorithmException e)
118 {
119 }
120 return("");
121
122 }
123
124
125 }