| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hiveutils.util; |
| 16 | |
| 17 | import java.security.MessageDigest; |
| 18 | |
| 19 | import org.apache.commons.codec.binary.Base64; |
| 20 | import org.apache.commons.logging.Log; |
| 21 | import org.apache.commons.logging.LogFactory; |
| 22 | import org.apache.hivemind.ApplicationRuntimeException; |
| 23 | |
| 24 | final public class PasswordEncryption |
| 25 | { |
| 26 | static final private Log _logger = LogFactory.getLog(PasswordEncryption.class); |
| 27 | |
| 28 | private PasswordEncryption() |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | //#### does MessageDigest.getInstance() return a new (thread-safe) instance |
| 33 | // every time???? This point is not clear in the API. |
| 34 | // static synchronized public String encrypt(String plaintext) |
| 35 | //CSOFF: IllegalCatchCheck |
| 36 | static public String encrypt(String str) |
| 37 | { |
| 38 | if (str == null) |
| 39 | { |
| 40 | return str; |
| 41 | } |
| 42 | try |
| 43 | { |
| 44 | MessageDigest md = MessageDigest.getInstance("SHA-1"); |
| 45 | md.update(str.getBytes("UTF-8")); |
| 46 | byte[] raw = md.digest(); |
| 47 | byte[] b64str = Base64.encodeBase64(raw); |
| 48 | return new String(b64str); |
| 49 | } |
| 50 | catch (Exception e) |
| 51 | { |
| 52 | _logger.error("encrypt()", e); |
| 53 | throw new ApplicationRuntimeException(e); |
| 54 | } |
| 55 | } |
| 56 | //CSON: IllegalCatchCheck |
| 57 | } |