add sha1 sum to file

This commit is contained in:
Daniel Gultsch 2014-04-12 10:02:48 +02:00
parent 96be96f9f8
commit d936a830e4
4 changed files with 50 additions and 3 deletions

View File

@ -1,11 +1,15 @@
package eu.siacs.conversations.persistance; package eu.siacs.conversations.persistance;
import java.io.BufferedInputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
@ -16,6 +20,7 @@ import android.util.LruCache;
import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.utils.CryptoHelper;
public class FileBackend { public class FileBackend {
@ -84,6 +89,7 @@ public class FileBackend {
Log.d("xmppService", "couldnt compress"); Log.d("xmppService", "couldnt compress");
} }
os.close(); os.close();
message.setBody(this.createSha1(file));
return file; return file;
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
@ -112,4 +118,43 @@ public class FileBackend {
} }
return thumbnail; 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;
}
} }

View File

@ -407,7 +407,6 @@ public class XmppConnectionService extends Service {
message.setPresence(presence); message.setPresence(presence);
message.setType(Message.TYPE_IMAGE); message.setType(Message.TYPE_IMAGE);
File file = this.fileBackend.copyImageToPrivateStorage(message, uri); File file = this.fileBackend.copyImageToPrivateStorage(message, uri);
Log.d(LOGTAG,"new file"+file.getAbsolutePath());
conversation.getMessages().add(message); conversation.getMessages().add(message);
databaseBackend.createMessage(message); databaseBackend.createMessage(message);
sendMessage(message, null); sendMessage(message, null);

View File

@ -34,6 +34,7 @@ public class JingleConnection {
private String responder; private String responder;
private List<Element> candidates = new ArrayList<Element>(); private List<Element> candidates = new ArrayList<Element>();
private HashMap<String, SocksConnection> connections = new HashMap<String, SocksConnection>(); private HashMap<String, SocksConnection> connections = new HashMap<String, SocksConnection>();
private File file = null;
private OnIqPacketReceived responseListener = new OnIqPacketReceived() { private OnIqPacketReceived responseListener = new OnIqPacketReceived() {
@ -110,7 +111,8 @@ public class JingleConnection {
if (message.getType() == Message.TYPE_IMAGE) { if (message.getType() == Message.TYPE_IMAGE) {
content.setAttribute("creator", "initiator"); content.setAttribute("creator", "initiator");
content.setAttribute("name", "a-file-offer"); 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); content.setCandidates(this.mJingleConnectionManager.nextRandomId(),this.candidates);
packet.setContent(content); packet.setContent(content);
Log.d("xmppService",packet.toString()); Log.d("xmppService",packet.toString());

View File

@ -15,12 +15,13 @@ public class Content extends Element {
super("content"); 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 description = this.addChild("description", "urn:xmpp:jingle:apps:file-transfer:3");
Element offer = description.addChild("offer"); Element offer = description.addChild("offer");
Element file = offer.addChild("file"); Element file = offer.addChild("file");
file.addChild("size").setContent(""+actualFile.length()); file.addChild("size").setContent(""+actualFile.length());
file.addChild("name").setContent(actualFile.getName()); 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<Element> canditates) { public void setCandidates(String transportId, List<Element> canditates) {