001 package org.apache.fulcrum.crypto.provider;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements. See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership. The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License. You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied. See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022
023 import org.apache.fulcrum.crypto.CryptoAlgorithm;
024
025 import java.util.Random;
026
027 /**
028 * Implements Standard Unix crypt(3) for use with the Crypto Service.
029 *
030 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
031 * @version $Id: UnixCrypt.java 581797 2007-10-04 08:26:18Z sgoeschl $
032 */
033
034 public class UnixCrypt
035 implements CryptoAlgorithm
036 {
037
038 /** The seed to use */
039 private String seed = null;
040
041 /** standard Unix crypt chars (64) */
042 private static final char[] SALT_CHARS =
043 (("abcdefghijklmnopqrstuvwxyz" +
044 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./").toCharArray());
045
046
047 /**
048 * C'tor
049 *
050 */
051
052 public UnixCrypt()
053 {
054 }
055
056 /**
057 * This class never uses anything but
058 * UnixCrypt, so it is just a dummy
059 * (Fixme: Should we throw an exception if
060 * something is requested that we don't support?
061 *
062 * @param cipher Cipher (ignored)
063 */
064
065 public void setCipher(String cipher)
066 {
067 /* dummy */
068 }
069
070 /**
071 * Setting the seed for the UnixCrypt
072 * algorithm. If a null value is supplied,
073 * or no seed is set, then a random seed is used.
074 *
075 * @param seed The seed value to use.
076 */
077
078 public void setSeed(String seed)
079 {
080 this.seed = seed;
081 }
082
083 /**
084 * encrypt the supplied string with the requested cipher
085 *
086 * @param value The value to be encrypted
087 * @return The encrypted value
088 * @throws Exception An Exception of the underlying implementation.
089 */
090 public String encrypt(String value)
091 throws Exception
092 {
093 if (seed == null)
094 {
095 Random randomGenerator = new java.util.Random();
096 int numSaltChars = SALT_CHARS.length;
097
098 seed = (new StringBuffer())
099 .append(SALT_CHARS[Math.abs(randomGenerator.nextInt())
100 % numSaltChars])
101 .append(SALT_CHARS[Math.abs(randomGenerator.nextInt())
102 % numSaltChars])
103 .toString();
104 }
105
106 return org.apache.fulcrum.crypto.impl.UnixCrypt.crypt(seed, value);
107 }
108 }