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:info.magnolia.module.files.MD5CheckingFileExtractorOperation.java

protected String retrieveMD5(DigestInputStream md5Stream) {
    final byte[] digInBytes = md5Stream.getMessageDigest().digest();
    return String.valueOf(Hex.encodeHex(digInBytes));
}

From source file:com.beligum.core.utils.Toolkit.java

public static String hash(String password, String salt) {
    KeySpec spec = new PBEKeySpec(password.toCharArray(), salt.getBytes(), 2048, 160);
    try {//w w w .jav  a 2  s. c o m
        SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        byte[] hash = f.generateSecret(spec).getEncoded();
        return new String(Hex.encodeHex(hash));
    } catch (Exception e) {
        return null;
    }

}

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

/**
 * /* w  w w.  j  a  v  a2 s  . c  o m*/
 * <p />
 *  {@code publicKey}  {@code null} {@code plaintext}  {@code null}
 *  {@code null}
 * 
 * @param publicKey
 *            
 * @param plaintext
 *            
 * @return 
 */
public static String encryptString(PublicKey publicKey, String plaintext) {
    if (publicKey == null || plaintext == null) {
        return null;
    }
    byte[] data = plaintext.getBytes();
    try {
        byte[] en_data = encrypt(publicKey, data);
        return new String(Hex.encodeHex(en_data));
    } catch (Exception ex) {
        LOGGER.error(ex.getCause().getMessage());
    }
    return null;
}

From source file:eu.planets_project.tb.utils.ExperimentUtils.java

/**
 * Computes the MD5 hash of an input stream.
 * @param in The input stream to hash./*from ww  w.ja  v  a  2 s.c  om*/
 * @return The MD% hash, encoded as a hex string.
 */
public static String computeFixity(InputStream in) {
    MessageDigest md;
    try {
        md = MessageDigest.getInstance(FIXITY_ALG);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return "";
    }
    // Go through the input stream and digest.
    byte buf[] = new byte[8192];
    int n;
    try {
        while ((n = in.read(buf)) > 0) {
            md.update(buf, 0, n);
        }
    } catch (IOException e) {
        e.printStackTrace();
        return "";
    }
    byte hash[] = md.digest();
    return new String(Hex.encodeHex(hash));

}

From source file:edu.harvard.iq.dataverse.ingest.tabulardata.impl.plugins.dta.DTAFileReaderSpi.java

