aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/siacs/conversations/persistance
diff options
context:
space:
mode:
authorDaniel Gultsch <daniel.gultsch@rwth-aachen.de>2014-04-12 10:02:48 +0200
committerDaniel Gultsch <daniel.gultsch@rwth-aachen.de>2014-04-12 10:02:48 +0200
commitd936a830e4866f070ba7726ecb845cc66644a238 (patch)
tree459db6ae6f15c7c6fabb5a25842f099b5183c534 /src/eu/siacs/conversations/persistance
parent96be96f9f8a7ccae492f87ac703585bc6ac2e7b1 (diff)
add sha1 sum to file
Diffstat (limited to 'src/eu/siacs/conversations/persistance')
-rw-r--r--src/eu/siacs/conversations/persistance/FileBackend.java45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/eu/siacs/conversations/persistance/FileBackend.java b/src/eu/siacs/conversations/persistance/FileBackend.java
index 122898b91..dbbf0a104 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;
+ }
}