KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > lightcrypto > SafeObject


1 /*
2   Name: net.sourceforge.lightcrypto.SafeObject
3   Licensing: LGPL (lesser GNU Public License)
4   API: Bouncy Castle (http://www.bouncycastle.org) lightweight API
5
6   Disclaimer:
7
8   COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND,
9   EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT THE COVERED CODE
10   IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. THE ENTIRE
11   RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED CODE IS WITH YOU. SHOULD ANY COVERED CODE
12   PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR)
13   ASSUME THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY
14   CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED
15   HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
16
17   (C) Copyright 2003 Gert Van Ham
18
19 */

20
21 package net.sourceforge.lightcrypto;
22
23 import org.bouncycastle.util.encoders.Base64;
24
25 /**
26  * Object containing text that can be removed from memory by calling clearText()
27  * method
28  *
29  * @author Gert Van Ham
30  * @author hamgert@users.sourceforge.net
31  * @author http://jcetaglib.sourceforge.net
32  * @version $Id: SafeObject.java,v 1.2 2003/10/05 11:41:29 hamgert Exp $
33  */

34
35 public class SafeObject {
36     private byte[] safetext;
37
38    /**
39     * Set text
40     *
41     * @param s byte[] byte array of text
42     * @exception Exception for all errors
43     **/

44     public void setText (byte[] s)
45      throws Exception JavaDoc {
46         this.safetext = s;
47     }
48     
49    /**
50     * Retrieve text
51     *
52     * @return text
53     * @exception Exception for all errors
54     **/

55     public StringBuffer JavaDoc getText ()
56      throws Exception JavaDoc {
57         return new StringBuffer JavaDoc(new String JavaDoc(safetext));
58     }
59
60     /**
61     * Retrieve text as a BASE64 string
62     *
63     * @return text in BASE64 format
64     * @exception Exception for all errors
65     **/

66     public String JavaDoc getBase64 ()
67      throws Exception JavaDoc {
68         return new String JavaDoc(Base64.encode(this.safetext));
69     }
70
71     /**
72     * Get text length
73     *
74     * @return text length
75     * @exception Exception for all errors
76     **/

77     public int getLength ()
78      throws Exception JavaDoc {
79         return safetext.length;
80     }
81
82     /**
83     * Retrieve bytes
84     *
85     * @return text
86     * @exception Exception for all errors
87     **/

88     public byte[] getBytes ()
89      throws Exception JavaDoc {
90         return safetext;
91     }
92
93    /**
94     * Wipe text from memory
95     *
96     * @exception Exception for all errors
97     **/

98     public void clearText ()
99      throws Exception JavaDoc {
100         for (int i = 0; i < safetext.length; i++)
101       {
102         safetext[i] = 0;
103       }
104     }
105 }
106     
Popular Tags