List of usage examples for org.apache.commons.codec.binary Hex encode
public Object encode(Object object) throws ClassCastException, UnsupportedEncodingException
From source file:corelyzer.util.StringUtility.java
public static String getSHASum(final String aString) { String hash;/*from ww w . ja v a 2 s . c o m*/ try { MessageDigest md = MessageDigest.getInstance("SHA"); md.update(aString.getBytes()); Hex hex = new Hex(); hash = new String(hex.encode(md.digest())); } catch (NoSuchAlgorithmException e) { System.err.println("---> [Error] NoSuchAlgorithmException!"); hash = "no-sha"; } return hash; }
From source file:fedorax.server.module.storage.lowlevel.irods.IrodsIFileSystem.java
private static final CopyResult stream2streamCopy(InputStream in, OutputStream out) throws IOException { int BUFFER_SIZE = 8192; LOG.debug("IrodsIFileSystem.stream2streamCopy() start"); CopyResult result = new CopyResult(); byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead = 0; try {/*from w ww . java 2 s .c o m*/ MessageDigest messageDigest; try { messageDigest = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw new IOException("Cannot compare checksums without MD5 algorithm.", e); } messageDigest.reset(); while ((bytesRead = in.read(buffer, 0, BUFFER_SIZE)) != -1) { messageDigest.update(buffer, 0, bytesRead); result.size = result.size + bytesRead; // for transport failure test add the following line: // buffer[0] = '!'; out.write(buffer, 0, bytesRead); } out.flush(); Hex hex = new Hex(); result.md5 = new String(hex.encode(messageDigest.digest())); } catch (IOException e) { LOG.error("Unexpected", e); throw e; } finally { try { out.close(); } catch (IOException e) { LOG.warn("Exception while trying to close jargon output stream", e); } } LOG.debug("IrodsIFileSystem.stream2streamCopy() end"); return result; }
From source file:com.cloudant.sync.query.IndexCreator.java
/** * Iterate candidate indexNames generated from the indexNameRandom generator * until we find one which doesn't already exist. * * We make sure the generated name is not an index already by the list * of index names returned by {@link #listIndexesInDatabaseQueue()} method. * This is because we avoid knowing about how indexes are stored in SQLite, however * this means that it is not thread safe, it is possible for a new index with the same * name to be created after a copy of the indexes has been taken from the database. * * We allow up to 200 random name generations, which should give us many millions * of indexes before a name fails to be generated and makes sure this method doesn't * loop forever./* w w w .j a va 2 s . c o m*/ * * @param existingIndexNames The names of the indexes that exist in the database. * * @return The generated index name or {@code null} if it failed. * */ private static String generateIndexName(Set<String> existingIndexNames) throws ExecutionException, InterruptedException { String indexName = null; Hex hex = new Hex(); int tries = 0; byte[] randomBytes = new byte[20]; while (tries < 200 && indexName == null) { indexNameRandom.nextBytes(randomBytes); String candidate = new String(hex.encode(randomBytes), Charset.forName("UTF-8")); if (!existingIndexNames.contains(candidate)) { indexName = candidate; } tries++; } if (indexName != null) { return indexName; } else { return null; } }
From source file:edu.ur.file.checksum.Md5ChecksumCalculator.java
/** * Calculate the md5 checksum of the specified file. * //from www .j ava 2 s.c o m * @see edu.ur.file.db.ChecksumCalculator#calculate(java.io.File) */ public String calculate(File f) { String checksum = null; try { InputStream fis = new FileInputStream(f); byte[] buffer = new byte[1024]; MessageDigest messageDigest = MessageDigest.getInstance("MD5"); int numRead; do { numRead = fis.read(buffer); if (numRead > 0) { messageDigest.update(buffer, 0, numRead); } } while (numRead != -1); fis.close(); byte[] value = messageDigest.digest(); Hex hex = new Hex(); checksum = new String(hex.encode(value)); } catch (Exception e) { throw new RuntimeException(e); } return checksum; }
From source file:edu.unc.lib.dl.util.Checksum.java
/** * Get the checksum for the passed in byte array * //from w ww . j a v a 2s.co m * @param byteArray * must not be null * @return byte array containing checksum */ public String getChecksum(byte[] byteArray) { messageDigest.reset(); messageDigest.update(byteArray); Hex hex = new Hex(); return new String(hex.encode(messageDigest.digest())); }
From source file:edu.unc.lib.dl.util.Checksum.java
/** * Get the checksum for the passed in byte array * /* w w w .j ava2s .c o m*/ * @param byteArray * must not be null * @return byte array containing checksum */ public String getChecksum(InputStream in) throws IOException { messageDigest.reset(); try (BufferedInputStream bis = new BufferedInputStream(in, 1024)) { byte[] buffer = new byte[1024]; int numRead; do { numRead = bis.read(buffer); if (numRead > 0) { messageDigest.update(buffer, 0, numRead); } } while (numRead != -1); } Hex hex = new Hex(); return new String(hex.encode(messageDigest.digest())); }
From source file:dk.hlyh.hudson.plugins.mavenrepo.repo.ChecksumFile.java
@Override public void sendResponse(StaplerRequest req, StaplerResponse rsp) throws IOException { FileInputStream fis = new FileInputStream(filename); try {//www . j ava 2s. c o m MessageDigest messageDigest = MessageDigest.getInstance(algorithm); messageDigest.reset(); // Stream the file contents to the MessageDigest. byte[] buffer = new byte[STREAMING_BUFFER_SIZE]; int size = fis.read(buffer, 0, STREAMING_BUFFER_SIZE); while (size >= 0) { messageDigest.update(buffer, 0, size); size = fis.read(buffer, 0, STREAMING_BUFFER_SIZE); } Hex encoder = new Hex(); byte[] encodeded = encoder.encode(messageDigest.digest()); rsp.setContentType("text/plain"); rsp.getOutputStream().write(encodeded); } catch (NoSuchAlgorithmException ex) { log.warn("Could not generate '" + algorithm + "' checksum for: " + filename, ex); rsp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } finally { try { fis.close(); } catch (IOException e) { /* ignored */ } } }
From source file:com.lightboxtechnologies.spectrum.HexWritable.java
@Override public String toString() { final Hex hex = new Hex(); final byte[] bytes = new byte[getLength()]; System.arraycopy(getBytes(), 0, bytes, 0, bytes.length); return new String(hex.encode(bytes)); }
From source file:co.cask.hydrator.plugin.EncoderTest.java
@Test public void testHEXEncoder() throws Exception { String test = "This is a test for testing hex encoding"; Transform<StructuredRecord, StructuredRecord> transform = new Encoder( new Encoder.Config("a:HEX", OUTPUT.toString())); transform.initialize(null);// w w w. j a v a 2s . com MockEmitter<StructuredRecord> emitter = new MockEmitter<>(); transform.transform(StructuredRecord.builder(INPUT).set("a", test).set("b", "2").set("c", "3").set("d", "4") .set("e", "5").build(), emitter); Hex hex = new Hex(); byte[] expected = hex.encode(test.getBytes("UTF-8")); byte[] actual = emitter.getEmitted().get(0).get("a"); Assert.assertEquals(2, emitter.getEmitted().get(0).getSchema().getFields().size()); Assert.assertArrayEquals(expected, actual); }
From source file:co.cask.hydrator.plugin.DecoderTest.java
@Test public void testHexDecoder() throws Exception { String test = "This is a test for testing hex decoding"; Transform<StructuredRecord, StructuredRecord> encoder = new Encoder( new Encoder.Config("a:HEX", OUTPUT.toString())); encoder.initialize(null);//w ww. j ava 2s.c o m MockEmitter<StructuredRecord> emitterEncoded = new MockEmitter<>(); encoder.transform(StructuredRecord.builder(INPUT).set("a", test).set("b", "2").set("c", "3").set("d", "4") .set("e", "5").build(), emitterEncoded); Hex hex = new Hex(); byte[] expected = hex.encode(test.getBytes("UTF-8")); byte[] actual = emitterEncoded.getEmitted().get(0).get("a"); Assert.assertEquals(2, emitterEncoded.getEmitted().get(0).getSchema().getFields().size()); Assert.assertArrayEquals(expected, actual); Transform<StructuredRecord, StructuredRecord> decoder = new Decoder( new Decoder.Config("a:HEX", OUTPUTSTR.toString())); decoder.initialize(null); MockEmitter<StructuredRecord> emitterDecoded = new MockEmitter<>(); decoder.transform(emitterEncoded.getEmitted().get(0), emitterDecoded); Assert.assertEquals(2, emitterDecoded.getEmitted().get(0).getSchema().getFields().size()); Assert.assertEquals(test, emitterDecoded.getEmitted().get(0).get("a")); }