List of usage examples for org.apache.commons.codec.binary Hex encodeHex
public static char[] encodeHex(byte[] data)
From source file:cascading.tap.hadoop.FSDigestInputStream.java
@Override public void close() throws IOException { inputStream.close();/*from w w w . j ava2 s .c om*/ LOG.info("closing stream, testing digest: [" + (digestHex == null ? "none" : digestHex) + "]"); if (digestHex == null) return; String digestHex = new String(Hex.encodeHex(((DigestInputStream) inputStream).getMessageDigest().digest())); if (!digestHex.equals(this.digestHex)) { String message = "given digest: [" + this.digestHex + "], does not match input stream digest: [" + digestHex + "]"; LOG.error(message); throw new IOException(message); } }
From source file:com.jpeterson.util.etag.FileETagTest.java
/** * Test the calculate method when using file last modified, file size, and * file content values.//from ww w. j ava2 s. co m */ public void test_calculateLastModifiedSizeContent() { 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 MessageDigest messageDigest = MessageDigest.getInstance("MD5"); messageDigest.update(content.getBytes()); StringBuffer buffer = new StringBuffer(); buffer.append(lastModified); buffer.append(content.length()); expected = new String(Hex.encodeHex(messageDigest.digest(buffer.toString().getBytes()))); etag = new FileETag(); etag.setFlags(FileETag.FLAG_CONTENT | FileETag.FLAG_MTIME | 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"); } }
From source file:com.moz.fiji.schema.util.TestJsonEntityIdParser.java
@Test public void testShouldWorkWithRKFHashedLayout() throws Exception { final TableLayoutDesc desc = FijiTableLayouts.getLayout(FijiTableLayouts.FOODS); final FijiTableLayout layout = FijiTableLayout.newLayout(desc); final EntityIdFactory factory = EntityIdFactory.getFactory(layout); // Byte array representations of row keys should work. 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()); // Component representation of entities should work. final JsonEntityIdParser restEid4 = JsonEntityIdParser.create(SINGLE_COMPONENT_EID, layout); final EntityId resolvedEid = restEid4.getEntityId(); final String recoveredComponent = Bytes.toString(Bytes .toBytesBinary(resolvedEid.toShellString().substring(ToolUtils.FIJI_ROW_KEY_SPEC_PREFIX.length()))); assertEquals(UNUSUAL_STRING_EID, recoveredComponent); assertEquals(resolvedEid, restEid4.getEntityId()); }
From source file:edu.umn.msi.tropix.common.io.IOUtilsImpl.java
public boolean isZippedStream(final InputStream inputStream) { final byte[] firstBytes = new byte[2]; try {/*from w ww . j ava 2 s . c om*/ final int numBytes = inputStream.read(firstBytes); if (numBytes < 2) { return false; } // I hex dumped a bunch of zip files and they all started with 4b50. // This is purely based on my observations. If at some point this stops // working, it will need to be fixed. This approach will yield some false // positives, so it shouldn't be considered a general use procedure. final String hexString = new String(Hex.encodeHex(firstBytes)); // String hexString = new String(HexUtils.encode(firstBytes)); return hexString.equals("504b"); } catch (final IOException e) { throw new IORuntimeException(e); } }
From source file:main.java.com.amazonaws.cognito.devauthsample.Utilities.java
public static String generateRandomString() { byte[] randomBytes = new byte[16]; RANDOM.nextBytes(randomBytes);//from w w w . jav a 2 s. com String randomString = new String(Hex.encodeHex(randomBytes)); return randomString; }
From source file:io.moquette.spi.impl.security.DBAuthenticator.java
@Override public synchronized boolean checkValid(String clientId, String username, byte[] password) { // Check Username / Password in DB using sqlQuery if (username == null || password == null) { LOG.info("username or password was null"); return false; }/*w ww .j av a2 s .com*/ ResultSet r = null; try { this.preparedStatement.setString(1, username); r = this.preparedStatement.executeQuery(); if (r.next()) { final String foundPwq = r.getString(1); messageDigest.update(password); byte[] digest = messageDigest.digest(); String encodedPasswd = new String(Hex.encodeHex(digest)); return foundPwq.equals(encodedPasswd); } r.close(); } catch (SQLException e) { e.printStackTrace(); } return false; }
From source file:me.senwang.newpdademo.MainActivity.java
@Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement switch (id) { case R.id.action_next: setProgressBarIndeterminateVisibility(true); if (mIp == null) { // request(WdtRequestCopy.RequestUrl.REQUEST_IP, WdtRequestCopy.Param.getRequestIpParams(mSidEdit.getText().toString()), mGetIpListener); GetIpInterface getIpInterface = mGetIpRestAdapter.create(GetIpInterface.class); getIpInterface.getIp(mSidEdit.getText().toString(), mGetIpCallback); } else if (mLicense == null) { // request(WdtRequestCopy.RequestUrl.getLicenseUrl(mIp), null, mGetLicenseListener); mHostInterface.getLicense(mGetLicenseCallback); } else if (mSession == null) { String password = mPasswordEdit.getText().toString(); byte[] pwdMd5Bytes = WdtRequestCopy.Param.md5Bytes(password); String pwdMd5Str = new String(Hex.encodeHex(pwdMd5Bytes)); // request(WdtRequestCopy.RequestUrl.getLoginUrl(mIp), // WdtRequestCopy.Param.getLoginParams(mSidEdit.getText().toString(),mUserNameEdit.getText().toString(), mLicense, pwdMd5Str), // mLoginListener); mHostInterface.login(WdtRequestCopy.Param.getLoginParams(mSidEdit.getText().toString(), mUserNameEdit.getText().toString(), mLicense, pwdMd5Str), mLoginCallback); } else if (mTestStock == null) { // request(WdtRequestCopy.RequestUrl.getWarehousesUrl(mIp), WdtRequestCopy.Param.getWarehousesParams(), mWarehousesListener); // mHostInterface.getWarehouses(mGetWarehousesCallback); Map<String, String> testParams = new HashMap<>(); testParams.put("warehouse_no", "WH001"); testParams.put("spec_no", "penblack6"); // testParams.put("page_no", "1"); // testParams.put("page_size", "1"); mHostInterface.getStocks(testParams, mGetStocksCallback); // mTestInterface.testStocks(testParams, mTestCallBack); } else if (mFastPdResult == null) { Map<String, String> testParams = new HashMap<>(); testParams.put("warehouse_no", "WH001"); testParams.put("spec_no", "penblack6"); testParams.put("old_stock", String.valueOf(mTestStock.stockNum)); testParams.put("new_stock", String.valueOf(mTestStock.stockNum + 1)); mHostInterface.fastPd(testParams, mFastPdCallback); } else {//from ww w.j av a 2s . com setProgressBarIndeterminateVisibility(false); } return true; case R.id.action_reset: mIp = null; mLicense = null; mSession = null; mTextView.setText(""); return true; default: return super.onOptionsItemSelected(item); } }
From source file:com.bitctrl.net.MAC.java
@Override public String toString() { final char[] hex = Hex.encodeHex(address); String s = ""; for (int i = 0; i < hex.length; ++i) { s += hex[i];//from w w w.j av a2 s . c o m if (i % 2 == 1 && i < hex.length - 1) { // Nach jedem Byte (2 Hexziffern) Trenzeichen einfgen. s += ':'; } } return s; }
From source file:com.ikanow.infinit.e.api.authentication.PasswordEncryption.java
public static String md5checksum(String toHash) { try {/*ww w . j av a 2 s. com*/ MessageDigest m = MessageDigest.getInstance("MD5"); m.reset(); m.update(toHash.getBytes(Charset.forName("UTF8"))); byte[] digest = m.digest(); return new String(Hex.encodeHex(digest)); } catch (Exception ex) { return toHash; } }
From source file:edu.harvard.iq.dvn.ingest.statdataio.impl.plugins.dta.DTAFileReaderSpi.java
@Override public boolean canDecodeInput(Object source) throws IOException { if (!(source instanceof BufferedInputStream)) { return false; }/*from ww w . ja v a2 s .c o m*/ if (source == null) { throw new IllegalArgumentException("stream == null!"); } BufferedInputStream stream = (BufferedInputStream) source; dbgLog.fine("applying the dta test\n"); byte[] b = new byte[DTA_HEADER_SIZE]; if (stream.markSupported()) { stream.mark(0); } int nbytes = stream.read(b, 0, DTA_HEADER_SIZE); if (nbytes == 0) { throw new IOException(); } //printHexDump(b, "hex dump of the byte-array"); if (stream.markSupported()) { stream.reset(); } dbgLog.info("hex dump: 1st 4bytes =>" + new String(Hex.encodeHex(b)) + "<-"); if (b[2] != 1) { dbgLog.fine("3rd byte is not 1: given file is not stata-dta type"); return false; } else if ((b[1] != 1) && (b[1] != 2)) { dbgLog.fine("2nd byte is neither 0 nor 1: this file is not stata-dta type"); return false; } else if (!DTAFileReaderSpi.stataReleaseNumber.containsKey(b[0])) { dbgLog.fine("1st byte (" + b[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: " + DTAFileReaderSpi.stataReleaseNumber.get(b[0]) + "(No in byte=" + b[0] + ")"); return true; } }