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:com.docd.purefm.test.CommandLineFileTest.java

private String md5sum(final File file) {
    try {//from   w w w  .j ava  2  s.c o m
        final InputStream fis = new AutoCloseInputStream(new FileInputStream(file));
        return new String(Hex.encodeHex(DigestUtils.md5(fis)));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:de.tudarmstadt.ukp.dkpro.core.api.datasets.DatasetFactory.java

/**
 * Verify/download/update artifact in cache. Execute post-download actions.
 */// w w  w. jav  a2 s .  c  o m
private void materialize(DatasetDescription aDataset) throws IOException {
    Path root = resolve(aDataset);
    Collection<ArtifactDescription> artifacts = aDataset.getArtifacts().values();

    // First validate if local copies are still up-to-date
    boolean reload = false;
    packageValidationLoop: for (ArtifactDescription artifact : artifacts) {
        Path cachedFile = resolve(aDataset, artifact);
        if (!Files.exists(cachedFile)) {
            continue;
        }

        if (artifact.getSha1() != null) {
            String actual = getDigest(cachedFile, "SHA1");
            if (!artifact.getSha1().equals(actual)) {
                LOG.info("Local SHA1 hash mismatch on [" + cachedFile + "] - expected [" + artifact.getSha1()
                        + "] - actual [" + actual + "]");
                reload = true;
                break packageValidationLoop;
            } else {
                LOG.info("Local SHA1 hash verified on [" + cachedFile + "] - [" + actual + "]");
            }
        }
    }

    // If any of the packages are outdated, clear the cache and download again
    if (reload) {
        LOG.info("Clearing local cache for [" + root + "]");
        FileUtils.deleteQuietly(root.toFile());
    }

    for (ArtifactDescription artifact : artifacts) {
        Path cachedFile = resolve(aDataset, artifact);

        if (Files.exists(cachedFile)) {
            continue;
        }

        if (artifact.getText() != null) {
            Files.createDirectories(cachedFile.getParent());

            LOG.info("Creating [" + cachedFile + "]");
            try (Writer out = Files.newBufferedWriter(cachedFile, StandardCharsets.UTF_8)) {
                out.write(artifact.getText());
            }
        }

        if (artifact.getUrl() != null) {
            Files.createDirectories(cachedFile.getParent());

            MessageDigest sha1;
            try {
                sha1 = MessageDigest.getInstance("SHA1");
            } catch (NoSuchAlgorithmException e) {
                throw new IOException(e);
            }

            URL source = new URL(artifact.getUrl());

            LOG.info("Fetching [" + cachedFile + "]");

            URLConnection connection = source.openConnection();
            connection.setRequestProperty("User-Agent", "Java");

            try (InputStream is = connection.getInputStream()) {
                DigestInputStream sha1Filter = new DigestInputStream(is, sha1);
                Files.copy(sha1Filter, cachedFile);

                if (artifact.getSha1() != null) {
                    String sha1Hex = new String(Hex.encodeHex(sha1Filter.getMessageDigest().digest()));
                    if (!artifact.getSha1().equals(sha1Hex)) {
                        String message = "SHA1 mismatch. Expected [" + artifact.getSha1() + "] but got ["
                                + sha1Hex + "].";
                        LOG.error(message);
                        throw new IOException(message);
                    }
                }
            }
        }
    }

    // Perform a post-fetch action such as unpacking
    Path postActionCompleteMarker = resolve(aDataset).resolve(".postComplete");
    if (!Files.exists(postActionCompleteMarker)) {
        for (ArtifactDescription artifact : artifacts) {
            Path cachedFile = resolve(aDataset, artifact);

            List<ActionDescription> actions = artifact.getActions();
            if (actions != null && !actions.isEmpty()) {
                try {
                    for (ActionDescription action : actions) {
                        LOG.info("Post-download action [" + action.getAction() + "]");
                        Class<? extends Action_ImplBase> implClass = actionRegistry.get(action.getAction());

                        if (implClass == null) {
                            throw new IllegalStateException(
                                    "Unknown or unsupported action [" + action.getAction() + "]");
                        }

                        Action_ImplBase impl = implClass.newInstance();
                        impl.apply(action, aDataset, artifact, cachedFile);
                    }
                } catch (IllegalStateException e) {
                    throw e;
                } catch (IOException e) {
                    throw e;
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
        }
        Files.createFile(postActionCompleteMarker);
    }
}

From source file:com.google.sampling.experiential.model.Event.java

public static String getAnonymousId(String who) {
    MessageDigest messageDigest = null;
    try {//from  w w w .j a  v  a 2  s . c o m
        messageDigest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        //Log.info("Could not get MD5 algorithm");
        return null;
    }
    messageDigest.reset();
    messageDigest.update(who.getBytes(Charset.forName("UTF8")));
    byte[] resultByte = messageDigest.digest();
    return new String(Hex.encodeHex(resultByte));
}

From source file:cn.quickj.AbstractApplication.java

private void decryptQuickjLicense(String hex) throws Exception {
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    byte[] encrypted = Hex.decodeHex(hex.toCharArray());
    byte[] keydata = new byte[128];
    System.arraycopy(encrypted, 0, keydata, 0, 128);
    String key = new String(Hex.encodeHex(keydata));
    PublicKey pubKey = keyFactory
            .generatePublic(new RSAPublicKeySpec(new BigInteger(key, 16), new BigInteger("10001", 16)));
    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    cipher.init(Cipher.DECRYPT_MODE, pubKey);
    byte[] decrypted = new byte[encrypted.length];
    int outputOffset = 0;
    for (int offset = 128; offset < encrypted.length;) {
        int inputLen = (encrypted.length - offset) > 128 ? 128 : (encrypted.length - offset);
        outputOffset += cipher.doFinal(encrypted, offset, inputLen, decrypted, outputOffset);
        offset += inputLen;//from  w  w w. j  a v a2s  . c  o m
    }

    String licenseInfo = new String(decrypted, 0, outputOffset - 16, "utf8");
    String[] s = licenseInfo.split("\\|");
    hosts = s[1].split(",");
    endDate = new SimpleDateFormat("yyyy-MM-dd").parse(s[2]);
    byte[] md5 = new byte[16];
    System.arraycopy(decrypted, outputOffset - 16, md5, 0, 16);
    licensePath = new String(Hex.encodeHex(md5));
}

From source file:com.cyanogenmod.account.util.CMAccountUtils.java

public static String encodeHex(byte[] bytes) {
    return new String(Hex.encodeHex(bytes));
}

From source file:com.yahoo.glimmer.indexing.preprocessor.ResourceRecordWriterTest.java

@Test
public void bySubjectsTest()
        throws IOException, InterruptedException, NoSuchAlgorithmException, BySubjectRecordException {
    FSDataOutputStream bySubjectOs = new FSDataOutputStream(
            new FileOutputStream(new File(tempDirPath.toUri().getPath(), "bySubject.bz2")), null);
    FSDataOutputStream bySubjectOffsetsOs = new FSDataOutputStream(
            new FileOutputStream(new File(tempDirPath.toUri().getPath(), "bySubject.blockOffsets")), null);

    e.one(fs).create(e.with(new Path(tempDirPath, "bySubject.bz2")), e.with(false));
    e.will(Expectations.returnValue(bySubjectOs));
    e.one(fs).create(e.with(new Path(tempDirPath, "bySubject.blockOffsets")), e.with(false));
    e.will(Expectations.returnValue(bySubjectOffsetsOs));

    e.allowing(subjectOs).write(e.with(new ByteMatcher()), e.with(0), e.with(Expectations.any(Integer.class)));

    e.allowing(allOs).write(e.with(new ByteMatcher("all\nall\n", true)), e.with(0),
            e.with(Expectations.any(Integer.class)));

    context.checking(e);/*from www  .java2  s  .c om*/
    System.out.println("tempDirPath:" + tempDirPath);
    ResourceRecordWriter writer = new ResourceRecordWriter(fs, tempDirPath, null);

    BySubjectRecord record = new BySubjectRecord();
    Random random = new Random();
    for (long l = 100000; l < 200000; l += (random.nextInt(19) + 2)) {
        record.setId(l);
        record.setSubject("Subject:" + Integer.toString(random.nextInt()));
        for (int i = 0; i < random.nextInt() % 100; i++) {
            record.addRelation("a relation " + Long.toString(random.nextLong()));
        }

        writer.write(null, record);

        record.setPreviousId(l);
        record.clearRelations();
    }

    BySubjectRecord beforeBigRecord = new BySubjectRecord();
    beforeBigRecord.setId(200200l);
    beforeBigRecord.setPreviousId(record.getId());
    beforeBigRecord.setSubject("Before Big Test Record");
    writer.write(null, beforeBigRecord);

    // Write a big record that will span multiple blocks of 100000 bytes.
    BySubjectRecord bigRecord = new BySubjectRecord();
    bigRecord.setId(200201l);
    bigRecord.setPreviousId(beforeBigRecord.getId());
    bigRecord.setSubject("Big Test Record");

    MessageDigest md5Digest = MessageDigest.getInstance("MD5");
    StringBuilder sb = new StringBuilder();
    // 8k x 128 byte relations.  The relation here is just a 128 byte hex string without delimiters.
    for (int i = 0; i < 8192; i++) {
        md5Digest.update((byte) ((i * 1299299) & 0xFF));
        byte[] digest = md5Digest.digest();
        sb.append(Hex.encodeHex(digest));

        md5Digest.update(digest);
        digest = md5Digest.digest();
        sb.append(Hex.encodeHex(digest));

        md5Digest.update(digest);
        digest = md5Digest.digest();
        sb.append(Hex.encodeHex(digest));

        md5Digest.update(digest);
        digest = md5Digest.digest();
        sb.append(Hex.encodeHex(digest));

        bigRecord.addRelation(sb.toString());
        sb.setLength(0);
    }

    writer.write(null, bigRecord);

    BySubjectRecord afterBigRecord = new BySubjectRecord();
    afterBigRecord.setId(200202l);
    afterBigRecord.setPreviousId(bigRecord.getId());
    afterBigRecord.setSubject("After Big Test Record");
    writer.write(null, afterBigRecord);

    OutputCount outputCount = new OutputCount();
    outputCount.output = OUTPUT.ALL;
    outputCount.count = 1;
    Text key = new Text("all");
    for (int i = 0; i < 200205; i++) {
        writer.write(key, outputCount);
    }
    writer.write(new Text("http://a/key1"), outputCount);

    writer.close(null);

    BlockCompressedDocumentCollection collection = new BlockCompressedDocumentCollection("bySubject", null, 10);
    String indexBaseName = new File(tempDirPath.toUri().getPath(), "bySubject").getCanonicalPath();
    collection.filename(indexBaseName);

    assertEquals(-1, collection.stream(99999).read());

    InputStream documentInputStream = collection.stream(100000);
    record.readFrom(new InputStreamReader(documentInputStream));
    assertEquals(100000, record.getId());

    documentInputStream = collection.stream(record.getId());
    record.readFrom(new InputStreamReader(documentInputStream));
    assertEquals(record.getId(), record.getId());

    record.setPreviousId(3);
    record.setSubject(null);
    documentInputStream = collection.stream(record.getId() + 1);
    assertEquals(-1, documentInputStream.read());

    documentInputStream = collection.stream(beforeBigRecord.getId());
    record.readFrom(new InputStreamReader(documentInputStream));
    assertEquals(beforeBigRecord, record);

    documentInputStream = collection.stream(afterBigRecord.getId());
    record.readFrom(new InputStreamReader(documentInputStream));
    assertEquals(afterBigRecord, record);

    documentInputStream = collection.stream(bigRecord.getId());
    record.readFrom(new InputStreamReader(documentInputStream));
    System.out.println("BigRecord Relation count:" + bigRecord.getRelationsCount());
    System.out.println("First:" + bigRecord.getRelation(0));
    System.out.println("Last:" + bigRecord.getRelation(bigRecord.getRelationsCount() - 1));
    System.out.println("Record Relation count:" + record.getRelationsCount());
    System.out.println("First:" + record.getRelation(0));
    System.out.println("Last:" + record.getRelation(record.getRelationsCount() - 1));

    int limit = bigRecord.getRelationsCount() > record.getRelationsCount() ? record.getRelationsCount()
            : bigRecord.getRelationsCount();
    for (int i = 0; i < limit; i++) {
        assertEquals("At index " + i, bigRecord.getRelation(i), record.getRelation(i));
    }

    assertEquals(bigRecord.getRelationsCount(), record.getRelationsCount());
    assertEquals(bigRecord, record);

    assertEquals(-1, collection.stream(afterBigRecord.getId() + 1).read());

    collection.close();
}

From source file:com.smartsheet.api.internal.http.RequestAndResponseData.java

public static String getContentAsText(HttpEntitySnapshot entity) throws IOException {
    if (entity == null) {
        return "";
    }//from   ww w .ja va2s.  co  m
    byte[] contentBytes = entity.getContentArray();
    String contentAsText;
    try {
        contentAsText = new String(contentBytes, "UTF-8");
    } catch (UnsupportedEncodingException badEncodingOrNotText) {
        contentAsText = new String(Hex.encodeHex(contentBytes));
    }
    return contentAsText;
}

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

@Override
public boolean canDecodeInput(BufferedInputStream stream) throws IOException {
    if (stream == null) {
        throw new IllegalArgumentException("file == null!");
    }//from   ww w . ja  v a 2s  . c om

    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 =>" + pormarks + "<-");
    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:com.jpeterson.util.etag.FileETagTest.java

/**
 * Test the calculate method when using file size.
 *///from  w  w w.  java2s .c o m
public void test_calculateSize() {
    File file;
    String content = "Hello, world!";
    String expected;
    FileETag etag;

    try {
        // create temporary file
        file = File.createTempFile("temp", "txt");

        // make sure that it gets cleaned up
        file.deleteOnExit();

        // put some date in the file
        FileOutputStream out = new FileOutputStream(file);
        out.write(content.getBytes());
        out.flush();
        out.close();

        // manipulate the last modified value to a "known" value
        SimpleDateFormat date = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        long lastModified = date.parse("06/21/2007 11:19:36").getTime();
        file.setLastModified(lastModified);

        // determined expected
        StringBuffer buffer = new StringBuffer();
        buffer.append(content.length());
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        expected = new String(Hex.encodeHex(messageDigest.digest(buffer.toString().getBytes())));

        etag = new FileETag();
        etag.setFlags(FileETag.FLAG_SIZE);
        String value = etag.calculate(file);

        assertEquals("Unexpected value", expected, value);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        fail("Unexpected exception");
    } catch (IOException e) {
        e.printStackTrace();
        fail("Unexpected exception");
    } catch (ParseException e) {
        e.printStackTrace();
        fail("Unexpected exception");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        fail("Unexpected exception");
    }
}