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