Util for digesting and encoding bytes/strings. : Hexadecimal « Data Type « Java






Util for digesting and encoding bytes/strings.

      

//Copyright 2007-2008 David Yu dyuproject@gmail.com
//------------------------------------------------------------------------
//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.dyuproject.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;

/**
 * Util for digesting and encoding bytes/strings.
 * 
 * @author David Yu
 * @created Sep 7, 2008
 */

public final class DigestUtil
{
    
    /**
     * "MD5"
     */
    public static final String MD5 = "MD5";
    /**
     * "SHA-1"
     */
    public static final String SHA1 = "SHA-1";
    /**
     * "SHA-256"
     */
    public static final String SHA256 = "SHA-256";
    
    /**
     * The hex chars as bytes.
     */
    public static final byte[] HEXADECIMAL = 
    {
        0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
        0x61, 0x62, 0x63, 0x64, 0x65, 0x66
    };
    
    /**
     * Returns the bytes {@code data} in hex form.
     */
    public static byte[] getHexBytes(byte[] data)
    {

        byte[] out = new byte[data.length*2];        
        for (int i=0,y=0; i<data.length; i++) 
        {            
            int l = (data[i] & 0xf0) >> 4;
            int r = data[i] & 0x0f; 
            out[y++] = HEXADECIMAL[l];
            out[y++] = HEXADECIMAL[r];       
        }
        return out;
    }
    
    /**
     * Returns the bytes {@code data} in hex form as characters.
     */
    public static char[] getHexChars(byte[] data)
    {

        char[] out = new char[data.length*2];        
        for (int i=0,y=0; i<data.length; i++) 
        {            
            int l = (data[i] & 0xf0) >> 4;
            int r = data[i] & 0x0f; 
            out[y++] = (char)HEXADECIMAL[l];
            out[y++] = (char)HEXADECIMAL[r];       
        }
        return out;
    }
    
    static String getHexString(byte[] data)    
    {
        return new String(getHexBytes(data));
    }
    
    /**
     * Returns the bytes {@code data} in hex form as string.
     */
    public static String getHexString(byte[] data, String charset) 
    throws UnsupportedEncodingException
    {
        return new String(getHexBytes(data), charset);
    }
    
    /**
     * Digests the {@code data} with the specified {@code type} and {@code charset}.
     */
    public static String getDigestedValue(String type, String data, String charset)
    throws UnsupportedEncodingException
    {
        return getDigestedValue(type, data.getBytes(charset), charset);
    }
    
    /**
     * Digests the {@code data} with the specified {@code type}.
     */
    public static String getDigestedValue(String type, String data)
    {
        return getDigestedValue(type, data.getBytes());
    }
    
    static String getDigestedValue(String type, byte[] data, String charset)
    {
        try
        {
            MessageDigest digest = MessageDigest.getInstance(type);
            return getHexString(digest.digest(data), charset);
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }
    
    static String getDigestedValue(String type, byte[] data)
    {
        try
        {
            MessageDigest digest = MessageDigest.getInstance(type);
            return getHexString(digest.digest(data));
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }
    
    /**
     * Digests the {@code data} with the specified {@code type} and returns the 
     * raw unencoded bytes.
     */
    public static byte[] getPlainDigestedValue(String type, byte[] data)
    {
        try
        {
            MessageDigest digest = MessageDigest.getInstance(type);
            return digest.digest(data);
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }
    
    /**
     * Digests the {@code data} with MD5 and returns the raw unencoded bytes.
     */
    public static String digestMD5(String data)
    {
        return getDigestedValue(MD5, data.getBytes());
    }
    
    /**
     * Digests the {@code data} with MD5 and the specified {@code charset} and 
     * returns the raw unencoded bytes.
     */
    public static String digestMD5(String data, String charset)
    {
        try
        {
            return getDigestedValue(MD5, data, charset);
        } 
        catch (Exception e)
        {            
            e.printStackTrace();
            return null;
        }
    }
    
    /**
     * Digests the {@code data} with SHA-1 and returns the raw unencoded bytes.
     */
    public static String digestSHA1(String data)
    {
        return getDigestedValue(SHA1, data.getBytes());
    }
    
    /**
     * Digests the {@code data} with SHA-1 and the specified {@code charset} and 
     * returns the raw unencoded bytes.
     */
    public static String digestSHA1(String data, String charset)
    {
        try
        {
            return getDigestedValue(SHA1, data, charset);
        } 
        catch (Exception e)
        {            
            e.printStackTrace();
            return null;
        }
    }
    
    /**
     * Digests the {@code data} with SHA-256 and returns the raw unencoded bytes.
     */
    public static String digestSHA256(String data)
    {
        return getDigestedValue(SHA256, data.getBytes());
    }
    
    /**
     * Digests the {@code data} with SHA-256 and the specified {@code charset} and 
     * returns the raw unencoded bytes.
     */
    public static String digestSHA256(String data, String charset)
    {
        try
        {
            return getDigestedValue(SHA256, data, charset);
        } 
        catch (Exception e)
        {            
            e.printStackTrace();
            return null;
        }
    }

}

   
    
    
    
    
    
  








Related examples in the same category

1.Convert Decimal to Hexadecimal
2.Convert from decimal to hexadecimal
3.Convert from Byte array to hexadecimal string
4.Convert from decimal to hexadecimal with leading zeroes and uppercase
5.Hex encoder and decoder.
6.Decode hex string to a byte array
7.Returns the hexadecimal value of the supplied byte array
8.Convert byte array to Hex String
9.Convert the bytes to a hex string representation of the bytes
10.Hex decimal dump
11.hex decoder
12.Get Base64 From HEX
13.Converts a hex string to a byte array.
14.append hex digit
15.dump an array of bytes in hex form
16.Get byte array from hex string
17.Check if the current character is an Hex Char <hex> ::= [0x30-0x39] | [0x41-0x46] | [0x61-0x66]
18.Helper function that returns a char from an hex
19.Hex encoder/decoder implementation borrowed from BouncyCastle
20.Java Hex dump Library
21.Provide hex utility for converting bytes to hex string
22.Number in hexadecimal format are used throughout Freenet.
23.Convert to/from Hex string
24.This class allows a number to be easily formatted as a hexadecimal number. The representation uses 0-f.
25.This class provides methods for working with hexadecimal representations of data.
26.Hex Util