Example usage for org.apache.commons.codec.binary Hex encodeHex

List of usage examples for org.apache.commons.codec.binary Hex encodeHex

Introduction

In this page you can find the example usage for org.apache.commons.codec.binary Hex encodeHex.

Prototype

public static char[] encodeHex(byte[] data) 

Source Link

Document

Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.

Usage

From source file:edu.harvard.iq.dataverse.ingest.tabulardata.impl.plugins.por.PORFileReaderSpi.java

@Override
public boolean canDecodeInput(Object source) throws IOException {
    if (!(source instanceof BufferedInputStream)) {
        return false;
    }//from ww w  .j a  va 2 s .  co  m
    if (source == null) {
        throw new IllegalArgumentException("source == null!");
    }
    BufferedInputStream stream = (BufferedInputStream) source;
    dbgLog.fine("applying the por test\n");

    byte[] b = new byte[POR_HEADER_SIZE];

    if (stream.markSupported()) {
        stream.mark(0);
    }

    int nbytes = stream.read(b, 0, POR_HEADER_SIZE);

    //printHexDump(b, "hex dump of the byte-array");

    if (nbytes == 0) {
        throw new IOException();
    } else if (nbytes < 491) {
        // size test
        dbgLog.fine("this file is NOT spss-por type");
        return false;
    }

    if (stream.markSupported()) {
        stream.reset();
    }

    boolean DEBUG = false;

    //windows [0D0A]=>   [1310] = [CR/LF]
    //unix    [0A]  =>   [10]
    //mac     [0D]  =>   [13]
    // 3char  [0D0D0A]=> [131310] spss for windows rel 15
    // expected results
    // unix    case: [0A]   : [80], [161], [242], [323], [404], [485]
    // windows case: [0D0A] : [81], [163], [245], [327], [409], [491]
    //  : [0D0D0A] : [82], [165], [248], [331], [414], [495]

    // convert b into a ByteBuffer

    ByteBuffer buff = ByteBuffer.wrap(b);
    byte[] nlch = new byte[36];
    int pos1;
    int pos2;
    int pos3;
    int ucase = 0;
    int wcase = 0;
    int mcase = 0;
    int three = 0;
    int nolines = 6;
    int nocols = 80;
    for (int i = 0; i < nolines; ++i) {
        int baseBias = nocols * (i + 1);
        // 1-char case
        pos1 = baseBias + i;
        buff.position(pos1);
        dbgLog.finer("\tposition(1)=" + buff.position());
        int j = 6 * i;
        nlch[j] = buff.get();

        if (nlch[j] == 10) {
            ucase++;
        } else if (nlch[j] == 13) {
            mcase++;
        }

        // 2-char case
        pos2 = baseBias + 2 * i;
        buff.position(pos2);
        dbgLog.finer("\tposition(2)=" + buff.position());

        nlch[j + 1] = buff.get();
        nlch[j + 2] = buff.get();

        // 3-char case
        pos3 = baseBias + 3 * i;
        buff.position(pos3);
        dbgLog.finer("\tposition(3)=" + buff.position());

        nlch[j + 3] = buff.get();
        nlch[j + 4] = buff.get();
        nlch[j + 5] = buff.get();

        dbgLog.finer(i + "-th iteration position =" + nlch[j] + "\t" + nlch[j + 1] + "\t" + nlch[j + 2]);
        dbgLog.finer(i + "-th iteration position =" + nlch[j + 3] + "\t" + nlch[j + 4] + "\t" + nlch[j + 5]);

        if ((nlch[j + 3] == 13) && (nlch[j + 4] == 13) && (nlch[j + 5] == 10)) {
            three++;
        } else if ((nlch[j + 1] == 13) && (nlch[j + 2] == 10)) {
            wcase++;
        }

        buff.rewind();
    }
    if (three == nolines) {
        dbgLog.fine("0D0D0A case");
        windowsNewLine = false;
    } else if ((ucase == nolines) && (wcase < nolines)) {
        dbgLog.fine("0A case");
        windowsNewLine = false;
    } else if ((ucase < nolines) && (wcase == nolines)) {
        dbgLog.fine("0D0A case");
    } else if ((mcase == nolines) && (wcase < nolines)) {
        dbgLog.fine("0D case");
        windowsNewLine = false;
    }

    buff.rewind();
    int PORmarkPosition = POR_MARK_POSITION_DEFAULT;
    if (windowsNewLine) {
        PORmarkPosition = PORmarkPosition + 5;
    } else if (three == nolines) {
        PORmarkPosition = PORmarkPosition + 10;
    }

    byte[] pormark = new byte[8];
    buff.position(PORmarkPosition);
    buff.get(pormark, 0, 8);
    String pormarks = new String(pormark);

    dbgLog.fine(
            "pormark[hex: 53 50 53 53 50 4F 52 54 == SPSSPORT] =>" + new String(Hex.encodeHex(pormark)) + "<-");

    if (pormarks.equals(POR_MARK)) {
        dbgLog.fine("this file is spss-por type");
        return true;
    } else {
        dbgLog.fine("this file is NOT spss-por type");
    }
    return false;
}

