com.apabi.qrcode.utils.DigestUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.apabi.qrcode.utils.DigestUtils.java

Source

/**
 * Copyright (c) 2005-2009 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * 
 * $Id: DigestUtils.java 799 2009-12-31 15:34:10Z calvinxiu $
 */
package com.apabi.qrcode.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;

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

/**
 * ?SHA-1/MD5??.
 * 
 * ?HexBase64???.
 * 
 * @author calvin
 */
public abstract class DigestUtils {
    private static final int BUFFER_LENGTH = 1024;

    private DigestUtils() {

    }

    private static final String SHA1 = "SHA-1";
    private static final String MD5 = "MD5";

    // -- String Hash function --//
    /**
     * sha1, Hex?.
     */
    public static String sha1ToHex(final String input) {
        byte[] digestResult = digest(input, SHA1);
        return EncodeUtils.hexEncode(digestResult);
    }

    /**
     * md5, Hex?.
     */
    public static String md5ToHex(final String input) {
        byte[] digestResult = digest(input, MD5);
        return EncodeUtils.hexEncode(digestResult);
    }

    /**
     * md5, Hex???.
     */
    public static String md5ToHex(final String input, final String charset) {
        byte[] digestResult;
        try {
            digestResult = digest(input.getBytes(charset), MD5);
        } catch (UnsupportedEncodingException e) {
            throw new IllegalStateException("Security exception", e);
        }
        return EncodeUtils.hexEncode(digestResult);
    }

    /**
     * charsetinputmd5?.
     * 
     * @param input   ?md5?
     * 
     * @param charset 
     * @return
     */
    public static byte[] encryptByMd5(String input, String charset) {
        try {
            return digest(input.getBytes(charset), MD5);
        } catch (UnsupportedEncodingException e) {
            throw new IllegalStateException("Security exception", e);
        }
    }

    /**
     * sha1, Base64?.
     */
    public static String sha1ToBase64(final String input) {
        byte[] digestResult = digest(input, SHA1);
        return EncodeUtils.base64Encode(digestResult);
    }

    /**
     * sha1, Base64?URL.
     */
    public static String sha1ToBase64UrlSafe(final String input) {
        byte[] digestResult = digest(input, SHA1);
        return EncodeUtils.base64UrlSafeEncode(digestResult);
    }

    /**
     * , ?md5sha1??.
     */
    private static byte[] digest(final String input, final String algorithm) {
        return digest(input.getBytes(), algorithm);
    }

    /**
     * , ?md5sha1.
     */
    private static byte[] digest(final byte[] data, final String algorithm) {
        try {
            MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
            return messageDigest.digest(data);
        } catch (GeneralSecurityException e) {
            throw new IllegalStateException("Security exception", e);
        }
    }

    // -- File Hash function --//
    /**
     * md5,Hex?.
     */
    public static String md5ToHex(final InputStream input) throws IOException {
        return digest(input, MD5);
    }

    /**
     * sha1,Hex?.
     */
    public static String sha1ToHex(final InputStream input) throws IOException {
        return digest(input, SHA1);
    }

    /**
     * , ?md5sha1.
     */
    private static String digest(final InputStream input, final String algorithm) throws IOException {
        try {
            MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
            int bufferLength = BUFFER_LENGTH;
            byte[] buffer = new byte[bufferLength];
            int read = input.read(buffer, 0, bufferLength);

            while (read > -1) {
                messageDigest.update(buffer, 0, read);
                read = input.read(buffer, 0, bufferLength);
            }

            return EncodeUtils.hexEncode(messageDigest.digest());

        } catch (GeneralSecurityException e) {
            throw new IllegalStateException("Security exception", e);
        }
    }

    public static void main(final String[] as) {
        //I1+qP18tmDrGtMAvuGgQjQ==
        String str = "founder";
        byte[] bytes = DigestUtils.encryptByMd5(str, "UTF-16LE");
        System.out.println(Base64.encodeBase64String(DigestUtils.encryptByMd5(str, "UTF-16LE")));
    }
}