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

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Data Type » HexadecimalScreenshots 
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 = 
    {
        0x300x310x320x330x340x350x360x370x380x39,
        0x610x620x630x640x650x66
    };
    
    /**
     * 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[i0xf0>> 4;
            int r = data[i0x0f
            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[i0xf0>> 4;
            int r = data[i0x0f
            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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.