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

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

Introduction

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

Prototype

public static String encodeHexString(byte[] data) 

Source Link

Document

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

Usage

From source file:com.thoughtworks.go.agent.launcher.ServerBinaryDownloaderTest.java

@Test
public void shouldSetMd5AndSSLPortHeaders() throws Exception {
    ServerBinaryDownloader downloader = new ServerBinaryDownloader(
            ServerUrlGeneratorMother.generatorFor("localhost", server.getPort()), null,
            SslVerificationMode.NONE);// w w w  . jav a 2  s  . co  m
    downloader.downloadIfNecessary(DownloadableFile.AGENT);

    MessageDigest digester = MessageDigest.getInstance("MD5");
    try (BufferedInputStream stream = new BufferedInputStream(
            new FileInputStream(DownloadableFile.AGENT.getLocalFile()))) {
        try (DigestInputStream digest = new DigestInputStream(stream, digester)) {
            IOUtils.copy(digest, new NullOutputStream());
        }
        assertThat(downloader.getMd5(), is(Hex.encodeHexString(digester.digest()).toLowerCase()));
    }
}

From source file:com.streamsets.lib.security.http.TestPasswordHasher.java

@Test
public void testPasswordHashDefault() throws Exception {
    Configuration configuration = new Configuration();
    configuration.set(PasswordHasher.ITERATIONS_KEY, 1);
    PasswordHasher hasher = new PasswordHasher(configuration);
    String currentVersion = hasher.getCurrentVersion();

    String passwordHash = hasher.getPasswordHash("user", "foo");
    Assert.assertEquals(hasher.getCurrentVersion(), hasher.getHashVersion(passwordHash));

    Assert.assertTrue(passwordHash.startsWith(currentVersion + ":" + hasher.getIterations() + ":"));
    String[] parts = passwordHash.split(":");
    Assert.assertEquals(4, parts.length);

    int iterations = Integer.parseInt(parts[1]);
    byte[] salt = Hex.decodeHex(parts[2].toCharArray());

    PBEKeySpec spec = new PBEKeySpec(hasher.getValueToHash(currentVersion, "user", "foo").toCharArray(), salt,
            iterations, hasher.getKeyLength());
    byte[] hash = PasswordHasher.SECRET_KEY_FACTORIES.get(hasher.getCurrentVersion()).generateSecret(spec)
            .getEncoded();//ww w.j  a v  a 2  s. co m
    String hashHex = Hex.encodeHexString(hash);
    Assert.assertEquals(parts[3], hashHex);

    //valid u/p
    Assert.assertTrue(hasher.verify(passwordHash, "user", "foo"));

    // invalid u valid p, V2 catches this
    Assert.assertFalse(hasher.verify(passwordHash, "userx", "foo"));

    // invalid p
    Assert.assertFalse(hasher.verify(passwordHash, "user", "bar"));
}

From source file:es.eucm.eadventure.editor.plugin.vignette.VignetteCharacterPreview.java

private void updateBytes() {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {/*from   w w  w .  ja  v a  2  s .co  m*/
        ImageIO.write(image, "png", baos);
        baos.close();
        imageBytes = baos.toByteArray();
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digest = md.digest(imageBytes);
        imageName = Hex.encodeHexString(digest) + ".png";
        System.err.println("Wrote " + imageBytes + " bytes to " + imageName);
    } catch (Exception e) {
        System.err.println("Error encoding & md5ing:");
        e.printStackTrace();
    }
}

From source file:com.g414.dgen.field.Fields.java

/**
 * returns some random Hex bytes : note, length is of "original" binary
 * bytes/*  w w  w .  java  2s . c  o  m*/
 */
public static Field<String> getRandomHexBytesField(final String name, final int length) {
    return new Field<String>() {
        @Override
        public String getName() {
            return name;
        }

        @Override
        public String getValue(String entityId, Map<String, Object> entitySoFar, Random random) {
            byte[] next = new byte[length];
            random.nextBytes(next);

            return Hex.encodeHexString(next);
        }
    };
}

From source file:eu.europa.esig.dss.validation.OCSPRef.java

/**
 * @param ocspResp//from   ww  w . jav a  2 s  . c  o  m
 * @return
 */
