KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > crypto > provider > UnixCrypt


1 package org.apache.turbine.services.crypto.provider;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.turbine.services.crypto.CryptoAlgorithm;
20
21 /**
22  * Implements Standard Unix crypt(3) for use with the Crypto Service.
23  *
24  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
25  * @version $Id: UnixCrypt.java,v 1.4.2.2 2004/05/20 03:05:19 seade Exp $
26  */

27 public class UnixCrypt
28         implements CryptoAlgorithm
29 {
30
31     /** The seed to use */
32     private String JavaDoc seed = null;
33
34     /** standard Unix crypt chars (64) */
35     private static final char[] SALT_CHARS =
36             (("abcdefghijklmnopqrstuvwxyz"
37             + "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./").toCharArray());
38
39     /**
40      * C'tor
41      */

42     public UnixCrypt()
43     {
44     }
45
46     /**
47      * This class never uses anything but
48      * UnixCrypt, so it is just a dummy
49      * (Fixme: Should we throw an exception if
50      * something is requested that we don't support?
51      *
52      * @param cipher Cipher (ignored)
53      */

54     public void setCipher(String JavaDoc cipher)
55     {
56         /* dummy */
57     }
58
59     /**
60      * Setting the seed for the UnixCrypt
61      * algorithm. If a null value is supplied,
62      * or no seed is set, then a random seed is used.
63      *
64      * @param seed The seed value to use.
65      */

66     public void setSeed(String JavaDoc seed)
67     {
68         this.seed = seed;
69     }
70
71     /**
72      * encrypt the supplied string with the requested cipher
73      *
74      * @param value The value to be encrypted
75      * @return The encrypted value
76      * @throws Exception An Exception of the underlying implementation.
77      */

78     public String JavaDoc encrypt(String JavaDoc value)
79             throws Exception JavaDoc
80     {
81         if (seed == null)
82         {
83             java.util.Random JavaDoc randomGenerator = new java.util.Random JavaDoc();
84             int numSaltChars = SALT_CHARS.length;
85
86             seed = (new StringBuffer JavaDoc())
87                     .append(SALT_CHARS[Math.abs(randomGenerator.nextInt())
88                     % numSaltChars])
89                     .append(SALT_CHARS[Math.abs(randomGenerator.nextInt())
90                     % numSaltChars])
91                     .toString();
92         }
93
94         /* UnixCrypt seems to be a really widespread name... */
95         return new cryptix.tools.UnixCrypt(seed).crypt(value);
96     }
97     
98 }
99
Popular Tags