From source file:edu.harvard.iq.dvn.ingest.statdataio.impl.plugins.dta.DTAFileReaderSpi.java

@Override
public boolean canDecodeInput(File file) throws IOException {
    if (file == null) {
        throw new IllegalArgumentException("file == null!");
    }/*w w  w.jav a  2  s.  co m*/
    if (!file.canRead()) {
        throw new IIOException("cannot read the input file");
    }

    // set-up a FileChannel instance for a given file object
    FileChannel srcChannel = new FileInputStream(file).getChannel();

    // create a read-only MappedByteBuffer
    MappedByteBuffer buff = srcChannel.map(FileChannel.MapMode.READ_ONLY, 0, DTA_HEADER_SIZE);

    //printHexDump(buff, "hex dump of the byte-buffer");

    buff.rewind();

    boolean result = false;

    dbgLog.fine("applying the dta test\n");

    byte[] hdr4 = new byte[4];
    buff.get(hdr4, 0, 4);

    dbgLog.info("hex dump: 1st 4bytes =>" + new String(Hex.encodeHex(hdr4)) + "<-");

    if (hdr4[2] != 1) {
        dbgLog.fine("3rd byte is not 1: given file is not stata-dta type");
        return false;
    } else if ((hdr4[1] != 1) && (hdr4[1] != 2)) {
        dbgLog.fine("2nd byte is neither 0 nor 1: this file is not stata-dta type");
        return false;
    } else if (!stataReleaseNumber.containsKey(hdr4[0])) {
        dbgLog.fine("1st byte (" + hdr4[0]
                + ") is not within the ingestable range [rel. 3-10]: this file is NOT stata-dta type");
        return false;
    } else {
        dbgLog.fine("this file is stata-dta type: " + stataReleaseNumber.get(hdr4[0]) + "(No in HEX=" + hdr4[0]
                + ")");
        return true;
    }
}

From source file:edu.psu.citeseerx.utility.FileDigest.java

/**
 * Informs if the SHA-1 file digest is equals to a given digest
 * @param digest        a <code>byte[]</code> that represents a file digest
 * @param toValidate    File to validate againts digest
 * @return   true if the SHA-1 file digest is equals to digest
 */// w ww.  j  a  v  a  2 s  .com
public static boolean validateSHA1(byte[] digest, File toValidate) {
    String fileDigest = new String(Hex.encodeHex(digest));
    return validateSHA1(fileDigest, toValidate);
}

From source file:br.vbathke.jenkins.UITestCaptureProjectAction.java

public String md5Hash(String data) throws NoSuchAlgorithmException {
    MessageDigest messageDigest;/*  w w w  .  ja v a2  s  .c om*/
    messageDigest = MessageDigest.getInstance("MD5");
    messageDigest.reset();
    messageDigest.update(data.getBytes(Charset.forName("UTF8")));
    byte[] resultByte = messageDigest.digest();
    String result = new String(Hex.encodeHex(resultByte));
    return result;
}

From source file:net.padlocksoftware.padlock.DefaultMacAddressProvider.java

