001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.http.util;
016:
017: import java.io.ByteArrayInputStream;
018: import java.io.ByteArrayOutputStream;
019: import java.io.InputStream;
020: import java.io.ObjectInputStream;
021: import java.io.ObjectOutputStream;
022: import java.io.OutputStream;
023: import java.io.Serializable;
024: import java.security.MessageDigest;
025: import java.util.zip.GZIPInputStream;
026: import java.util.zip.GZIPOutputStream;
027: import net.iharder.base64.Base64;
028:
029: /**
030: * Provides base64 encoding, decoding methods, generating a digest and checking the digest methods.
031: *
032: * @author "Toomas Römer" <toomas@webmedia.ee>
033: */
034: public abstract class EncodingUtil {
035: /**
036: * The method serializes the object, optionally compresses it and base64 encodes.
037: * GZip is used for the optional compression.
038: *
039: * The method uses streams for the different operations and the underlying base64
040: * algorithm is from iHarder.net base64 utilities..
041: *
042: * @param object that will be encoded.
043: * @param compress if set, the serialized object will be compressed.
044: * @return A String represtation of the object in base64 encoded format.
045: * @throws Exception
046: */
047: public static String encodeObjectBase64(Serializable object,
048: boolean compress) throws Exception {
049: ByteArrayOutputStream baos = null;
050: Base64.OutputStream b64os = null;
051: ObjectOutputStream oos = null;
052: try {
053: baos = new ByteArrayOutputStream();
054: b64os = new Base64.OutputStream(baos, Base64.ENCODE
055: | Base64.DONT_BREAK_LINES);
056: OutputStream os = compress ? ((OutputStream) new GZIPOutputStream(
057: b64os))
058: : b64os;
059: oos = new ObjectOutputStream(os);
060:
061: oos.writeObject(object);
062:
063: oos.flush();
064: } finally {
065: oos.close();
066: }
067:
068: return new String(baos.toByteArray(), "UTF-8");
069: }
070:
071: /**
072: * Base64 decodes the value, optionally decompresses it and then deserializes it. For
073: * the decompression GZip is expected.
074: *
075: * @param value is the base64 encoded data of the optionally compressed serialized object.
076: * @param compress determines if after the decoding gzip decompression should be applied.
077: * @return an Object that is deserialized object of the decoded optionally decompressed value.
078: * @throws Exception
079: */
080: public static Object decodeObjectBase64(String value,
081: boolean compress) throws Exception {
082: ByteArrayInputStream bais = new ByteArrayInputStream(value
083: .getBytes("UTF-8"));
084: Base64.InputStream b64is = new Base64.InputStream(bais,
085: Base64.DECODE | Base64.DONT_BREAK_LINES);
086: InputStream is = compress ? ((InputStream) new GZIPInputStream(
087: b64is)) : b64is;
088: ObjectInputStream ois = new ObjectInputStream(is);
089:
090: return ois.readObject();
091: }
092:
093: /**
094: * Builds a digest of the byte array, using the SHA hash function.
095: *
096: * @param data the byte array that's digest we are building.
097: * @return A byte[] array representing the SHA hash.
098: * @throws Exception
099: */
100: public static byte[] buildDigest(byte[] data) throws Exception {
101: MessageDigest dgst = MessageDigest.getInstance("SHA");
102: dgst.update(data);
103: return dgst.digest();
104: }
105:
106: /**
107: * Returns true, if the digest of the value equals to the given digest.
108: *
109: * @param value byte array that's digest we are comparing.
110: * @param digest the allready generated digest we are comparing against.
111: * @return true if the digest of the value equals digest, otherwise false.
112: * @throws Exception
113: */
114: public static boolean checkDigest(byte[] value, byte[] digest)
115: throws Exception {
116: return MessageDigest.isEqual(buildDigest(value), digest);
117: }
118:
119: }
|