@Override
public boolean canDecodeInput(File file) throws IOException {
    if (file == null) {
        throw new IllegalArgumentException("file == null!");
    }//from w  w  w.  j a  v  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();

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

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

    dbgLog.fine("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:net.padlocksoftware.padlock.validator.ValidatorTest.java

License:asdf

@Test
public void testExpiredFloat() throws Exception {
    KeyPair pair = KeyManager.createKeyPair();
    License license = LicenseFactory.createLicense();
    license.setFloatingExpirationPeriod(1L);
    LicenseSigner signer = LicenseSigner.createLicenseSigner((DSAPrivateKey) pair.getPrivate());
    signer.sign(license);//from  ww w. j av a 2s  . c  o  m

    String key = new String(Hex.encodeHex(pair.getPublic().getEncoded()));
    Validator validator = new Validator(license, key);

    validator.validate();

    boolean ex = false;
    try {
        validator.validate();
    } catch (ValidatorException e) {
        ex = true;
    }
    assertTrue(ex);
}

From source file:de.innovationgate.webgate.api.jdbc.filehandling.CS5P4FileHandling.java

public ContentFileContent storeFileContents(CS41FileEntityDescriptor descriptor, InputStream in,
        Session session, MessageDigest digestMd5, MessageDigest digestSha512)
        throws IOException, InstantiationException, IllegalAccessException, WGAPIException {
    int partnr = 0;
    byte[] buffer = new byte[CS41FileAttachmentHandler.ATTACHMENT_FILEPART_SIZE];
    int len = in.read(buffer);
    int totalLen = 0;

    // Create file contents entity
    ContentFileContent fileContents = new ContentFileContent();
    fileContents.setOrdinalnr(getParent().incrementSystemSequence(ORDINAL_NUMBER_SEQUENCE_NAME));
    session.save(fileContents);//www  .j  a v  a 2  s  .  c o m

    while (len > 0) {
        totalLen += len;

        // create new file part
        AttachmentFilePart part = descriptor.createFilePart(fileContents);
        part.setPartnr(partnr);

        Blob data = Hibernate.getLobCreator(session).createBlob(new ByteArrayInputStream(buffer, 0, len), len);
        part.setData(data);

        // store file part
        session.save(part);
        session.flush();
        session.evict(part);

        // update digests
        digestMd5.update(buffer, 0, len);
        digestSha512.update(buffer, 0, len);

        // read next part from inputstream
        partnr++;
        len = in.read(buffer);

    }

    String checksumSha512 = new String(Hex.encodeHex(digestSha512.digest()));

    // Store file data on contents entity
    fileContents.setChecksumSha512(checksumSha512);
    fileContents.setSize(totalLen);
    if (getParent().isSaveIsolationActive()) {
        session.update(fileContents);
    }
    return fileContents;
}

From source file:de.longri.cachebox3.Utils.java

/**
 * Returns the MD5 hash from given fileHandle, or an empty String with any Exception
 *
 * @param fileHandle/*w  ww .j av a2s  .c  o  m*/
 * @return
 */
public static String getMd5(FileHandle fileHandle) {

    try {
        final MessageDigest md = MessageDigest.getInstance("MD5");
        final byte[] bytes = new byte[2048];
        int numBytes;
        InputStream inputStream = fileHandle.read();
        while ((numBytes = inputStream.read(bytes)) != -1) {
            md.update(bytes, 0, numBytes);
        }
        inputStream.close();
        return new String(Hex.encodeHex(md.digest()));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:com.impetus.client.crud.datatypes.ByteDataTest.java

/**
 * On insert pdf in cassandra blob object
 * /*ww w  .ja  v a  2  s  .c  om*/
 * @throws Exception
 *             the exception
 */
@Test
public void onInsertBlobPdfCassandra() throws Exception {

    try {

        FileInputStream fileInputStream = null;

        File file = new File("src/test/resources/persistence.pdf");

        byte[] bFile = new byte[(int) file.length()];

        PersonCassandra personWithKey = new PersonCassandra();
        personWithKey.setPersonId("111");
        // convert file into array of bytes
        fileInputStream = new FileInputStream(file);
        fileInputStream.read(bFile);
        fileInputStream.close();

        personWithKey.setA(bFile);
        em.persist(personWithKey);
        em.clear();

        String qry = "Select p from PersonCassandra p where p.personId = 111";
        Query q = em.createQuery(qry);
        List<PersonCassandra> persons = q.getResultList();
        PersonCassandra person = persons.get(0);

        // convert array of bytes into file
        FileOutputStream fileOuputStream = new FileOutputStream("src/test/resources/persistence-test.pdf");
        fileOuputStream.write(person.getA());
        fileOuputStream.close();

        Assert.assertNotNull(person.getA());
        Assert.assertEquals(new File("src/test/resources/persistence.pdf").getTotalSpace(),
                new File("src/test/resources/persistence-test.pdf").getTotalSpace());
        Assert.assertTrue(isFileBinaryEqual(new File("src/test/resources/persistence.pdf"),
                new File("src/test/resources/persistence-test.pdf")));
        Assert.assertEquals(String.valueOf(Hex.encodeHex((byte[]) bFile)),
                String.valueOf(Hex.encodeHex((byte[]) person.getA())));

    } catch (Exception e) {

    }

}

From source file:me.footlights.core.crypto.SecretKey.java

private SecretKey(SecretKeySpec key, Fingerprint fingerprint) {
    this.keySpec = key;
    this.fingerprint = fingerprint;

    String algorithm = keySpec.getAlgorithm();
    String keyData = new String(Hex.encodeHex(keySpec.getEncoded()));

    try {//from w w  w .jav  a  2 s .com
        this.uri = new URI(algorithm, keyData, null);
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException("Unable to generate symmetric key URI " + algorithm + ":" + keyData,
                e);
    }
}