File hashes have a multitude of uses and MD5 is, despite the fact that weaknesses have been found in the algorithm, still very useful and widely employed. Creating MD5 digests in Java is quite easy, but the documentation for MessageDigest is a little bit tricky. Here is a simple method that takes in a string of input and returns the hash of the string.
public static String md5(String input) {
// Compute the MD5 digest of the passed String
MessageDigest algorithm = null;
try {
algorithm = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) { e.printStackTrace(); }
algorithm.reset();
byte[] hash = algorithm.digest(input.getBytes());
// Convert it to a String
StringBuffer ret = new StringBuffer();
for (int x = 0; x < hash.length; x++) {
String hexString = Integer.toHexString(0xFF & hash[x]);
if (hexString.length() == 1)
hexString = "0" + hexString;
ret.append(hexString);
// Add the dashes
if (x == 3 || x == 5 || x == 7 || x == 9)
ret.append("-");
} //for
return ret.toString().toUpperCase();
} //md5
I came across a post today in which someone mentioned that they used Perl for text replacement and, despite the simplicity of the solution, I hadn’t ever thought of it. By simply using three options with the perl command, you can run a quick script to do regex replacement in a file. The -p option tells Perl to treat your script as a loop, reading in from the diamond (<>) operator. -i makes it do in-place editing of files and -e allows you to script from the command line. For example:
~$ perl -p -i -e "s/foo/bar/g" baz.txt
The above command will replace ‘foo’ with ‘bar’ throughout the file ‘baz.txt’
Making your own Perl modules is actually really easy, only involving a few special alterations to a script and an environment variable to make them easy to utilize. To make Perl modules, you first need to understand Object Oriented Perl, so if that’s new to you, check out Tom’s object-oriented Perl Tutorial. Onward.
For this example, I’ll just use a simplified version of the ‘Person’ class from the above-mentioned tutorial:
package Person;
use strict;
sub new {
my $self = {
NAME => undef,
AGE => undef,
PEERS => [],
};
bless($self);
return $self;
}
sub name {
my $self = shift;
if (@_) { $self->{NAME} = shift }
return $self->{NAME};
}
sub age {
my $self = shift;
if (@_) { $self->{AGE} = shift }
return $self->{AGE};
}
sub peers {
my $self = shift;
if (@_) { @{ $self->{PEERS} } = @_ }
return @{ $self->{PEERS} };
}
1;
To begin with, let’s make a directory where you will put all the modules you make; say, ~/perl-modules. Take the code above and save it as Person.pm in your new perl-modules directory. Notice the .pm extension that perl will look for when you go to use this module. Now, we need to let perl know where to look for the modules you write. When you ask for a Perl module by writing use foo in a script, perl, by default, it looks in a bunch of system directories and in the current directory for anything named foo.pm. You can tell it to look other places by adding to the PERL5LIB environment variable. For ease, I simply put this into my .bashrc:
export PERL5LIB=~/perl-modules
Now, anywhere that I write a perl script, I can simply say use Person and the Person class will be grabbed from my perl-modules directory.
Another thing to keep in mind when writing modules is that you should load the Carp module and use the functions carp and croak rather than warn and die respectively. Carp’s functions produce errors that reference the proper context in the user’s code when an error occurs, rather then saying there is a problem in your module.
Have some CHMs that you’d rather browse as PDFs? Easy. To convert, you’ll need libchm-bin and htmldoc on Ubuntu, probably the same on Debian and similar on others. Then, do the following:
$ extract_chmLib foo.chm foo/
$ htmldoc --webpage -f foo.pdf foo/*.html
Done.
Wikipedia has an amazingly large and varied database of information, including numerous lists, such as a list of ridiculous highway intersections. There are some really screwed up intersections shown on that page most notably I-40 meeting I-295 in Knoxville, I-96 with highway 39 in Detroit, and the local-to-me Springfield Mixing Bowl. None of these intersections, however, can shake a stick at Swindon’s Magic Roundabout which is a single large central roundabout with 5 feeder roundabouts.