com.ucpaas.utils.EncryptUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.ucpaas.utils.EncryptUtil.java

Source

/*
 *  Copyright (c) 2013 The CCP project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a Beijing Speedtong Information Technology Co.,Ltd license
 *  that can be found in the LICENSE file in the root of the web site.
 *
 *   http://www.cloopen.com
 *
 *  An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */
package com.ucpaas.utils;

import java.security.MessageDigest;

import org.apache.commons.codec.binary.Base64;

public class EncryptUtil {

    private static final String UTF8 = "utf-8";

    /** 
     * MD5?? 
     * @param src 
     * @return 
     * @throws Exception 
     */
    public String md5Digest(String src) throws Exception {
        // ??, ?MD5, SHA-1  
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] b = md.digest(src.getBytes(UTF8));
        return this.byte2HexStr(b);
    }

    /** 
     * BASE64?
     * @param src 
     * @return 
     * @throws Exception 
     */
    public static String base64Encoder(String src) throws Exception {
        Base64 encoder = new Base64();
        return encoder.encodeToString(src.getBytes(UTF8));
    }

    /** 
     * BASE64?
     * @param dest 
     * @return 
     * @throws Exception 
     */
    public static String base64Decoder(String dest) throws Exception {
        Base64 decoder = new Base64();
        return new String(decoder.decode(dest), UTF8);
    }

    /** 
     * 16 
     * @param b 
     * @return 
     */
    private String byte2HexStr(byte[] b) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < b.length; i++) {
            String s = Integer.toHexString(b[i] & 0xFF);
            if (s.length() == 1) {
                sb.append("0");
            }
            sb.append(s.toUpperCase());
        }
        return sb.toString();
    }
}