From d936a830e4866f070ba7726ecb845cc66644a238 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 12 Apr 2014 10:02:48 +0200 Subject: [PATCH] add sha1 sum to file --- .../persistance/FileBackend.java | 45 +++++++++++++++++++ .../services/XmppConnectionService.java | 1 - .../xmpp/jingle/JingleConnection.java | 4 +- .../xmpp/jingle/stanzas/Content.java | 3 +- 4 files changed, 50 insertions(+), 3 deletions(-) diff --git a/src/eu/siacs/conversations/persistance/FileBackend.java b/src/eu/siacs/conversations/persistance/FileBackend.java index 122898b9..dbbf0a10 100644 --- a/src/eu/siacs/conversations/persistance/FileBackend.java +++ b/src/eu/siacs/conversations/persistance/FileBackend.java @@ -1,11 +1,15 @@ package eu.siacs.conversations.persistance; +import java.io.BufferedInputStream; import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import android.content.Context; import android.graphics.Bitmap; @@ -16,6 +20,7 @@ import android.util.LruCache; import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Message; +import eu.siacs.conversations.utils.CryptoHelper; public class FileBackend { @@ -84,6 +89,7 @@ public class FileBackend { Log.d("xmppService", "couldnt compress"); } os.close(); + message.setBody(this.createSha1(file)); return file; } catch (FileNotFoundException e) { // TODO Auto-generated catch block @@ -112,4 +118,43 @@ public class FileBackend { } return thumbnail; } + + private String createSha1(final File file) { + InputStream fis = null; + try { + MessageDigest digest = MessageDigest.getInstance("SHA-1"); + fis = new FileInputStream(file); + int n = 0; + byte[] buffer = new byte[8192]; + while (n != -1) { + n = fis.read(buffer); + if (n > 0) { + digest.update(buffer, 0, n); + } + } + fis.close(); + return CryptoHelper.bytesToHex(digest.digest()); + } catch (NoSuchAlgorithmException e) { + return null; + } catch (FileNotFoundException e) { + if (fis!=null) { + try { + fis.close(); + return null; + } catch (IOException e1) { + return null; + } + } + } catch (IOException e) { + if (fis!=null) { + try { + fis.close(); + return null; + } catch (IOException e1) { + return null; + } + } + } + return null; + } } diff --git a/src/eu/siacs/conversations/services/XmppConnectionService.java b/src/eu/siacs/conversations/services/XmppConnectionService.java index 4dfcff50..cce6882b 100644 --- a/src/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/eu/siacs/conversations/services/XmppConnectionService.java @@ -407,7 +407,6 @@ public class XmppConnectionService extends Service { message.setPresence(presence); message.setType(Message.TYPE_IMAGE); File file = this.fileBackend.copyImageToPrivateStorage(message, uri); - Log.d(LOGTAG,"new file"+file.getAbsolutePath()); conversation.getMessages().add(message); databaseBackend.createMessage(message); sendMessage(message, null); diff --git a/src/eu/siacs/conversations/xmpp/jingle/JingleConnection.java b/src/eu/siacs/conversations/xmpp/jingle/JingleConnection.java index a5453443..31b643c0 100644 --- a/src/eu/siacs/conversations/xmpp/jingle/JingleConnection.java +++ b/src/eu/siacs/conversations/xmpp/jingle/JingleConnection.java @@ -34,6 +34,7 @@ public class JingleConnection { private String responder; private List candidates = new ArrayList(); private HashMap connections = new HashMap(); + private File file = null; private OnIqPacketReceived responseListener = new OnIqPacketReceived() { @@ -110,7 +111,8 @@ public class JingleConnection { if (message.getType() == Message.TYPE_IMAGE) { content.setAttribute("creator", "initiator"); content.setAttribute("name", "a-file-offer"); - content.offerFile(this.mXmppConnectionService.getFileBackend().getImageFile(message)); + this.file = this.mXmppConnectionService.getFileBackend().getImageFile(message); + content.offerFile(file,message.getBody()); content.setCandidates(this.mJingleConnectionManager.nextRandomId(),this.candidates); packet.setContent(content); Log.d("xmppService",packet.toString()); diff --git a/src/eu/siacs/conversations/xmpp/jingle/stanzas/Content.java b/src/eu/siacs/conversations/xmpp/jingle/stanzas/Content.java index 27d6b9e3..304656ee 100644 --- a/src/eu/siacs/conversations/xmpp/jingle/stanzas/Content.java +++ b/src/eu/siacs/conversations/xmpp/jingle/stanzas/Content.java @@ -15,12 +15,13 @@ public class Content extends Element { super("content"); } - public void offerFile(File actualFile) { + public void offerFile(File actualFile, String hash) { Element description = this.addChild("description", "urn:xmpp:jingle:apps:file-transfer:3"); Element offer = description.addChild("offer"); Element file = offer.addChild("file"); file.addChild("size").setContent(""+actualFile.length()); file.addChild("name").setContent(actualFile.getName()); + file.addChild("hash","urn:xmpp:hashes:1").setAttribute("algo", "sha-1").setContent(hash); } public void setCandidates(String transportId, List canditates) {