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:me.lachlanap.summis.downloader.VerifierCallable.java

@Override
public Void call() throws Exception {
    Path file = binaryRoot.resolve(info.getName());
    MessageDigest md5 = MessageDigest.getInstance("MD5");
    MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
    byte[] buffer = new byte[1024];
    try (final InputStream is = Files.newInputStream(file)) {
        DigestInputStream dis = new DigestInputStream(new DigestInputStream(is, md5), sha1);
        while (dis.read(buffer) != -1) {
            ;//from w  w  w.ja  v a2 s  .c om
        }
    }
    String md5Digest = new String(Hex.encodeHex(md5.digest()));
    String sha1Digest = new String(Hex.encodeHex(sha1.digest()));
    if (!md5Digest.equals(info.getMD5Digest()) || !sha1Digest.equals(info.getSHA1Digest()))
        throw new RuntimeException(info.getName() + " failed verification");
    downloadListener.completedAVerify();
    return null;
}

From source file:fr.rjoakim.android.jonetouch.service.UserService.java

public String encodePassword(String email, String password) {
    try {//from   w  w w .j a  v a  2s .co  m
        String str = email + password;
        MessageDigest messageDigest = java.security.MessageDigest.getInstance("SHA-256");
        return new String(Hex.encodeHex(messageDigest.digest(str.getBytes())));
    } catch (NoSuchAlgorithmException e) {
        return null;
    }
}

From source file:fr.eoit.util.dumper.DumperTest.java

@Test
public void testCompression() {

    byte[] strBytes = COPYRIGHTS.getBytes();

    byte[] output = new byte[8096];
    Deflater compresser = new Deflater(Deflater.BEST_COMPRESSION, true);
    compresser.setInput(strBytes);/*w  w w.ja va 2  s .  com*/
    compresser.finish();
    int compressedDataLength = compresser.deflate(output);
    compresser.end();

    String inputString = new String(Hex.encodeHex(strBytes));
    String hexString = new String(Arrays.copyOf(output, compressedDataLength));

    int i = 0;
    i++;
}

From source file:net.padlocksoftware.padlock.tools.KeyMaker.java

private String getJavaCode() {
    String[] lines = formatOutput(new String(Hex.encodeHex(pair.getPublic().getEncoded())));
    StringBuilder builder = new StringBuilder();
    for (int x = 0; x < lines.length; x++) {
        String line = lines[x];/* w w w.  j ava 2 s .  c o  m*/
        if (x == 0) {
            // First line
            builder.append("\t private static final String publicKey = \n\t\t\"" + line + "\" + \n");
        } else if (x == lines.length - 1) {
            // Last line
            builder.append("\t\t\"" + line + "\";\n\n");
        } else {
            builder.append("\t\t\"" + line + "\" + \n");
        }
    }
    return builder.toString();
}

From source file:com.tc.simple.apn.factories.FeedbackFactory.java

public List<Feedback> createFeedback(InputStream in) {
    List<Feedback> list = new ArrayList<Feedback>();
    try {/*ww w.ja va2s.co m*/

        byte[] byteMe = IOUtils.toByteArray(in);

        int startRange = 0;

        int endRange = 38; //38 byte chunks per feedback item.

        while (startRange < byteMe.length) {

            //init the value object to hold the feedback data.
            Feedback feedback = new Feedback();

            //build the item based on range
            byte[] item = Arrays.copyOfRange(byteMe, startRange, endRange);//38 byte chunks.

            byte[] date = Arrays.copyOfRange(item, 0, 4);

            byte[] size = Arrays.copyOfRange(item, 4, 6);

            byte[] token = Arrays.copyOfRange(item, 6, item.length);

            ByteBuffer dateWrap = ByteBuffer.wrap(date);

            ByteBuffer javaSize = ByteBuffer.wrap(size);

            //set the date (returns number of seconds from unix epoch date)
            feedback.setDate(new Date(dateWrap.getInt() * 1000L));

            //get the size of the token (should always be 32)
            feedback.setSize(javaSize.getShort());

            //drop in our encoded token (will be used as our key for marking the user's token doc as failed)
            feedback.setToken(String.valueOf(Hex.encodeHex(token)));

            //add it to our collection
            list.add(feedback);

            //increment the start range
            startRange = startRange + 38;

            //increment the end range.
            endRange = endRange + 38;

        }

    } catch (Exception e) {
        logger.log(Level.SEVERE, null, e);

    }

    return list;

}

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

@Test
public void testShouldWorkWithRKFRawLayout() throws Exception {
    final TableLayoutDesc desc = FijiTableLayouts.getLayout(FijiTableLayouts.COUNTER_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());
}

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

/**
 * Export the supplied Keypair to an output Stream.
 *
 * @param pair The KeyPair to export.  KeyPairs should only be pairs
 * created with the createKeyPair(int) method.
 *
 * @param stream The stream to write the KeyPair to.  Key streams contain both the
 * public and private keys and should be secured.
 *
 * @throws java.io.IOException For any Stream IO related exceptions
 * @throws java.lang.NullPointerException If either parameter is null
 * @since 2.0//  ww  w. jav  a2  s .c  o  m
 */
public static void exportKeyPair(KeyPair pair, OutputStream stream) throws IOException {
    if (pair == null) {
        throw new IllegalArgumentException("KeyPair may not be null");
    }

    if (stream == null) {
        throw new IllegalArgumentException("Stream may not be null");
    }

    //
    // Turn the keypair into properties
    //
    Properties p = new Properties();

    String pri = new String(Hex.encodeHex(pair.getPrivate().getEncoded()));
    String pub = new String(Hex.encodeHex((pair.getPublic().getEncoded())));
    p.setProperty("public", pub);
    p.setProperty("private", pri);

    p.store(stream, null);
    stream.flush();
    stream.close();

}

From source file:com.shenit.commons.codec.DesUtils.java

/**
 * ?//from   w ww. j  a v a  2 s  . com
 * @param codec
 * @param src
 * @param key
 * @param secure
 * @return
 */
public static String encryptHex(String codec, String src, String key) {
    try {
        return new String(
                Hex.encodeHex(encrypt(src.getBytes(HttpUtils.ENC_UTF8), key.getBytes(HttpUtils.ENC_UTF8))));
    } catch (UnsupportedEncodingException e) {
        if (LOG.isWarnEnabled())
            LOG.warn("[encryptHex] Could not encrypt to hex due to exception", e);
    }
    return null;
}

From source file:com.amazonaws.cognito.devauthsample.identity.AWSCognitoDeveloperAuthenticationSample.java

private static String generateRandomString() {
    byte[] randomBytes = new byte[16];
    RANDOM.nextBytes(randomBytes);/*from   ww  w .  j a  v a 2s .c om*/
    String randomString = new String(Hex.encodeHex(randomBytes));
    return randomString;
}

From source file:com.qut.middleware.esoemanager.util.PolicyIDGenerator.java

/**
 * Generates the specified number of random bytes using SecureRandom
 * //  w w w.j av a2s  . co  m
 * @param length
 *            The number of random bytes to generate
 * @return The generated random string
 */
private String generate(int length) {
    String id;
    byte[] buf;
    buf = new byte[length];

    this.lock.lock();
    try {
        /* Seed the specified RNG instance and get bytes */
        this.random.setSeed(Thread.currentThread().getName().getBytes());
        this.random.setSeed(System.currentTimeMillis());
        this.random.nextBytes(buf);
    } finally {
        this.lock.unlock();
    }

    id = new String(Hex.encodeHex(buf));

    return id;
}