private Set<String> getMACAddresses() {
    Set<String> addresses = new HashSet<String>();

    if (!FORCE_MACADDR_SHELL) {
        try {//w ww  . j av a 2  s . c o m
            Method method = NetworkInterface.class.getMethod("getHardwareAddress", (Class[]) null);

            Enumeration<NetworkInterface> ifs = NetworkInterface.getNetworkInterfaces();
            if (ifs != null) {
                while (ifs.hasMoreElements()) {
                    NetworkInterface iface = ifs.nextElement();
                    byte[] hardware = (byte[]) method.invoke(iface);
                    if (hardware != null && hardware.length == 6 && hardware[1] != (byte) 0xff) {
                        if (permitVMAddresses || !isVirtualAddress(hardware)) {
                            addresses.add(new String(Hex.encodeHex(hardware)));
                        }
                    }
                }
            }
        } catch (Exception ex) {
            logger.log(Level.FINE, null, ex);
        }

        // may not have found any
        if (!addresses.isEmpty()) {
            return addresses;
        }
    }

    // otherwise default to JDK1.5 way
    return useShellToFindAddresses();
}

From source file:controller.InputSanitizer.java

private static String getDigest(InputStream is, MessageDigest md, int byteArraySize)
        throws NoSuchAlgorithmException, IOException {

    md.reset();/*from  w ww  .j av  a 2s. c  om*/
    byte[] bytes = new byte[byteArraySize];
    int numBytes;
    while ((numBytes = is.read(bytes)) != -1) {
        md.update(bytes, 0, numBytes);
    }
    byte[] digest = md.digest();
    String result = new String(Hex.encodeHex(digest));
    return result;
}

From source file:at.diamonddogs.util.Utils.java

/**
 * Computes a MD5 hash from an byte array
 *
 * @param data the input data/*from w  ww.j a va2 s.  c  om*/
 * @return the MD5 hash or <code>null</code> if an error occured
 */
public static String getMD5Hash(byte[] data) {
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        return new String(Hex.encodeHex(md.digest(data)));
    } catch (Throwable tr) {
        Log.w(TAG, "Could not md5 data");
        return null;
    }
}

From source file:com.fegor.alfresco.security.crypto.Crypto.java

public String getSalt() {
    return (new String(Hex.encodeHex(salt_pos)));
}

From source file:com.glaf.core.security.RSAUtils.java

/**
 * //from w  w  w. j  a  v a2s .  co  m
 * <p />
 * {@code plaintext}  {@code null}  {@code null}
 * 
 * @param plaintext
 *            
 * @return 
 */
public static String encryptString(String plaintext) {
    if (plaintext == null) {
        return null;
    }
    byte[] data = plaintext.getBytes();
    KeyPair keyPair = getKeyPair();
    try {
        byte[] en_data = encrypt((RSAPublicKey) keyPair.getPublic(), data);
        return new String(Hex.encodeHex(en_data));
    } catch (Exception ex) {
        LOGGER.error(ex.getCause().getMessage());
    }
    return null;
}

From source file:com.moz.fiji.schema.util.TestJsonEntityIdParser.java

@Test
public void testShouldWorkWithRKF2RawLayout() throws Exception {
    final TableLayoutDesc desc = FijiTableLayouts.getLayout(FijiTableLayouts.PAGING_TEST);
    final FijiTableLayout layout = FijiTableLayout.newLayout(desc);
    final EntityIdFactory factory = EntityIdFactory.getFactory(layout);
    final byte[] rowKey = Bytes.toBytes(UNUSUAL_STRING_EID);
    final EntityId originalEid = factory.getEntityIdFromHBaseRowKey(rowKey);
    final JsonEntityIdParser restEid1 = JsonEntityIdParser.create(originalEid, layout);
    final JsonEntityIdParser restEid2 = JsonEntityIdParser
            .create(String.format("hbase=%s", Bytes.toStringBinary(rowKey)), layout);
    final JsonEntityIdParser restEid3 = JsonEntityIdParser
            .create(String.format("hbase_hex=%s", new String(Hex.encodeHex((rowKey)))), layout);

    // Resolved entity id should match origin entity id.
    assertEquals(originalEid, restEid1.getEntityId());
    assertEquals(originalEid, restEid2.getEntityId());
    assertEquals(originalEid, restEid3.getEntityId());
}