|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| PasswordDigester.java | 50% | 76.2% | 100% | 75% |
|
||||||||||||||
| 1 | // | |
| 2 | //Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. | |
| 3 | //All rights reserved. | |
| 4 | // | |
| 5 | //Redistribution and use in source and binary forms, with or without modification, | |
| 6 | //are permitted provided that the following conditions are met: | |
| 7 | // | |
| 8 | //* Redistributions of source code must retain the above copyright notice, | |
| 9 | //this list of conditions and the following disclaimer. | |
| 10 | //* Redistributions in binary form must reproduce the above copyright notice, | |
| 11 | //this list of conditions and the following disclaimer in the documentation | |
| 12 | //and/or other materials provided with the distribution. | |
| 13 | //* Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. | |
| 14 | //nor the names of its contributors may be used to endorse or promote products | |
| 15 | //derived from this software without specific prior written permission. | |
| 16 | // | |
| 17 | //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 18 | //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| 19 | //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
| 20 | //IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
| 21 | //INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
| 22 | //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, | |
| 23 | //OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
| 24 | //WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 25 | //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 26 | //POSSIBILITY OF SUCH DAMAGE. | |
| 27 | // | |
| 28 | ||
| 29 | package org.objectledge.authentication; | |
| 30 | ||
| 31 | import java.security.MessageDigest; | |
| 32 | ||
| 33 | import org.apache.commons.pool.BasePoolableObjectFactory; | |
| 34 | import org.apache.commons.pool.ObjectPool; | |
| 35 | import org.apache.commons.pool.impl.GenericObjectPool; | |
| 36 | ||
| 37 | import sun.misc.BASE64Encoder; | |
| 38 | ||
| 39 | /** | |
| 40 | * Default implementation of password digester. | |
| 41 | * | |
| 42 | * @author <a href="mailto:pablo@caltha.pl">Pawel Potempski</a> | |
| 43 | */ | |
| 44 | public class PasswordDigester | |
| 45 | { | |
| 46 | /** the digest algorithm - null for plaintext */ | |
| 47 | private String algorithm; | |
| 48 | ||
| 49 | /** the local message digest pool */ | |
| 50 | private ObjectPool messageDigestPool = new GenericObjectPool(new MessageDigestFactory()); | |
| 51 | ||
| 52 | /** | |
| 53 | * component constructor. | |
| 54 | * | |
| 55 | * @param algorithm the algorithm. | |
| 56 | */ | |
| 57 | 782 | public PasswordDigester(String algorithm) |
| 58 | { | |
| 59 | 782 | this.algorithm = algorithm; |
| 60 | } | |
| 61 | ||
| 62 | /** | |
| 63 | * Digests a given password using a chosen algorithm. | |
| 64 | * @param password the password to be digested. | |
| 65 | */ | |
| 66 | 690 | public String digestPassword(String password) |
| 67 | { | |
| 68 | 690 | if (algorithm != null) |
| 69 | { | |
| 70 | 690 | if (password == null) |
| 71 | { | |
| 72 | // return unmatchable password for non-login accounts | |
| 73 | 0 | return "-"; |
| 74 | } | |
| 75 | 690 | MessageDigest digest = null; |
| 76 | 690 | try |
| 77 | { | |
| 78 | 690 | digest = (MessageDigest)messageDigestPool.borrowObject(); |
| 79 | 690 | byte[] hash = digest.digest(password.getBytes()); |
| 80 | 690 | messageDigestPool.returnObject(digest); |
| 81 | 690 | BASE64Encoder enc = new BASE64Encoder(); |
| 82 | 690 | StringBuilder encoded = new StringBuilder(); |
| 83 | 690 | encoded.append('{'); |
| 84 | 690 | encoded.append(algorithm.toLowerCase()); |
| 85 | 690 | encoded.append('}'); |
| 86 | 690 | encoded.append(enc.encode(hash)); |
| 87 | 690 | return encoded.toString(); |
| 88 | } | |
| 89 | catch (Exception e) | |
| 90 | { | |
| 91 | 0 | RuntimeException ee = new IllegalArgumentException("Digest password exception: "+ |
| 92 | e.getMessage()); | |
| 93 | 0 | ee.initCause(e); |
| 94 | 0 | throw ee; |
| 95 | } | |
| 96 | } | |
| 97 | else | |
| 98 | { | |
| 99 | 0 | return password; |
| 100 | } | |
| 101 | } | |
| 102 | ||
| 103 | /** | |
| 104 | * A factory of MessageDigest objects. | |
| 105 | */ | |
| 106 | private class MessageDigestFactory | |
| 107 | extends BasePoolableObjectFactory | |
| 108 | { | |
| 109 | /** | |
| 110 | * {@inheritDoc} | |
| 111 | */ | |
| 112 | 414 | public synchronized Object makeObject() throws Exception |
| 113 | { | |
| 114 | 414 | return MessageDigest.getInstance(algorithm); |
| 115 | } | |
| 116 | } | |
| 117 | ||
| 118 | } |
|
||||||||||