KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > utils > CryptoSHA1BASE64


1 /*
2  ************************************************************************************
3  * Copyright (C) 2001-2007 Openbravo S.L.
4  * Licensed under the Apache Software License version 2.0
5  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6  * Unless required by applicable law or agreed to in writing, software distributed
7  * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8  * CONDITIONS OF ANY KIND, either express or implied. See the License for the
9  * specific language governing permissions and limitations under the License.
10  ************************************************************************************
11 */

12 package org.openbravo.utils;
13
14 import javax.servlet.ServletException JavaDoc;
15 import java.io.UnsupportedEncodingException JavaDoc;
16 import java.security.MessageDigest JavaDoc;
17 import java.security.NoSuchAlgorithmException JavaDoc;
18 import sun.misc.BASE64Encoder;
19
20 public final class CryptoSHA1BASE64 {
21   public static String JavaDoc encriptar(String JavaDoc textoplano) throws ServletException JavaDoc {
22     MessageDigest JavaDoc md = null;
23
24     try {
25       md = MessageDigest.getInstance("SHA"); // SHA-1 generator instance
26
} catch(NoSuchAlgorithmException JavaDoc e) {
27       throw new ServletException JavaDoc(e.getMessage());
28     }
29
30     try {
31       md.update(textoplano.getBytes("UTF-8")); // Message summary generation
32
} catch(UnsupportedEncodingException JavaDoc e) {
33       throw new ServletException JavaDoc(e.getMessage());
34     }
35
36     byte raw[] = md.digest(); // Message summary reception
37
String JavaDoc hash = (new BASE64Encoder()).encode(raw); // Encoding to BASE64
38
return hash;
39   }
40 }
41
Popular Tags