List of usage examples for java.security DigestInputStream DigestInputStream
public DigestInputStream(InputStream stream, MessageDigest digest)
From source file:com.mobicage.rogerthat.plugins.messaging.MessagingPlugin.java
public boolean startUploadingFile(final File f, final String parentKey, final String messageKey, final String service, final long expectNext, final boolean deleteImageOnFinish, final String contentType) { T.UI();/*from ww w. jav a 2s. com*/ SharedPreferences options = PreferenceManager.getDefaultSharedPreferences(mMainService); boolean wifiOnlySettingEnabled = options.getBoolean(MainService.PREFERENCE_UPLOAD_PHOTO_WIFI, false); boolean willDirectlyStartTransferring = true; if (!mMainService.getNetworkConnectivityManager().isConnected()) { showTransferPendingNotification( mMainService.getString(R.string.transfer_pending_notification_no_network)); if (expectNext == 0) { UIUtils.showLongToast(mMainService, mMainService.getString(R.string.transfer_pending_notification_no_network)); } else { UIUtils.showLongToast(mMainService, mMainService.getString(R.string.transfer_pending_notification_no_network_followup)); } willDirectlyStartTransferring = false; } else if (wifiOnlySettingEnabled && !mMainService.getNetworkConnectivityManager().isWifiConnected()) { showTransferPendingNotification(mMainService.getString(R.string.transfer_pending_notification_no_wifi)); if (expectNext == 0) { UIUtils.showLongToast(mMainService, mMainService.getString(R.string.transfer_pending_notification_no_wifi)); } else { UIUtils.showLongToast(mMainService, mMainService.getString(R.string.transfer_pending_notification_no_wifi_followup)); } willDirectlyStartTransferring = false; } mMainService.postOnBIZZHandler(new SafeRunnable() { @SuppressWarnings("null") @Override protected void safeRun() throws Exception { FileInputStream in = new FileInputStream(f); try { byte[] buffer = new byte[MAX_CHUNK_SIZE]; MessageDigest complete = MessageDigest.getInstance("SHA-256"); DigestInputStream dis = new DigestInputStream(in, complete); try { ZipUtils.DeflaterInputStream dos = new ZipUtils.DeflaterInputStream(dis); try { UploadChunkRequestTO chunkRequest = null; int chunkNumber = 0; while (true) { int bufferPosition = 0; while (true) { int numRead = dos.read(buffer, bufferPosition, MAX_CHUNK_SIZE - bufferPosition); if (numRead == -1) break; bufferPosition += numRead; if (bufferPosition == MAX_CHUNK_SIZE) break; } if (bufferPosition == 0) { chunkRequest.total_chunks = chunkRequest.number; chunkRequest.photo_hash = com.mobicage.rogerthat.util.TextUtils .toHex(complete.digest()); UploadChunkResponseHandler responsehandler = new UploadChunkResponseHandler(); responsehandler.setChunkRequest(chunkRequest); Rpc.uploadChunk(responsehandler, chunkRequest); break; } if (chunkNumber > 0) { chunkRequest.total_chunks = -1; chunkRequest.photo_hash = null; UploadChunkResponseHandler responsehandler = new UploadChunkResponseHandler(); responsehandler.setChunkRequest(chunkRequest); Rpc.uploadChunk(responsehandler, chunkRequest); } String chunk = null; if (bufferPosition == MAX_CHUNK_SIZE) chunk = Base64.encodeBytes(buffer); else { byte[] tmp = new byte[bufferPosition]; System.arraycopy(buffer, 0, tmp, 0, bufferPosition); chunk = Base64.encodeBytes(tmp); } chunkRequest = new UploadChunkRequestTO(); chunkRequest.parent_message_key = parentKey; chunkRequest.message_key = messageKey; chunkRequest.number = ++chunkNumber; chunkRequest.chunk = chunk; chunkRequest.service_identity_user = service; chunkRequest.content_type = contentType; if (bufferPosition < MAX_CHUNK_SIZE) { chunkRequest.total_chunks = chunkRequest.number; chunkRequest.photo_hash = com.mobicage.rogerthat.util.TextUtils .toHex(complete.digest()); UploadChunkResponseHandler responsehandler = new UploadChunkResponseHandler(); responsehandler.setChunkRequest(chunkRequest); Rpc.uploadChunk(responsehandler, chunkRequest); break; } } } finally { dos.close(); } } finally { dis.close(); } } finally { in.close(); } if (deleteImageOnFinish) f.delete(); transferQueueAdd(messageKey); } }); return willDirectlyStartTransferring; }
From source file:API.amazon.mws.feeds.service.MarketplaceWebServiceClient.java
/** * Calculate content MD5 header values for feeds stored on disk. *//* ww w. j a va 2 s .c o m*/ private String computeContentMD5HeaderValue(FileInputStream fis) throws IOException, NoSuchAlgorithmException { DigestInputStream dis = new DigestInputStream(fis, MessageDigest.getInstance("MD5")); byte[] buffer = new byte[8192]; while (dis.read(buffer) > 0) ; String md5Content = new String( org.apache.commons.codec.binary.Base64.encodeBase64(dis.getMessageDigest().digest())); // Effectively resets the stream to be beginning of the file via a FileChannel. fis.getChannel().position(0); return md5Content; }
From source file:com.flexoodb.common.FlexUtils.java
/** * computes the SHA1 hashcode for the passed byte array. * * @param ba byte array./*from w w w . j a va 2 s .co m*/ * @return SHA1 string. */ public static String computeSHA1(byte[] ba) { String hash = null; try { MessageDigest sha1 = MessageDigest.getInstance("SHA1"); InputStream is = new ByteArrayInputStream(ba); BufferedInputStream bis = new BufferedInputStream(is); DigestInputStream dis = new DigestInputStream(bis, sha1); while (dis.read() != -1) { } ; byte[] h = sha1.digest(); Formatter formatter = new Formatter(); for (byte b : h) { formatter.format("%02x", b); } hash = formatter.toString(); } catch (Exception e) { e.printStackTrace(); } return hash; }