01: package org.apache.turbine.services.crypto.provider;
02:
03: /*
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "License"); you may not use this file except in compliance
10: * with the License. You may obtain a copy of the License at
11: *
12: * http://www.apache.org/licenses/LICENSE-2.0
13: *
14: * Unless required by applicable law or agreed to in writing,
15: * software distributed under the License is distributed on an
16: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17: * KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations
19: * under the License.
20: */
21:
22: import org.apache.turbine.services.crypto.CryptoAlgorithm;
23:
24: import java.util.Random;
25:
26: /**
27: * Implements Standard Unix crypt(3) for use with the Crypto Service.
28: *
29: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
30: * @version $Id: UnixCrypt.java 613402 2008-01-19 15:16:34Z seade $
31: */
32: public class UnixCrypt implements CryptoAlgorithm {
33:
34: /** The seed to use */
35: private String seed = null;
36:
37: /** standard Unix crypt chars (64) */
38: private static final char[] SALT_CHARS = (("abcdefghijklmnopqrstuvwxyz"
39: + "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./").toCharArray());
40:
41: /**
42: * C'tor
43: */
44: public UnixCrypt() {
45: }
46:
47: /**
48: * This class never uses anything but
49: * UnixCrypt, so it is just a dummy
50: * (Fixme: Should we throw an exception if
51: * something is requested that we don't support?
52: *
53: * @param cipher Cipher (ignored)
54: */
55: public void setCipher(String cipher) {
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 seed) {
67: this .seed = seed;
68: }
69:
70: /**
71: * encrypt the supplied string with the requested cipher
72: *
73: * @param value The value to be encrypted
74: * @return The encrypted value
75: * @throws Exception An Exception of the underlying implementation.
76: */
77: public String encrypt(String value) throws Exception {
78: if (seed == null) {
79: Random randomGenerator = new java.util.Random();
80: int numSaltChars = SALT_CHARS.length;
81:
82: seed = (new StringBuffer()).append(
83: SALT_CHARS[Math.abs(randomGenerator.nextInt())
84: % numSaltChars]).append(
85: SALT_CHARS[Math.abs(randomGenerator.nextInt())
86: % numSaltChars]).toString();
87: }
88:
89: return org.apache.turbine.services.crypto.impl.UnixCrypt.crypt(
90: seed, value);
91: }
92:
93: }
|