001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.api.ldap.model.constants;
021
022
023/**
024 * An enum to store all the security constants used in the server
025 *
026 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
027 */
028public enum LdapSecurityConstants
029{
030    /** The SHA encryption method */
031    HASH_METHOD_SHA("SHA", "SHA", "sha"),
032
033    /** The Salted SHA encryption method */
034    HASH_METHOD_SSHA("SSHA", "SHA", "ssha"),
035
036    /** The SHA-256 encryption method */
037    HASH_METHOD_SHA256("SHA-256", "SHA-256", "sha256"),
038
039    /** The salted SHA-256 encryption method */
040    HASH_METHOD_SSHA256("SSHA-256", "SHA-256", "ssha256"),
041
042    /** The SHA-384 encryption method */
043    HASH_METHOD_SHA384("SHA-384", "SHA-384", "sha384"),
044
045    /** The salted SHA-384 encryption method */
046    HASH_METHOD_SSHA384("SSHA-384", "SHA-384", "ssha384"),
047
048    /** The SHA-512 encryption method */
049    HASH_METHOD_SHA512("SHA-512", "SHA-512", "sha512"),
050
051    /** The salted SHA-512 encryption method */
052    HASH_METHOD_SSHA512("SSHA-512", "SHA-512", "ssha512"),
053
054    /** The MD5 encryption method */
055    HASH_METHOD_MD5("MD5", "MD5", "md5"),
056
057    /** The Salter MD5 encryption method */
058    HASH_METHOD_SMD5("SMD5", "MD5", "smd5"),
059
060    /** The crypt encryption method */
061    HASH_METHOD_CRYPT("CRYPT", "CRYPT", "crypt"),
062
063    /** The PBKDF2-based encryption method */
064    HASH_METHOD_PKCS5S2("PKCS5S2", "PBKDF2WithHmacSHA1", "PKCS5S2");
065
066    /* These encryption types are not yet supported 
067    ** The AES encryption method *
068    ENC_METHOD_AES("aes"),
069    
070    ** The 3DES encryption method *
071    ENC_METHOD_3DES("3des"),
072    
073    ** The Blowfish encryption method *
074    ENC_METHOD_BLOWFISH("blowfish"),
075    
076    ** The RC4 encryption method *
077    ENC_METHOD_RC4("rc4");
078    */
079
080    /** The associated name */
081    private String name;
082
083    /** The associated algorithm */
084    private String algorithm;
085
086    /** The associated prefix */
087    private String prefix;
088
089
090    /**
091     * Creates a new instance of LdapSecurityConstants.
092     * 
093     * @param name the associated name
094     * @param algorithm the associated algorithm
095     * @param prefix the associated prefix
096     */
097    private LdapSecurityConstants( String name, String algorithm, String prefix )
098    {
099        this.name = name;
100        this.algorithm = algorithm;
101        this.prefix = prefix;
102    }
103
104
105    /**
106     * @return the name associated with the constant.
107     */
108    public String getName()
109    {
110        return name;
111    }
112
113
114    /**
115     * @return the prefix associated with the constant.
116     */
117    public String getAlgorithm()
118    {
119        return algorithm;
120    }
121
122
123    /**
124     * @return the prefix associated with the constant.
125     */
126    public String getPrefix()
127    {
128        return prefix;
129    }
130
131
132    /**
133     * Get the associated constant from a string
134     *
135     * @param algorithm The algorithm's name
136     * @return The associated constant
137     */
138    public static LdapSecurityConstants getAlgorithm( String algorithm )
139    {
140        if ( HASH_METHOD_SHA.name.equalsIgnoreCase( algorithm )
141            || HASH_METHOD_SHA.prefix.equalsIgnoreCase( algorithm ) )
142        {
143            return HASH_METHOD_SHA;
144        }
145
146        if ( HASH_METHOD_SSHA.name.equalsIgnoreCase( algorithm )
147            || HASH_METHOD_SSHA.prefix.equalsIgnoreCase( algorithm ) )
148        {
149            return HASH_METHOD_SSHA;
150        }
151
152        if ( HASH_METHOD_MD5.name.equalsIgnoreCase( algorithm )
153            || HASH_METHOD_MD5.prefix.equalsIgnoreCase( algorithm ) )
154        {
155            return HASH_METHOD_MD5;
156        }
157
158        if ( HASH_METHOD_SMD5.name.equalsIgnoreCase( algorithm )
159            || HASH_METHOD_SMD5.prefix.equalsIgnoreCase( algorithm ) )
160        {
161            return HASH_METHOD_SMD5;
162        }
163
164        if ( HASH_METHOD_CRYPT.name.equalsIgnoreCase( algorithm )
165            || HASH_METHOD_CRYPT.prefix.equalsIgnoreCase( algorithm ) )
166        {
167            return HASH_METHOD_CRYPT;
168        }
169
170        if ( ( HASH_METHOD_SHA256.name.equalsIgnoreCase( algorithm ) )
171            || ( HASH_METHOD_SHA256.prefix.equalsIgnoreCase( algorithm ) )
172            // "sha-256" used for backwards compatibility
173            || ( "sha-256".equalsIgnoreCase( algorithm ) ) )
174        {
175            return HASH_METHOD_SHA256;
176        }
177
178        if ( ( HASH_METHOD_SSHA256.name.equalsIgnoreCase( algorithm ) )
179            || ( HASH_METHOD_SSHA256.prefix.equalsIgnoreCase( algorithm ) )
180            // "ssha-256" used for backwards compatibility
181            || ( "ssha-256".equalsIgnoreCase( algorithm ) ) )
182        {
183            return HASH_METHOD_SSHA256;
184        }
185
186        if ( ( HASH_METHOD_SHA384.name.equalsIgnoreCase( algorithm ) )
187            || ( HASH_METHOD_SHA384.prefix.equalsIgnoreCase( algorithm ) )
188            // "sha-384" used for backwards compatibility
189            || ( "sha-384".equalsIgnoreCase( algorithm ) ) )
190        {
191            return HASH_METHOD_SHA384;
192        }
193
194        if ( ( HASH_METHOD_SSHA384.name.equalsIgnoreCase( algorithm ) )
195            || ( HASH_METHOD_SSHA384.prefix.equalsIgnoreCase( algorithm ) )
196            // "ssha-384" used for backwards compatibility
197            || ( "ssha-384".equalsIgnoreCase( algorithm ) ) )
198        {
199            return HASH_METHOD_SSHA384;
200        }
201
202        if ( ( HASH_METHOD_SHA512.name.equalsIgnoreCase( algorithm ) )
203            || ( HASH_METHOD_SHA512.prefix.equalsIgnoreCase( algorithm ) )
204            // "sha-512" used for backwards compatibility
205            || ( "sha-512".equalsIgnoreCase( algorithm ) ) )
206        {
207            return HASH_METHOD_SHA512;
208        }
209
210        if ( ( HASH_METHOD_SSHA512.name.equalsIgnoreCase( algorithm ) )
211            || ( HASH_METHOD_SSHA512.prefix.equalsIgnoreCase( algorithm ) )
212            // "ssha-512" used for backwards compatibility
213            || ( "ssha-512".equalsIgnoreCase( algorithm ) ) )
214        {
215            return HASH_METHOD_SSHA512;
216        }
217
218        if ( HASH_METHOD_PKCS5S2.name.equalsIgnoreCase( algorithm )
219            || HASH_METHOD_PKCS5S2.prefix.equalsIgnoreCase( algorithm ) )
220        {
221            return HASH_METHOD_PKCS5S2;
222        }
223
224        /*
225        if ( ENC_METHOD_AES.name.equalsIgnoreCase( algorithm ) )
226        {
227            return ENC_METHOD_AES;
228        }
229
230        if ( ENC_METHOD_3DES.name.equalsIgnoreCase( algorithm ) )
231        {
232            return ENC_METHOD_3DES;
233        }
234
235        if ( ENC_METHOD_BLOWFISH.name.equalsIgnoreCase( algorithm ) )
236        {
237            return ENC_METHOD_BLOWFISH;
238        }
239
240        if ( ENC_METHOD_RC4.name.equalsIgnoreCase( algorithm ) )
241        {
242            return ENC_METHOD_RC4;
243        }
244        */
245
246        return null;
247    }
248}