View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  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,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.model.constants;
21  
22  
23  import org.apache.directory.api.util.Strings;
24  
25  
26  /**
27   * An enum to store all the security constants used in the server
28   *
29   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
30   */
31  public enum LdapSecurityConstants
32  {
33      /** The SHA encryption method */
34      HASH_METHOD_SHA("SHA", "SHA", "sha"),
35  
36      /** The Salted SHA encryption method */
37      HASH_METHOD_SSHA("SSHA", "SHA", "ssha"),
38  
39      /** The SHA-256 encryption method */
40      HASH_METHOD_SHA256("SHA-256", "SHA-256", "sha256"),
41  
42      /** The salted SHA-256 encryption method */
43      HASH_METHOD_SSHA256("SSHA-256", "SHA-256", "ssha256"),
44  
45      /** The SHA-384 encryption method */
46      HASH_METHOD_SHA384("SHA-384", "SHA-384", "sha384"),
47  
48      /** The salted SHA-384 encryption method */
49      HASH_METHOD_SSHA384("SSHA-384", "SHA-384", "ssha384"),
50  
51      /** The SHA-512 encryption method */
52      HASH_METHOD_SHA512("SHA-512", "SHA-512", "sha512"),
53  
54      /** The salted SHA-512 encryption method */
55      HASH_METHOD_SSHA512("SSHA-512", "SHA-512", "ssha512"),
56  
57      /** The MD5 encryption method */
58      HASH_METHOD_MD5("MD5", "MD5", "md5"),
59  
60      /** The Salter MD5 encryption method */
61      HASH_METHOD_SMD5("SMD5", "MD5", "smd5"),
62  
63      /** The crypt encryption method */
64      HASH_METHOD_CRYPT("CRYPT", "CRYPT", "crypt"),
65  
66      /** The PBKDF2-based encryption method */
67      HASH_METHOD_PKCS5S2("PKCS5S2", "PBKDF2WithHmacSHA1", "PKCS5S2");
68  
69      /* These encryption types are not yet supported 
70      ** The AES encryption method *
71      ENC_METHOD_AES("aes"),
72      
73      ** The 3DES encryption method *
74      ENC_METHOD_3DES("3des"),
75      
76      ** The Blowfish encryption method *
77      ENC_METHOD_BLOWFISH("blowfish"),
78      
79      ** The RC4 encryption method *
80      ENC_METHOD_RC4("rc4");
81      */
82  
83      /** The associated name */
84      private String name;
85  
86      /** The associated algorithm */
87      private String algorithm;
88  
89      /** The associated prefix */
90      private String prefix;
91  
92  
93      /**
94       * Creates a new instance of LdapSecurityConstants.
95       * 
96       * @param name the associated name
97       * @param algorithm the associated algorithm
98       * @param prefix the associated prefix
99       */
100     private LdapSecurityConstants( String name, String algorithm, String prefix )
101     {
102         this.name = name;
103         this.algorithm = algorithm;
104         this.prefix = prefix;
105     }
106 
107 
108     /**
109      * @return the name associated with the constant.
110      */
111     public String getName()
112     {
113         return name;
114     }
115 
116 
117     /**
118      * @return the prefix associated with the constant.
119      */
120     public String getAlgorithm()
121     {
122         return algorithm;
123     }
124 
125 
126     /**
127      * @return the prefix associated with the constant.
128      */
129     public String getPrefix()
130     {
131         return prefix;
132     }
133 
134 
135     /**
136      * Get the associated constant from a string
137      *
138      * @param name The algorithm's name
139      * @return The associated constant
140      */
141     public static LdapSecurityConstants getAlgorithm( String name )
142     {
143         String algorithm = "";
144 
145         if ( name != null )
146         {
147             algorithm = Strings.toLowerCase( name );
148         }
149 
150         if ( HASH_METHOD_SHA.getName().equalsIgnoreCase( algorithm )
151             || HASH_METHOD_SHA.getPrefix().equalsIgnoreCase( algorithm ) )
152         {
153             return HASH_METHOD_SHA;
154         }
155 
156         if ( HASH_METHOD_SSHA.getName().equalsIgnoreCase( algorithm )
157             || HASH_METHOD_SSHA.getPrefix().equalsIgnoreCase( algorithm ) )
158         {
159             return HASH_METHOD_SSHA;
160         }
161 
162         if ( HASH_METHOD_MD5.getName().equalsIgnoreCase( algorithm )
163             || HASH_METHOD_MD5.getPrefix().equalsIgnoreCase( algorithm ))
164         {
165             return HASH_METHOD_MD5;
166         }
167 
168         if ( HASH_METHOD_SMD5.getName().equalsIgnoreCase( algorithm )
169             || HASH_METHOD_SMD5.getPrefix().equalsIgnoreCase( algorithm ))
170         {
171             return HASH_METHOD_SMD5;
172         }
173 
174         if ( HASH_METHOD_CRYPT.getName().equalsIgnoreCase( algorithm )
175             || HASH_METHOD_CRYPT.getPrefix().equalsIgnoreCase( algorithm ))
176         {
177             return HASH_METHOD_CRYPT;
178         }
179 
180         if ( ( HASH_METHOD_SHA256.getName().equalsIgnoreCase( algorithm ) )
181             || ( HASH_METHOD_SHA256.getPrefix().equalsIgnoreCase( algorithm ) )
182             || ( "sha-256".equalsIgnoreCase( algorithm ) ) ) // "sha-256" used for backwards compatibility
183         {
184             return HASH_METHOD_SHA256;
185         }
186 
187         if ( ( HASH_METHOD_SSHA256.getName().equalsIgnoreCase( algorithm ) )
188             || ( HASH_METHOD_SSHA256.getPrefix().equalsIgnoreCase( algorithm ) )
189             || ( "ssha-256".equalsIgnoreCase( algorithm ) ) ) // "ssha-256" used for backwards compatibility
190         {
191             return HASH_METHOD_SSHA256;
192         }
193 
194         if ( ( HASH_METHOD_SHA384.getName().equalsIgnoreCase( algorithm ) )
195             || ( HASH_METHOD_SHA384.getPrefix().equalsIgnoreCase( algorithm ) )
196             || ( "sha-384".equalsIgnoreCase( algorithm ) ) ) // "sha-384" used for backwards compatibility
197         {
198             return HASH_METHOD_SHA384;
199         }
200 
201         if ( ( HASH_METHOD_SSHA384.getName().equalsIgnoreCase( algorithm ) )
202             ||  ( HASH_METHOD_SSHA384.getPrefix().equalsIgnoreCase( algorithm ) )
203             || ( "ssha-384".equalsIgnoreCase( algorithm ) ) ) // "ssha-384" used for backwards compatibility
204         {
205             return HASH_METHOD_SSHA384;
206         }
207 
208         if ( ( HASH_METHOD_SHA512.getName().equalsIgnoreCase( algorithm ) )
209             ||  ( HASH_METHOD_SHA512.getPrefix().equalsIgnoreCase( algorithm ) )
210             || ( "sha-512".equalsIgnoreCase( algorithm ) ) ) // "sha-512" used for backwards compatibility
211         {
212             return HASH_METHOD_SHA512;
213         }
214 
215         if ( ( HASH_METHOD_SSHA512.getName().equalsIgnoreCase( algorithm ) )
216             ||  ( HASH_METHOD_SSHA512.getPrefix().equalsIgnoreCase( algorithm ) )
217             || ( "ssha-512".equalsIgnoreCase( algorithm ) ) ) // "ssha-512" used for backwards compatibility
218         {
219             return HASH_METHOD_SSHA512;
220         }
221 
222         if ( HASH_METHOD_PKCS5S2.getName().equalsIgnoreCase( algorithm )
223             || HASH_METHOD_PKCS5S2.getPrefix().equalsIgnoreCase( algorithm ) )
224         {
225             return HASH_METHOD_PKCS5S2;
226         }
227 
228         /*
229         if ( ENC_METHOD_AES.getName().equalsIgnoreCase( algorithm ) )
230         {
231             return ENC_METHOD_AES;
232         }
233 
234         if ( ENC_METHOD_3DES.getName().equalsIgnoreCase( algorithm ) )
235         {
236             return ENC_METHOD_3DES;
237         }
238 
239         if ( ENC_METHOD_BLOWFISH.getName().equalsIgnoreCase( algorithm ) )
240         {
241             return ENC_METHOD_BLOWFISH;
242         }
243 
244         if ( ENC_METHOD_RC4.getName().equalsIgnoreCase( algorithm ) )
245         {
246             return ENC_METHOD_RC4;
247         }
248         */
249 
250         return null;
251     }
252 }