com.webarch.common.lang.Digest.java Source code

Java tutorial

Introduction

Here is the source code for com.webarch.common.lang.Digest.java

Source

/*
    Copyright  DR.YangLong
    
    Licensed 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.
*/
package com.webarch.common.lang;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * functional describe:
 *
 * @author DR.YangLong [410357434@163.com]
 * @version 1.0 2015/5/13 20:50
 */
public class Digest {
    /**
     * ??????
     *
     * @param values ?
     * @return ????
     */
    public static String generateSourceString(String... values) {
        List<String> list = Arrays.asList(values);
        Collections.sort(list);
        return StringUtils.join(list, "");
    }

    /**
     * sha1??????UTF-8
     *
     * @param sources ?
     * @return ?
     */
    public static String signatureString(final String... sources) {
        String source;
        if (sources.length == 1) {
            source = sources[0];
        } else {
            source = generateSourceString(sources);
        }
        return DigestUtils.sha1Hex(source);
    }

    /**
     * base64
     *
     * @param charset  UTF-8
     * @param soureces ?
     * @return <code>sources</code>??
     */
    public static String[] base64Code(String charset, final String... soureces) {
        try {
            if (charset == null || "".equals(charset))
                charset = "UTF-8";
            String[] results = new String[soureces.length];
            for (int i = 0; i < soureces.length; i++) {
                results[i] = Base64.encodeBase64URLSafeString(soureces[i].getBytes(charset));
            }
            return results;
        } catch (UnsupportedEncodingException E) {
            return null;
        }
    }

    /**
     * base64?
     *
     * @param charset utf-8
     * @param sources 
     * @return ?<code>sources</code>
     */
    public static String[] base64Decode(String charset, final String... sources) {
        try {
            if (charset == null || "".equals(charset))
                charset = "UTF-8";
            String[] results = new String[sources.length];
            for (int i = 0; i < sources.length; i++) {
                results[i] = new String(Base64.decodeBase64(sources[i]), charset);
            }
            return results;
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }

    public static void main(String args[]) {
        System.out.println(generateSourceString("sdfa", "ggggg", "wwwww"));
        System.out.println(signatureString("sdfa", "ggggg", "wwwww"));
        System.out.println("" + DigestUtils.md5Hex("?"));
        System.out.println(
                StringUtils.join(base64Code("utf-8", "46d045ff5190f6ea93739da6c0aa19bc", "ggggg", "wwwww"), "-"));
        System.out.println(StringUtils.join(
                base64Decode("utf-8", "NDZkMDQ1ZmY1MTkwZjZlYTkzNzM5ZGE2YzBhYTE5YmM", "Z2dnZ2c", "d3d3d3c"), "-"));
    }
}