001    /*
002     * This file is part of muCommander, http://www.mucommander.com
003     * Copyright (C) 2002-2008 Maxence Bernard
004     *
005     * muCommander is free software; you can redistribute it and/or modify
006     * it under the terms of the GNU General Public License as published by
007     * the Free Software Foundation; either version 3 of the License, or
008     * (at your option) any later version.
009     *
010     * muCommander is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017     */
018    
019    package com.mucommander.io.security;
020    
021    import java.security.Provider;
022    import java.security.Security;
023    
024    /**
025     * This custom <code>java.security.Provider</code> exposes muCommander's own <code>MessageDigest</code> implementations,
026     * and the ones used aggregated from third party libraries.
027     *
028     * <p>The {@link #registerProvider()} method should be called once to register this <code>Provider</code> with the
029     * Java Cryptography Extension and advertise the additional <code>MessageDigest</code> implementations.</p>
030     *
031     * @author Maxence Bernard
032     */
033    public class MuProvider extends Provider {
034    
035        /** True if an instance of this Provider has already been registered with java.security.Security */
036        private static boolean initialized;
037    
038        private MuProvider() {
039            super("muCommander", 1.0, "muCommander's additional MessageDigest implementations.");
040        }
041    
042        /**
043         * Registers an instance of this Provider with the <code>java.security.Security</code> class, to expose the
044         * additional <code>MessageDigest</code> implementations and have them returned by
045         * <code>java.security.Security.getAlgorithms("MessageDigest")</code>.
046         * This method should be called once
047         */
048        public static void registerProvider() {
049            // A Provider must be registered only once
050            if(initialized)
051                return;
052    
053            MuProvider provider = new MuProvider();
054    
055            // Add our own MessageDigest implementations
056            provider.put("MessageDigest."+Adler32MessageDigest.getAlgorithmName(), Adler32MessageDigest.class.getName());
057            provider.put("MessageDigest."+CRC32MessageDigest.getAlgorithmName(), CRC32MessageDigest.class.getName());
058    
059            // Aggregate MessageDigest implementations from 3rd party libraries
060            provider.put("MessageDigest.MD4", jcifs.util.MD4.class.getName());
061    
062            // Register the provider with java.security.Security
063            Security.addProvider(provider);
064    
065            // A Provider must be registered only once
066            initialized = true;
067        }
068    }