Skip to main content

eclipse-hasher updated and re-released on GitHub

Several years ago, I released an Eclipse plugin called Hasher. Hasher’s goal is to output values of common hash algorithms (MD5, SHA-1, SHA-256, SHA-384, and SHA-512 right now) of files selected in Eclipse. Hasher had fallen by the wayside and last worked under early version of Eclipse 3. I recently had a need for it for a personal project and decided to update it. Turns out, quite a bit has changed. Most notably, Eclipse actions are now deprecated in favor of Eclipse commands. Code using commands is much cleaner, but it’s quite a bit different, so I essentially had to rewrite the entire plugin. Also, I had some dependency issues that plagued me for far too long, but thanks to Stack Overflow, I was finally able to get things straightened out.

Hasher is now live on GitHub (https://github.com/gfairchild/eclipse-hasher), freshly tagged with v1.2. One of the things I learned during this rewrite is that there’s a lack of good examples and documentation out there for modern Eclipse plugins. I’m hopeful that Hasher can be useful to someone wanting to get into writing Eclipse plugins. Hasher is pretty simple right now, but it’s non-trivial (has external dependencies, interacts meaningfully with Eclipse – more than just Hello World). If you find yourself using it to learn, please let me know!

There’s still a to do list. I want to make the output prettier using a custom view. A tree view or a table view (or perhaps some hybrid) would probably be ideal. I don’t know how to do a custom view yet, though, so that’ll add to the learning process. Also, I want to make use of Eclipse’s Jobs API. Right now, I’m just manually creating a new thread to do computations. This works and leaves the UI free to do its work, but it’s not elegant and doesn’t take advantage of several nice features Eclipse offers for background jobs.

If you use Hasher, let me know what you think!

Hasher Update (and how to obtain the hash of a file in Java)

I updated Hasher today. The plugin still functions the same, but I fixed a bug in the hash generation algorithm (thanks to Vladimir Kozlov for pointing the bug out to me). I also greatly increased modularity which in turn reduced the code size rather significantly. Here is how I calculate the hash of a file (the String parameter algorithm can be any algorithm listed at http://java.sun.com/javase/6/docs/technotes/guides/security/StandardNames.html#MessageDigest):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
 
public static String generateHash(File file, String algorithm) throws NoSuchAlgorithmException, IOException {
	byte[] dataBytes = getFileContents(file);
	MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
	messageDigest.update(dataBytes);
	byte[] digestBytes = messageDigest.digest();
 
	// this chunk of code was taken from http://echochamber.me/viewtopic.php?f=11&t=16666&p=553936#p459685
	char[] hash = new char[2 * digestBytes.length];
	for (int i = 0; i < digestBytes.length; ++i) {
		hash[2 * i] = HEX_CHARS[(digestBytes[i] & 0xF0) >>> 4];
		hash[2 * i + 1] = HEX_CHARS[digestBytes[i] & 0x0F];
	}
 
	return new String(hash);
}
 
private static byte[] getFileContents(File file) throws IOException {
	FileInputStream fileInputStream = null;
 
	try {
		fileInputStream = new FileInputStream(file);
		byte fileBytes[] = new byte[fileInputStream.available()];
		fileInputStream.read(fileBytes);
		return fileBytes;
	} catch (IOException e) {
		throw e;
	} finally {
		if (fileInputStream != null)
			try {
				fileInputStream.close();
			} catch (IOException e) {
			} finally {
				fileInputStream = null;
			}
	}
}

Eclipse plugins section added

I’ve added a section for Eclipse plugins that I write. NextHub is written in C#, but my primarily coding language is Java, and I spend a lot of time in Eclipse everyday. Eclipse’s plugin community is huge, but occasionally, I find myself wanting a plugin that doesn’t exist. I figured I’d just go ahead and do it myself. 🙂

My first plugin is called Hasher, and it generates hashes (MD5, SHA-1, SHA-256, SHA-384, and SHA-512) of one or more selected files in an Eclipse project. I find myself comparing 2 files to see if they have the same hash quite often (especially for school projects), and I got tired of having to use an external application to do this. This is much more convenient. I found HashMe! through Google searches, but I read reports that it didn’t report the correct hashes, so I figured I’d just write it myself. I plan on improving the output later (putting the output into an actual table instead of text output to the console), but this is a good start and definitely gets the job done for now.

If you have any plugin requests or suggestions, please tell me!