com.anteam.demo.codec.common.CoderUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.anteam.demo.codec.common.CoderUtil.java

Source

/*
 * Project: root
 * 
 * File Created at 2014-05-23
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package com.anteam.demo.codec.common;

import com.anteam.demo.codec.annotation.Encrypted;
import com.anteam.demo.codec.cipher.symmetric.DESedeCoder;
import com.anteam.demo.codec.core.BasicCoder;
import com.anteam.demo.codec.core.EncryptAlgorithm;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.WeakHashMap;

/**
 * @author bash
 * @version V1.0
 * @type CoderUtil
 * @desc
 * @date 2014-05-23
 */
public final class CoderUtil {

    private static final EncryptAlgorithm DEFAULT_ENCRYPT_ALGORITHM = EncryptAlgorithm.DESEDE;

    /**
     * Logger
     */
    private static final Logger LOG = LoggerFactory.getLogger(CoderUtil.class);

    private static WeakHashMap<String, BasicCoder> encoderWeakHashMap = new WeakHashMap<String, BasicCoder>();

    /**
     * ??
     */
    private CoderUtil() {
        super();
    }

    public static byte[] encode(byte[] source) throws EncoderException {
        if (source == null) {
            return null;
        }
        return CoderUtil.encode(source, DEFAULT_ENCRYPT_ALGORITHM, null);
    }

    public static byte[] decode(byte[] source) throws DecoderException {
        if (source == null) {
            return null;
        }
        return CoderUtil.decode(source, DEFAULT_ENCRYPT_ALGORITHM, null);
    }

    public static String encode(String source) throws EncoderException {
        if (source == null) {
            return null;
        }
        return CoderUtil.encode(source, DEFAULT_ENCRYPT_ALGORITHM, null);
    }

    public static String decode(String source) throws DecoderException {
        if (source == null) {
            return null;
        }
        return CoderUtil.decode(source, DEFAULT_ENCRYPT_ALGORITHM, null);
    }

    public static Object encode(Object source) throws EncoderException {
        if (source == null) {
            return null;
        }
        return CoderUtil.encode(source, DEFAULT_ENCRYPT_ALGORITHM, null);
    }

    public static Object decode(Object source) throws DecoderException {
        if (source == null) {
            return null;
        }
        return CoderUtil.decode(source, DEFAULT_ENCRYPT_ALGORITHM, null);
    }

    public static Object encode(Object source, EncryptAlgorithm encryptAlgorithm, byte[] key)
            throws EncoderException {
        if (source == null || encryptAlgorithm == null) {
            return null;
        }
        Object result = source;
        if (source instanceof byte[]) {
            return CoderUtil.encode((byte[]) source, encryptAlgorithm, key);
        } else if (source instanceof String) {
            return CoderUtil.encode((String) source, encryptAlgorithm, key);
        }
        Field[] fields = source.getClass().getDeclaredFields();
        for (Field field : fields) {
            Encrypted encrypted = field.getAnnotation(Encrypted.class);
            if (encrypted != null) {
                ReflectionUtils.makeAccessible(field);
                if (!Modifier.isStatic(field.getModifiers())) {
                    try {
                        field.set(source, CoderUtil.encode(field.get(source)));
                    } catch (IllegalAccessException e) {
                        LOG.error("?:" + source.getClass().getName() + ":" + field.getName(), e);
                    }
                }
            }
        }
        return result;
    }

    public static Object decode(Object source, EncryptAlgorithm encryptAlgorithm, byte[] key)
            throws DecoderException {
        if (source == null || encryptAlgorithm == null) {
            return null;
        }
        Object result = source;
        if (source instanceof byte[]) {
            return CoderUtil.decode((byte[]) source, encryptAlgorithm, key);
        } else if (source instanceof String) {
            return CoderUtil.decode((String) source, encryptAlgorithm, key);
        }
        Field[] fields = source.getClass().getDeclaredFields();
        for (Field field : fields) {
            Encrypted encrypted = field.getAnnotation(Encrypted.class);
            if (encrypted != null) {
                ReflectionUtils.makeAccessible(field);
                if (!Modifier.isStatic(field.getModifiers())) {
                    try {
                        field.set(source, CoderUtil.decode(field.get(source)));
                    } catch (IllegalAccessException e) {
                        LOG.error("?:" + source.getClass().getName() + ":" + field.getName(), e);
                    }
                }
            }
        }
        return result;
    }

    public static byte[] encode(byte[] source, EncryptAlgorithm encryptAlgorithm, byte[] key)
            throws EncoderException {
        if (source == null || encryptAlgorithm == null) {
            return null;
        }
        return CoderUtil.getBasicCoder(encryptAlgorithm, key).encode(source);
    }

    public static byte[] decode(byte[] source, EncryptAlgorithm encryptAlgorithm, byte[] key)
            throws DecoderException {
        if (source == null || encryptAlgorithm == null) {
            return null;
        }
        return CoderUtil.getBasicCoder(encryptAlgorithm, key).decode(source);
    }

    public static String encode(String source, EncryptAlgorithm encryptAlgorithm, byte[] key)
            throws EncoderException {
        if (source == null || encryptAlgorithm == null) {
            return null;
        }
        return CoderUtil.getBasicCoder(encryptAlgorithm, key).encode(source);
    }

    public static String decode(String source, EncryptAlgorithm encryptAlgorithm, byte[] key)
            throws DecoderException {
        if (source == null || encryptAlgorithm == null) {
            return null;
        }
        return CoderUtil.getBasicCoder(encryptAlgorithm, key).decode(source);
    }

    /**
     * 
     *
     * @param encryptAlgorithm
     * @param key
     * @return
     */
    private static BasicCoder newBasicCoder(EncryptAlgorithm encryptAlgorithm, byte[] key) {
        EncryptAlgorithm algorithm = encryptAlgorithm;
        BasicCoder basicCoder = null;
        switch (algorithm) {
        case DESEDE:
            basicCoder = new DESedeCoder(key);
            break;
        default:
            basicCoder = new DESedeCoder(key);
            break;
        }
        return basicCoder;
    }

    /**
     * ?encryptAlgorithm ?
     *
     * @param encryptAlgorithm 
     * @param key              
     * @return
     */
    private static BasicCoder getBasicCoder(EncryptAlgorithm encryptAlgorithm, byte[] key) {
        String identifier = encryptAlgorithm.identifier() + StringUtil.bytesToString(key);
        BasicCoder basicCoder = encoderWeakHashMap.get(identifier);
        if (basicCoder == null) {
            basicCoder = CoderUtil.newBasicCoder(encryptAlgorithm, key);
            encoderWeakHashMap.put(identifier, basicCoder);
        }
        return basicCoder;
    }

}