public boolean match(final BasicOCSPResp ocspResp) {

    if (digestAlgorithm == null) { // -444
        return false;
    }
    try {

        MessageDigest digest = DSSUtils.getMessageDigest(digestAlgorithm);
        if (matchOnlyBasicOCSPResponse) {
            digest.update(ocspResp.getEncoded());
        } else {
            digest.update(DSSRevocationUtils.fromBasicToResp(ocspResp).getEncoded());
        }
        byte[] computedValue = digest.digest();
        if (LOG.isInfoEnabled()) {
            LOG.info("Compare " + Hex.encodeHexString(digestValue) + " to computed value "
                    + Hex.encodeHexString(computedValue) + " of " + "BasicOCSPResp produced at "
                    + ocspResp.getProducedAt());
        }
        return Arrays.equals(digestValue, computedValue);
    } catch (IOException e) {
        throw new DSSException(e);
    }
}

From source file:net.solarnetwork.node.rfxcom.test.SetModeMessageTest.java

@Test
public void encodeSetModeCommand3() {
    SetModeMessage msg = new SetModeMessage((short) 11, TransceiverType.Type43392a);
    msg.setACEnabled(true);/* w  w w  . j  a v a  2 s .c  om*/
    msg.setFS20Enabled(true);
    final byte[] packet = msg.getMessagePacket();
    log.debug("Got packet: " + Hex.encodeHexString(packet));
    assertNotNull("Packet", packet);
    assertArrayEquals(TestUtils.bytesFromHexString("0D 00 00 0B 03 53 00 00 10 04 00 00 00 00"), packet);
}

From source file:cherry.goods.crypto.AESCryptoTest.java

@Test
public void testRandomizing() {
    int size = 10000;
    for (int i = 0; i < 10; i++) {

        byte[] key = RandomUtils.nextBytes(16);
        byte[] plain = RandomUtils.nextBytes(1024);
        AESCrypto crypto = new AESCrypto();
        crypto.setSecretKeyBytes(key);/*from www.  j  av  a  2 s.  c  o m*/

        Set<String> set = new HashSet<>();
        for (int j = 0; j < size; j++) {
            byte[] enc = crypto.encrypt(plain);
            set.add(Hex.encodeHexString(enc));
        }
        assertThat(set.size(), is(size));
    }
}

From source file:de.resol.vbus.DatagramTest.java

@Test
public void testToLiveBuffer() throws Exception {
    String refValue1 = "aa362335332034433353300332630851";

    Datagram testDgram1 = new Datagram(0, 0x13, 0x2336, 0x3335, 0x4334, 0x5333, 0x63328330);
    byte[] testBuffer1 = testDgram1.toLiveBuffer(null, 0, 0);
    assertEquals(refValue1, Hex.encodeHexString(testBuffer1));

    byte[] testBuffer2 = new byte[testBuffer1.length + 2];
    assertEquals(testBuffer2, testDgram1.toLiveBuffer(testBuffer2, 1, testBuffer2.length - 1));
    assertEquals("00" + refValue1 + "00", Hex.encodeHexString(testBuffer2));

    assertEquals(null, testDgram1.toLiveBuffer(testBuffer2, 3, testBuffer2.length - 3));
}

From source file:com.kuzumeji.platform.standard.SecurityServiceTest.java

private static String dumpKeyPair(final Key key) {
    return MessageFormat.format("?:{0} ?:{1} ?:{2}", key.getAlgorithm(), key.getFormat(),
            Hex.encodeHexString(key.getEncoded()));
}

From source file:cn.guoyukun.spring.jpa.repository.hibernate.type.ObjectSerializeUserType.java

/**
 * Hibernate??//from   w w w .j  a  v  a 2  s  .c  o m
 * ?PreparedStateme??
 */
@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session)
        throws HibernateException, SQLException {
    ObjectOutputStream oos = null;
    if (value == null) {
        st.setNull(index, Types.VARCHAR);
    } else {
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(value);
            oos.close();

            byte[] objectBytes = bos.toByteArray();
            String hexStr = Hex.encodeHexString(objectBytes);

            st.setString(index, hexStr);
        } catch (Exception e) {
            throw new HibernateException(e);
        } finally {
            try {
                oos.close();
            } catch (IOException e) {
            }
        }
    }
}