com.buession.mcrypt.Mcrypt.java Source code

Java tutorial

Introduction

Here is the source code for com.buession.mcrypt.Mcrypt.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 
 * (the "License"); you may not use this file except in compliance with the License. You may obtain 
 * a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 * 
 * =================================================================================================
 * 
 * This software consists of voluntary contributions made by many individuals on behalf of the
 * Apache Software Foundation. For more information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 * 
 * +------------------------------------------------------------------------------------------------+
 * | License: http://mcrypt.buession.com.cn/LICENSE                                     |
 * | Author: Yong.Teng <webmaster@buession.com>                                        |
 * | Copyright @ 2013-2014 Buession.com Inc.                                          |
 * +------------------------------------------------------------------------------------------------+
 */
package com.buession.mcrypt;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * 
 * @author Yong.Teng <webmaster@buession.com>
 */
public class Mcrypt {

    public final static String MD5 = "MD5";

    public final static String SHA = "SHA";

    public final static String SHA1 = "SHA-1";

    public final static String SHA256 = "SHA-256";

    public final static String SHA512 = "SHA-512";

    private final static char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
            'e', 'f' };

    /**
     * ??
     */
    private String algo;

    /**
     * 
     */
    private String salt;

    /**
     * ?
     */
    private String characterEncoding;

    /**
     * ????
     */
    private Provider provider = null;

    /**
     * ??
     */
    private int count = 1;

    private final static Logger logger = LoggerFactory.getLogger(Mcrypt.class);

    public Mcrypt() {
    }

    /**
     * @param algo
     *        ??
     */
    public Mcrypt(final String algo) {
        this.algo = algo;
    }

    /**
     * @param algo
     *        ??
     * @param count
     *        ??
     */
    public Mcrypt(final String algo, final int count) {
        this(algo, null, count);
    }

    /**
     * @param algo
     *        ??
     * @param count
     *        ??
     * @param provider
     *        ????
     */
    public Mcrypt(final String algo, int count, final Provider provider) {
        this(algo, null, count, provider);
    }

    /**
     * @param algo
     *        ??
     * @param provider
     *        ????
     */
    public Mcrypt(final String algo, final Provider provider) {
        this.algo = algo;
        this.provider = provider;
    }

    /**
     * @param algo
     *        ??
     * @param characterEncoding
     *        ?
     */
    public Mcrypt(final String algo, final String characterEncoding) {
        this.algo = algo;
        this.characterEncoding = characterEncoding;
    }

    /**
     * @param algo
     *        ??
     * @param characterEncoding
     *        ?
     * @param provider
     *        ????
     */
    public Mcrypt(final String algo, final String characterEncoding, final Provider provider) {
        this.algo = algo;
        this.characterEncoding = characterEncoding;
        this.provider = provider;
    }

    /**
     * @param algo
     *        ??
     * @param characterEncoding
     *        ?
     * @param count
     *        ??
     */
    public Mcrypt(final String algo, final String characterEncoding, final int count) {
        this(algo, characterEncoding, null, count);
    }

    /**
     * @param algo
     *        ??
     * @param characterEncoding
     *        ?
     * @param count
     *        ??
     * @param provider
     *        ????
     */
    public Mcrypt(final String algo, final String characterEncoding, int count, final Provider provider) {
        this(algo, characterEncoding, null, count, provider);
    }

    /**
     * @param algo
     *        ??
     * @param characterEncoding
     *        ?
     * @param salt
     *        
     */
    public Mcrypt(final String algo, final String characterEncoding, final String salt) {
        this(algo, characterEncoding, salt, null);
    }

    /**
     * @param algo
     *        ??
     * @param characterEncoding
     *        ?
     * @param salt
     *        
     * @param provider
     *        ????
     */
    public Mcrypt(final String algo, final String characterEncoding, final String salt, final Provider provider) {
        this.algo = algo;
        this.characterEncoding = characterEncoding;
        this.salt = salt;
        this.provider = provider;
    }

    /**
     * @param algo
     *        ??
     * @param characterEncoding
     *        ?
     * @param salt
     *        
     * @param count
     *        ??
     */
    public Mcrypt(final String algo, final String characterEncoding, final String salt, final int count) {
        this(algo, characterEncoding, salt, count, null);
    }

    /**
     * @param algo
     *        ??
     * @param characterEncoding
     *        ?
     * @param salt
     *        
     * @param count
     *        ??
     * @param provider
     *        ????
     */
    public Mcrypt(final String algo, final String characterEncoding, final String salt, final int count,
            final Provider provider) {
        this(algo, characterEncoding, salt);

        if (count < 1) {
            throw new IllegalArgumentException("Count could not less than 1");
        }

        this.count = count;
        this.provider = provider;
    }

    /**
     * ??
     * 
     * @return ??
     */
    public String getAlgo() {
        return algo;
    }

    /**
     * ??
     * 
     * @param algo
     *        ??
     */
    public void setAlgo(final String algo) {
        this.algo = algo;
    }

    /**
     * 
     * 
     * @return 
     */
    public String getSalt() {
        return salt;
    }

    /**
     * 
     * 
     * @param salt
     *        
     */
    public void setSalt(final String salt) {
        this.salt = salt;
    }

    /**
     * ??
     * 
     * @return ?
     */
    public String getCharacterEncoding() {
        return characterEncoding;
    }

    /**
     * ?
     * 
     * @param characterEncoding
     *        ?
     */
    public void setCharacterEncoding(final String characterEncoding) {
        this.characterEncoding = characterEncoding;
    }

    /**
     * ????
     * 
     * @return ????
     */
    public Provider getProvider() {
        return provider;
    }

    /**
     * ????
     * 
     * @param provider
     *        ????
     */
    public void setProvider(final Provider provider) {
        this.provider = provider;
    }

    /**
     * ??
     * 
     * @return ??
     */
    public int getCount() {
        return count;
    }

    /**
     * ??
     * 
     * @param count
     *        ??
     */
    public void setCount(int count) {
        if (count < 1) {
            throw new IllegalArgumentException("Count could not less than 1");
        }

        this.count = count;
    }

    /**
     * 
     * 
     * @param object
     *        ?
     * @return ?
     */
    public String encode(final Object object) {
        if (object == null) {
            throw new IllegalArgumentException("String could not be null");
        }

        if (algo == null || algo.length() == 0) {
            throw new RuntimeException("Algo could not be null");
        }

        try {
            MessageDigest messageDigest = provider == null ? MessageDigest.getInstance(algo)
                    : MessageDigest.getInstance(algo, provider);

            if (object instanceof char[]) {
                return encode(new String((char[]) object), messageDigest);
            } else if (object instanceof byte[]) {
                return encode(new String((byte[]) object, characterEncoding), messageDigest);
            } else {
                return encode(object.toString(), messageDigest);
            }
        } catch (final NoSuchAlgorithmException e) {
            logger.error(e.getMessage());
            throw new SecurityException(e);
        } catch (UnsupportedEncodingException e) {
            logger.error(e.getMessage());
        }

        return null;
    }

    /**
     * 
     * ??????????
     * 
     * @param cs
     *        ? char ?
     * @return ?
     */
    public String decode(final CharSequence cs) {
        if (algo == null || algo.length() == 0) {
            throw new RuntimeException("Algo could not be null");
        }

        throw new UnsupportedOperationException("Algo '" + algo + "' unsupported decode");
    }

    /**
     * 
     * 
     * @param str
     *        ?
     * @param messageDigest
     *        ? MessageDigest 
     * @return ?
     */
    private String encode(String str, final MessageDigest messageDigest) {
        if (StringUtils.isEmpty(salt) == false) {
            str += salt;
        }

        try {
            if (StringUtils.isEmpty(this.characterEncoding)) {
                messageDigest.update(str.getBytes());
            } else {
                messageDigest.update(str.getBytes(this.characterEncoding));
            }

            final byte[] digest = messageDigest.digest();
            String result = getFormattedText(digest);

            logger.debug("Mcrypt encode string <{}> by algo <{}>, salt <{}>", algo, salt);
            while (--count > 0) {
                result = encode(result, messageDigest);
            }

            return result;
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @param bytes
     * @return formatted string
     */
    private static String getFormattedText(byte[] bytes) {
        final StringBuilder buffer = new StringBuilder(bytes.length * 2);

        for (int j = 0; j < bytes.length; j++) {
            buffer.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
            buffer.append(HEX_DIGITS[bytes[j] & 0x0f]);
        }

        return buffer.toString();
    }

}