package com.FitBank.crypt; /** * Clase que se encarga de convertir HEX 2 String. */ public class HexString{ /** * Método que se encarga convertir String 2 HEX. * @param s String Valor string * @return String buffer */ public static String stringToHex(String s){ byte[] stringBytes = s.getBytes(); return HexString.bufferToHex(stringBytes); } /** * Método que se encarga convertir bytes in buffer 2 String. * @param buffer[] byte Valor bytes en buffer * @return String buffer */ public static String bufferToHex(byte buffer[]){ return HexString.bufferToHex(buffer, 0, buffer.length); } /** * Método que se encarga convertir bytes in buffer 2 HEX String. * @param buffer[] byte Valor bytes en buffer * @param startOffset int posición inicial * @param length int longitud de bytes * @return String buffer */ public static String bufferToHex(byte buffer[], int startOffset, int length){ StringBuffer hexString = new StringBuffer(2 * length); int endOffset = startOffset + length; for (int i = startOffset; i < endOffset; i++){ HexString.appendHexPair(buffer[i], hexString); } return hexString.toString(); } /** * Método que se encarga convertir HEX 2 String. * @param hexString String Valor HEX * @return String HEX convertido a string * @throws NumberFormatException Excepción Genérica */ public static String hexToString(String hexString) throws NumberFormatException{ byte[] bytes = HexString.hexToBuffer(hexString); return new String(bytes); } /** * Método que se encarga convertir HEX 2 byte. * @param hexString String Valor HEX * @return byte[] HEX convertido a bytes * @throws NumberFormatException Excepción Genérica */ public static byte[] hexToBuffer(String hexString) throws NumberFormatException{ int length = hexString.length(); byte[] buffer = new byte[(length + 1) / 2]; boolean evenByte = true; byte nextByte = 0; int bufferOffset = 0; if ((length % 2) == 1) evenByte = false; for (int i = 0; i < length; i++){ char c = hexString.charAt(i); int nibble; if ((c >= '0') && (c <= '9')) nibble = c - '0'; else if ((c >= 'A') && (c <= 'F')) nibble = c - 'A' + 0x0A; else if ((c >= 'a') && (c <= 'f')) nibble = c - 'a' + 0x0A; else throw new NumberFormatException("Invalid hex digit '" + c + "'."); if (evenByte) { nextByte = (byte) (nibble << 4); } else { nextByte += (byte) nibble; buffer[bufferOffset++] = nextByte; } evenByte = ! evenByte; } return buffer; } /** * Método que se encarga agreagar bytes 2 HEX. * @param hexString StringBuffer Valor HEX * @param b byte bytes q necesitan agregar a HEX */ private static void appendHexPair(byte b, StringBuffer hexString){ char highNibble = kHexChars[(b & 0xF0) >> 4]; char lowNibble = kHexChars[b & 0x0F]; hexString.append(highNibble); hexString.append(lowNibble); } private static final char kHexChars[] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' }; /** * Byte a Hexadecimal * * @param b * @return */ public static String byteArrayToHexString(byte[] b) { StringBuffer sb = new StringBuffer(b.length * 2); for (byte element : b) { int v = element & 0xff; if (v < 16) { sb.append('0'); } sb.append(Integer.toHexString(v)); } return sb.toString().toUpperCase(); } /** * Hexadecimal a String * * @param s * @return */ public static byte[] hexStringToByteArray(String s) { byte[] b = new byte[s.length() / 2]; for (int i = 0; i < b.length; i++) { int index = i * 2; int v = Integer.parseInt(s.substring(index, index + 2), 16); b[i] = (byte) v; } return b; } }