Java encrypt class

A simple implementation of an encryption class to be used in Java applications for either encrypting strings and calculating files’ fingerprint.

import java.io.FileInputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Encrypt {

   private FileInputStream fis;

   public Encrypt() { }

   public String encryptString(String input, String salt, String algorithm) {
      String output = null;
      String fullInput = null;

      if (!salt.equals(null)) {
         fullInput = salt + input;
      } else {
         fullInput = input;
      }

      try {
         MessageDigest digest = MessageDigest.getInstance(algorithm);
         digest.update(fullInput.getBytes(), 0, fullInput.length());
         output = new BigInteger(1, digest.digest()).toString(16);
      } catch (NoSuchAlgorithmException ex) {
         ex.printStackTrace();
      }
      return output;
   }

   // Based on: http://www.mkyong.com/java/how-to-generate-a-file-checksum-value-in-java/
   public String calculateFingerprint(String fileName, String algorithm) {
      StringBuffer sb = new StringBuffer();
      try {
         MessageDigest md = MessageDigest.getInstance(algorithm);
         fis = new FileInputStream(fileName);
         byte[] dataBytes = new byte[1024];
         int nread = 0;
         while ((nread = fis.read(dataBytes)) != -1) { md.update(dataBytes, 0, nread); }
         byte[] mdbytes = md.digest();
         for (int i = 0; i < mdbytes.length; i++) {
            sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
         }
      } catch (Exception ex) {
         ex.printStackTrace();
      }
      return sb.toString();
   }
}

Link to GiHub repository. The class is intended to be used as a library within Java applications.

Example for encrypting a string:

Encrypt en = new Encrypt();
String encryptedString = en.encryptString(textField.getText().toString(), \
"salt goes here if you want", "md5")));

Example for calculating the fingerprint of a file:

Encrypt en = new Encrypt();
String fingerPrint = en.calculateFingerprint("/path/to/file", sha1);
Advertisement

Android SQLite Adapter class

Working with an SQLite database on Android can require some times more effort than what you should really give. For instance, if you ship a pre-configured and populated database with an application, you would need first to copy over the database to the specific database directory and then open it for reading and/or writing data to it. The SQLiteOpenHelper class provides a few methods that make life a bit easier but it does not provide a copy method for handling this issue. In addition to this, a check method would be required before copying the database and so on. Following on this, I implemented a SQLite adapter class to cover my needs as bellow:

– Copy database to the correct directory.
– Open and close the database.
– Execute raw SQL query.
– Execute SQL query for string, int, long, double.
– Drop table.
– Return count of a table.
– Download a db copy from Internet and replace local one.

Source code at GitHub.