io.manasobi.utils.DigestUtils.java Source code

Java tutorial

Introduction

Here is the source code for io.manasobi.utils.DigestUtils.java

Source

/*
 * Copyright 2002-2012 the original author or authors.
 *
 * 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 io.manasobi.utils;

import java.security.MessageDigest;

import io.manasobi.exception.DigestUtilsException;

/**
 *  ??  characterset ? base64  ?  ?/  ? .
 * 
 * @author manasobi
 * @since 1.0.0
 */
public final class DigestUtils {

    private DigestUtils() {
    }

    /**
     * ? ?? charsetName?  ?.
     * 
     * @param str         ? ?
     * @param charsetName ?  ?
     * @return ?? ?
     */
    public static String encodeCharset(String str, String charsetName) {

        String result = "";

        try {
            result = new String(str.getBytes(charsetName), charsetName);
        } catch (Exception e) {
            throw new DigestUtilsException(e.getMessage());
        }

        return result;
    }

    /**
     * ? ?? Base64 ? ?.
     * 
     * @param str       Base64 ? ?
     * @return Base64 ?? ?
     */
    public static String encodeBase64String(String str) {
        return org.apache.commons.codec.binary.Base64.encodeBase64String(str.getBytes());
    }

    /**
     * ? ? ?? Base64 ? ?.
     * 
     * @param binaryData Base64 ? binary data
     * @return Base64 ?? ?
     */
    public static String encodeBase64String(byte[] binaryData) {
        return org.apache.commons.codec.binary.Base64.encodeBase64String(binaryData);
    }

    /**
     * ? ?? Base64 ? ?.
     * 
     * @param str       Base64 ? ?
     * @return Base64 ?? byte array
     */
    public static byte[] encodeBase64ByteArray(String str) {
        return org.apache.commons.codec.binary.Base64.encodeBase64(str.getBytes());
    }

    /**
     * ? ? ?? Base64 ? ?.
     * 
     * @param binaryData Base64 ? binary data
     * @return Base64 ?? byte array
     */
    public static byte[] encodeBase64ByteArray(byte[] binaryData) {
        return org.apache.commons.codec.binary.Base64.encodeBase64(binaryData);
    }

    /**
     * ? ?? Base64 ? .
     * 
     * @param str Base64  ?
     * @return Base64 ? ?
     */
    public static String decodeBase64String(String str) {
        return new String(org.apache.commons.codec.binary.Base64.decodeBase64(str));
    }

    /**
     * ? binary data Base64 ? .
     * 
     * @param binaryData Base64  binary data
     * @return Base64 ? ?
     */
    public static String decodeBase64String(byte[] binaryData) {
        return new String(org.apache.commons.codec.binary.Base64.decodeBase64(binaryData));
    }

    /**
     * ? ?? Base64 ? .
     * 
     * @param str Base64  ?
     * @return Base64 ? byte array
     */
    public static byte[] decodeBase64ByteArray(String str) {
        return org.apache.commons.codec.binary.Base64.decodeBase64(str);
    }

    /**
     * ? binary data Base64 ? .
     * 
     * @param binaryData Base64  binary data
     * @return Base64 ? byte array
     */
    public static byte[] decodeBase64ByteArray(byte[] binaryData) {
        return org.apache.commons.codec.binary.Base64.decodeBase64(binaryData);
    }

    private static final int HEX_FF = 0xff;
    private static final int HEX_10 = 0x10;
    private static final int HEX = 16;

    private static enum Secure {

        MD5("md5"), SHA_1("sha-1"), SHA_256("sha-256");

        private String algorithm;

        private Secure(String algorithm) {
            this.algorithm = algorithm;
        }

        public String getAlgorithm() {
            return algorithm;
        }
    }

    /**
     *  ? ?  .<br>
     *    - "MD5", "SHA-1", "SHA-256"<br>
     * ? ? ? ?? ? - MD5: 32?, SHA-1: 40?, SHA-256: 64?
     * 
     * @param password    ?
     * @param secure    
     * @return ? ? ? ?
     */
    public static String encodePassword(String password, Secure secure) {

        byte[] unencodedPassword = password.getBytes();

        MessageDigest md = null;

        try {
            md = MessageDigest.getInstance(secure.getAlgorithm());
        } catch (Exception e) {
            throw new DigestUtilsException(e.getMessage());
        }

        md.reset();
        md.update(unencodedPassword);

        byte[] encodedPassword = md.digest();

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < encodedPassword.length; i++) {
            if (((int) encodedPassword[i] & HEX_FF) < HEX_10) {
                sb.append("0");
            }

            sb.append(Long.toString((int) encodedPassword[i] & HEX_FF, HEX));
        }

        return sb.toString();
    }

}