1package org.bouncycastle.util.encoders;
2
3import java.io.ByteArrayOutputStream;
4import java.io.IOException;
5import java.io.OutputStream;
6
7import org.bouncycastle.util.Strings;
8
9/**
10 * Utility class for converting hex data to bytes and back again.
11 */
12public class Hex
13{
14    private static final Encoder encoder = new HexEncoder();
15
16    public static String toHexString(
17        byte[] data)
18    {
19        return toHexString(data, 0, data.length);
20    }
21
22    public static String toHexString(
23        byte[] data,
24        int    off,
25        int    length)
26    {
27        byte[] encoded = encode(data, off, length);
28        return Strings.fromByteArray(encoded);
29    }
30
31    /**
32     * encode the input data producing a Hex encoded byte array.
33     *
34     * @return a byte array containing the Hex encoded data.
35     */
36    public static byte[] encode(
37        byte[]    data)
38    {
39        return encode(data, 0, data.length);
40    }
41
42    /**
43     * encode the input data producing a Hex encoded byte array.
44     *
45     * @return a byte array containing the Hex encoded data.
46     */
47    public static byte[] encode(
48        byte[]    data,
49        int       off,
50        int       length)
51    {
52        ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
53
54        try
55        {
56            encoder.encode(data, off, length, bOut);
57        }
58        catch (Exception e)
59        {
60            throw new EncoderException("exception encoding Hex string: " + e.getMessage(), e);
61        }
62
63        return bOut.toByteArray();
64    }
65
66    /**
67     * Hex encode the byte data writing it to the given output stream.
68     *
69     * @return the number of bytes produced.
70     */
71    public static int encode(
72        byte[]         data,
73        OutputStream   out)
74        throws IOException
75    {
76        return encoder.encode(data, 0, data.length, out);
77    }
78
79    /**
80     * Hex encode the byte data writing it to the given output stream.
81     *
82     * @return the number of bytes produced.
83     */
84    public static int encode(
85        byte[]         data,
86        int            off,
87        int            length,
88        OutputStream   out)
89        throws IOException
90    {
91        return encoder.encode(data, off, length, out);
92    }
93
94    /**
95     * decode the Hex encoded input data. It is assumed the input data is valid.
96     *
97     * @return a byte array representing the decoded data.
98     */
99    public static byte[] decode(
100        byte[]    data)
101    {
102        ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
103
104        try
105        {
106            encoder.decode(data, 0, data.length, bOut);
107        }
108        catch (Exception e)
109        {
110            throw new DecoderException("exception decoding Hex data: " + e.getMessage(), e);
111        }
112
113        return bOut.toByteArray();
114    }
115
116    /**
117     * decode the Hex encoded String data - whitespace will be ignored.
118     *
119     * @return a byte array representing the decoded data.
120     */
121    public static byte[] decode(
122        String    data)
123    {
124        ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
125
126        try
127        {
128            encoder.decode(data, bOut);
129        }
130        catch (Exception e)
131        {
132            throw new DecoderException("exception decoding Hex string: " + e.getMessage(), e);
133        }
134
135        return bOut.toByteArray();
136    }
137
138    /**
139     * decode the Hex encoded String data writing it to the given output stream,
140     * whitespace characters will be ignored.
141     *
142     * @return the number of bytes produced.
143     */
144    public static int decode(
145        String          data,
146        OutputStream    out)
147        throws IOException
148    {
149        return encoder.decode(data, out);
150    }
151}
152