001: // serverCodings.java
002: // -----------------------
003: // (C) by Michael Peter Christen; mc@anomic.de
004: // first published on http://www.anomic.de
005: // Frankfurt, Germany, 2004
006: // last major change: 29.04.2004
007: //
008: // $LastChangedDate: 2008-01-29 10:12:48 +0000 (Di, 29 Jan 2008) $
009: // $LastChangedRevision: 4414 $
010: // $LastChangedBy: orbiter $
011: //
012: // This program is free software; you can redistribute it and/or modify
013: // it under the terms of the GNU General Public License as published by
014: // the Free Software Foundation; either version 2 of the License, or
015: // (at your option) any later version.
016: //
017: // This program is distributed in the hope that it will be useful,
018: // but WITHOUT ANY WARRANTY; without even the implied warranty of
019: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: // GNU General Public License for more details.
021: //
022: // You should have received a copy of the GNU General Public License
023: // along with this program; if not, write to the Free Software
024: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
025: //
026: // Using this software in any meaning (reading, learning, copying, compiling,
027: // running) means that you agree that the Author(s) is (are) not responsible
028: // for cost, loss of data or any harm that may be caused directly or indirectly
029: // by usage of this softare or this documentation. The usage of this software
030: // is on your own risk. The installation and usage (starting/running) of this
031: // software may allow other people or application to access your computer and
032: // any attached devices and is highly dependent on the configuration of the
033: // software which must be done by the user of the software; the author(s) is
034: // (are) also not responsible for proper configuration and usage of the
035: // software, even if provoked by documentation provided together with
036: // the software.
037: //
038: // Any changes to this file according to the GPL as documented in the file
039: // gpl.txt aside this file in the shipment you received can be done to the
040: // lines that follows this copyright notice here, but changes must not be
041: // done inside the copyright notive above. A re-distribution must contain
042: // the intact and unchanged copyright notice.
043: // Contributions and changes to the program code must be marked as such.
044:
045: package de.anomic.server;
046:
047: import java.io.BufferedInputStream;
048: import java.io.ByteArrayInputStream;
049: import java.io.File;
050: import java.io.FileInputStream;
051: import java.io.InputStream;
052: import java.security.MessageDigest;
053: import java.util.Collections;
054: import java.util.HashMap;
055: import java.util.HashSet;
056: import java.util.Iterator;
057: import java.util.Map;
058: import java.util.Properties;
059: import java.util.Set;
060: import java.util.StringTokenizer;
061: import java.util.Map.Entry;
062:
063: public final class serverCodings {
064:
065: public static String encodeHex(long in, int length) {
066: String s = Long.toHexString(in);
067: while (s.length() < length)
068: s = "0" + s;
069: return s;
070: }
071:
072: public static String encodeOctal(byte[] in) {
073: if (in == null)
074: return "";
075: StringBuffer result = new StringBuffer(in.length * 8 / 3);
076: for (int i = 0; i < in.length; i++) {
077: if ((0Xff & in[i]) < 8)
078: result.append('0');
079: result.append(Integer.toOctalString(0Xff & in[i]));
080: }
081: return new String(result);
082: }
083:
084: public static String encodeHex(byte[] in) {
085: if (in == null)
086: return "";
087: StringBuffer result = new StringBuffer(in.length * 2);
088: for (int i = 0; i < in.length; i++) {
089: if ((0Xff & in[i]) < 16)
090: result.append('0');
091: result.append(Integer.toHexString(0Xff & in[i]));
092: }
093: return new String(result);
094: }
095:
096: public static byte[] decodeHex(String hex) {
097: byte[] result = new byte[hex.length() / 2];
098: for (int i = 0; i < result.length; i++) {
099: result[i] = (byte) (16 * Integer.parseInt(hex.charAt(i * 2)
100: + "", 16) + Integer.parseInt(hex.charAt(i * 2 + 1)
101: + "", 16));
102: }
103: return result;
104: }
105:
106: public static String encodeMD5Hex(String key) {
107: // generate a hex representation from the md5 of a string
108: return encodeHex(encodeMD5Raw(key));
109: }
110:
111: public static String encodeMD5Hex(File file) {
112: // generate a hex representation from the md5 of a file
113: return encodeHex(encodeMD5Raw(file));
114: }
115:
116: public static String encodeMD5Hex(byte[] b) {
117: // generate a hex representation from the md5 of a byte-array
118: return encodeHex(encodeMD5Raw(b));
119: }
120:
121: public static byte[] encodeMD5Raw(String key) {
122: try {
123: MessageDigest digest = MessageDigest.getInstance("MD5");
124: digest.reset();
125: digest.update(key.getBytes());
126: return digest.digest();
127: } catch (java.security.NoSuchAlgorithmException e) {
128: System.out.println("Internal Error at md5:"
129: + e.getMessage());
130: }
131: return null;
132: }
133:
134: public static byte[] encodeMD5Raw(File file) {
135: try {
136: MessageDigest digest = MessageDigest.getInstance("MD5");
137: digest.reset();
138: InputStream in = new BufferedInputStream(
139: new FileInputStream(file), 2048);
140: byte[] buf = new byte[2048];
141: int n;
142: while ((n = in.read(buf)) > 0)
143: digest.update(buf, 0, n);
144: in.close();
145: // now compute the hex-representation of the md5 digest
146: return digest.digest();
147: } catch (java.security.NoSuchAlgorithmException e) {
148: System.out.println("Internal Error at md5:"
149: + e.getMessage());
150: } catch (java.io.FileNotFoundException e) {
151: System.out.println("file not found:" + file.toString());
152: } catch (java.io.IOException e) {
153: System.out.println("file error with " + file.toString()
154: + ": " + e.getMessage());
155: }
156: return null;
157: }
158:
159: private static byte[] encodeMD5Raw(byte[] b) {
160: try {
161: MessageDigest digest = MessageDigest.getInstance("MD5");
162: digest.reset();
163: InputStream in = new ByteArrayInputStream(b);
164: byte[] buf = new byte[2048];
165: int n;
166: while ((n = in.read(buf)) > 0)
167: digest.update(buf, 0, n);
168: in.close();
169: // now compute the hex-representation of the md5 digest
170: return digest.digest();
171: } catch (java.security.NoSuchAlgorithmException e) {
172: System.out.println("Internal Error at md5:"
173: + e.getMessage());
174: } catch (java.io.IOException e) {
175: System.out.println("byte[] error: " + e.getMessage());
176: }
177: return null;
178: }
179:
180: public static Properties s2p(String s) {
181: Properties p = new Properties();
182: int pos;
183: StringTokenizer st = new StringTokenizer(s, ",");
184: String token;
185: while (st.hasMoreTokens()) {
186: token = st.nextToken().trim();
187: pos = token.indexOf("=");
188: if (pos > 0)
189: p.setProperty(token.substring(0, pos).trim(), token
190: .substring(pos + 1).trim());
191: }
192: return p;
193: }
194:
195: public static HashMap<String, String> string2map(String string,
196: String separator) {
197: // this can be used to parse a Map.toString() into a Map again
198: if (string == null)
199: return null;
200: HashMap<String, String> map = new HashMap<String, String>();
201: int pos;
202: if ((pos = string.indexOf("{")) >= 0)
203: string = string.substring(pos + 1).trim();
204: if ((pos = string.lastIndexOf("}")) >= 0)
205: string = string.substring(0, pos).trim();
206: StringTokenizer st = new StringTokenizer(string, separator);
207: String token;
208: while (st.hasMoreTokens()) {
209: token = st.nextToken().trim();
210: pos = token.indexOf("=");
211: if (pos > 0)
212: map.put(token.substring(0, pos).trim(), token
213: .substring(pos + 1).trim());
214: }
215: return map;
216: }
217:
218: public static String map2string(Map<String, String> m,
219: String separator, boolean braces) {
220: final StringBuffer buf = new StringBuffer(20 * m.size());
221: if (braces) {
222: buf.append("{");
223: }
224: final Iterator<Map.Entry<String, String>> i = m.entrySet()
225: .iterator();
226: while (i.hasNext()) {
227: final Entry<String, String> e = i.next();
228: buf.append(e.getKey()).append('=');
229: if (e.getValue() != null) {
230: buf.append(e.getValue());
231: }
232: buf.append(separator);
233: }
234: if (buf.length() > 1) {
235: buf.setLength(buf.length() - 1);
236: } // remove last separator
237: if (braces) {
238: buf.append("}");
239: }
240: return new String(buf);
241: }
242:
243: public static Set<String> string2set(String string, String separator) {
244: // this can be used to parse a Map.toString() into a Map again
245: if (string == null)
246: return null;
247: Set<String> set = Collections
248: .synchronizedSet(new HashSet<String>());
249: int pos;
250: if ((pos = string.indexOf("{")) >= 0)
251: string = string.substring(pos + 1).trim();
252: if ((pos = string.lastIndexOf("}")) >= 0)
253: string = string.substring(0, pos).trim();
254: StringTokenizer st = new StringTokenizer(string, separator);
255: while (st.hasMoreTokens()) {
256: set.add(st.nextToken().trim());
257: }
258: return set;
259: }
260:
261: public static String set2string(Set<String> s, String separator,
262: boolean braces) {
263: StringBuffer buf = new StringBuffer();
264: if (braces)
265: buf.append("{");
266: Iterator<String> i = s.iterator();
267: boolean hasNext = i.hasNext();
268: while (hasNext) {
269: buf.append(i.next().toString());
270: hasNext = i.hasNext();
271: if (hasNext)
272: buf.append(separator);
273: }
274: if (braces)
275: buf.append("}");
276: return new String(buf);
277: }
278:
279: public static void main(String[] s) {
280: if (s.length == 0) {
281: System.out.println("usage: -[ec|dc|es|ds|s2m] <arg>");
282: System.exit(0);
283: }
284:
285: if (s[0].equals("-s2m")) {
286: // generate a b64 decoding from a given string
287: System.out.println(string2map(s[1], ",").toString());
288: }
289: }
290:
291: }
|