01: /**
02: * ChainBuilder ESB
03: * Visual Enterprise Integration
04: *
05: * Copyright (C) 2006 Bostech Corporation
06: *
07: * This program is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU General Public License as published by the
09: * Free Software Foundation; either version 2 of the License, or (at your option)
10: * any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15: * for more details.
16: *
17: * You should have received a copy of the GNU General Public License along with
18: * this program; if not, write to the Free Software Foundation, Inc.,
19: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20: *
21: *
22: * $Id$
23: */package com.bostechcorp.cbesb.common.util.etl.util;
24:
25: /**
26: * @author LPS
27: *
28: */
29: import java.io.IOException;
30: import java.io.UnsupportedEncodingException;
31: import java.security.MessageDigest;
32: import java.security.NoSuchAlgorithmException;
33:
34: import sun.misc.BASE64Decoder;
35: import sun.misc.BASE64Encoder;
36:
37: public final class PasswordService {
38: private static PasswordService instance;
39:
40: private PasswordService() {
41: }
42:
43: // public synchronized String encrypt(String plaintext) throws SystemUnavailableException
44: // {
45: // MessageDigest md = null;
46: // try
47: // {
48: // md = MessageDigest.getInstance("SHA"); //step 2
49: // }
50: // catch(NoSuchAlgorithmException e)
51: // {
52: // throw new SystemUnavailableException(e.getMessage());
53: // }
54: // try
55: // {
56: // md.update(plaintext.getBytes("UTF-8")); //step 3
57: // }
58: // catch(UnsupportedEncodingException e)
59: // {
60: // throw new SystemUnavailableException(e.getMessage());
61: // }
62: // byte raw[] = md.digest(); //step 4
63: // String hash = (new BASE64Encoder()).encode(raw); //step 5
64: // return hash; //step 6
65: // }
66:
67: public synchronized String encrypt(String plaintext)
68: throws SystemUnavailableException {
69: String hash = (new BASE64Encoder())
70: .encode(plaintext.getBytes());
71: return hash;
72: }
73:
74: public static synchronized String decrypt(String ecrypted)
75: throws SystemUnavailableException {
76: try {
77: return new String((new BASE64Decoder())
78: .decodeBuffer(ecrypted));
79: } catch (IOException e) {
80: throw new SystemUnavailableException(
81: "Decryption Exception: " + e.getMessage());
82: }
83: }
84:
85: public static synchronized PasswordService getInstance() //step 1
86: {
87: if (instance == null) {
88: return new PasswordService();
89: } else {
90: return instance;
91: }
92: }
93: }
|