package com.tcl.ah.security;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {
    public static final String mAlgorithm ="MD5";

    public static String getHash(InputStream is)throws IOException,NoSuchAlgorithmException {
        MessageDigest mDigest =null ;
        byte mBuffer[]=new byte[1024*8];
        int len ;
        mDigest = MessageDigest.getInstance(mAlgorithm);
        while((len=is.read(mBuffer))!=-1){
            mDigest.update(mBuffer,0,len);
        }
        byte [] md5Bytes = mDigest.digest();
        return toHexString(md5Bytes);
    }
    private static String toHexString(byte[] bytes){
        StringBuffer hexbuffer = new StringBuffer();
        for(int i = 0;i<bytes.length;i++){
            int val = ((int)bytes[i])&0xff;
            if(val<16){
                hexbuffer.append("0");
            }
            hexbuffer.append(Integer.toHexString(val));
        }
        return hexbuffer.toString();
    }

    public static String getHash(File file)throws IOException,NoSuchAlgorithmException{
        InputStream is =null;
        try {
            is = new FileInputStream(file);
            return getHash(is);
        }finally {
            try {
                if (is != null) is.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
    public static String getHash(String value)throws IOException,NoSuchAlgorithmException{
        InputStream is =null;
        try{
            is = new ByteArrayInputStream(value.getBytes());
            return getHash(is);
        }finally {
            try {
                if (is != null) is.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

    public static String getEnhanceHash(long size,InputStream is1,InputStream is2 ,InputStream is3)throws IOException,NoSuchAlgorithmException{
        MessageDigest mDigest =null ;
        byte mBuffer[]=new byte[1024*8];
        int len ;
        mDigest = MessageDigest.getInstance(mAlgorithm);
        int size1 =2012;
        int size2 =1999;
        int size3 =2010;
        int total = size1 ;
        while((len=is1.read(mBuffer))!=-1){
            if(len<=total) {
                mDigest.update(mBuffer, 0, len);
                total =total-len;
            }else{
                mDigest.update(mBuffer,0,total);
                break;
            }
        }
        total =size2;
        is2.skip(size/2-size2);
        while((len=is2.read(mBuffer))!=-1){
            if(len<=total) {
                mDigest.update(mBuffer, 0, len);
                total =total-len;
            }else{
                mDigest.update(mBuffer,0,total);
                break;
            }
        }
        total =size3;
        is3.skip(size-size3);
        while((len=is3.read(mBuffer))!=-1){
            if(len<=total) {
                mDigest.update(mBuffer, 0, len);
                total =total-len;
            }else{
                mDigest.update(mBuffer,0,total);
                break;
            }
        }
        byte [] md5Bytes = mDigest.digest();
        return toHexString(md5Bytes);
    }

    public static String getEnhanceHash(File file)throws IOException,NoSuchAlgorithmException{
        InputStream is =null;
        InputStream is1 =null;
        InputStream is2 =null;
        InputStream is3 =null;
        try {
            long size =file.length();
            is = new FileInputStream(file);
            is1 = new FileInputStream(file);
            is2 = new FileInputStream(file);
            is3 = new FileInputStream(file);
            return getEnhanceHash(size,is1,is2,is3);
        }finally {
            try {
                if (is != null) is.close();
            }catch (IOException e){
                e.printStackTrace();
            }
            try {
                if (is1 != null) is1.close();
            }catch (IOException e){
                e.printStackTrace();
            }
            try {
                if (is2 != null) is2.close();
            }catch (IOException e){
                e.printStackTrace();
            }
            try {
                if (is3 != null) is3.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }



}
