예제 #1
0
 public boolean save(Avatar avatar) {
   File file;
   if (isAvatarCached(avatar)) {
     file = new File(getAvatarPath(avatar.getFilename()));
   } else {
     String filename = getAvatarPath(avatar.getFilename());
     file = new File(filename + ".tmp");
     file.getParentFile().mkdirs();
     OutputStream os = null;
     try {
       file.createNewFile();
       os = new FileOutputStream(file);
       MessageDigest digest = MessageDigest.getInstance("SHA-1");
       digest.reset();
       DigestOutputStream mDigestOutputStream = new DigestOutputStream(os, digest);
       mDigestOutputStream.write(avatar.getImageAsBytes());
       mDigestOutputStream.flush();
       mDigestOutputStream.close();
       String sha1sum = CryptoHelper.bytesToHex(digest.digest());
       if (sha1sum.equals(avatar.sha1sum)) {
         file.renameTo(new File(filename));
       } else {
         Log.d(Config.LOGTAG, "sha1sum mismatch for " + avatar.owner);
         file.delete();
         return false;
       }
     } catch (IllegalArgumentException | IOException | NoSuchAlgorithmException e) {
       return false;
     } finally {
       close(os);
     }
   }
   avatar.size = file.length();
   return true;
 }
예제 #2
0
 public Avatar getPepAvatar(Uri image, int size, Bitmap.CompressFormat format) {
   try {
     Avatar avatar = new Avatar();
     Bitmap bm = cropCenterSquare(image, size);
     if (bm == null) {
       return null;
     }
     ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
     Base64OutputStream mBase64OutputStream =
         new Base64OutputStream(mByteArrayOutputStream, Base64.DEFAULT);
     MessageDigest digest = MessageDigest.getInstance("SHA-1");
     DigestOutputStream mDigestOutputStream = new DigestOutputStream(mBase64OutputStream, digest);
     if (!bm.compress(format, 75, mDigestOutputStream)) {
       return null;
     }
     mDigestOutputStream.flush();
     mDigestOutputStream.close();
     avatar.sha1sum = CryptoHelper.bytesToHex(digest.digest());
     avatar.image = new String(mByteArrayOutputStream.toByteArray());
     return avatar;
   } catch (NoSuchAlgorithmException e) {
     return null;
   } catch (IOException e) {
     return null;
   }
 }
예제 #3
0
 public Avatar getStoredPepAvatar(String hash) {
   if (hash == null) {
     return null;
   }
   Avatar avatar = new Avatar();
   File file = new File(getAvatarPath(hash));
   FileInputStream is = null;
   try {
     BitmapFactory.Options options = new BitmapFactory.Options();
     options.inJustDecodeBounds = true;
     BitmapFactory.decodeFile(file.getAbsolutePath(), options);
     is = new FileInputStream(file);
     ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
     Base64OutputStream mBase64OutputStream =
         new Base64OutputStream(mByteArrayOutputStream, Base64.DEFAULT);
     MessageDigest digest = MessageDigest.getInstance("SHA-1");
     DigestOutputStream os = new DigestOutputStream(mBase64OutputStream, digest);
     byte[] buffer = new byte[4096];
     int length;
     while ((length = is.read(buffer)) > 0) {
       os.write(buffer, 0, length);
     }
     os.flush();
     os.close();
     avatar.sha1sum = CryptoHelper.bytesToHex(digest.digest());
     avatar.image = new String(mByteArrayOutputStream.toByteArray());
     avatar.height = options.outHeight;
     avatar.width = options.outWidth;
     return avatar;
   } catch (IOException e) {
     return null;
   } catch (NoSuchAlgorithmException e) {
     return null;
   } finally {
     close(is);
   }
 }