Projects on Hashing

Hashing Assignment #1

Come up with a hashing algorithm that translates a card (suit and rank) in a standard deck of cards to a unique integer. Create methods to convert from two Strings to a number and also back from a number to a string representation of the card.

There should be no collisions - each card should easily code to a unique number and vice versa. Only store this number to represent the Card internally (do not store the rank and suit).

Here is an example of some test code:

public static void main(String [] args) {

    Card x = new Card("Jack", "Clubs");

    System.out.println(x.getHash());  /* should print the hashed number */

    System.out.println(x); /* should print "Jack of Clubs" */


Here is what the class should look like:

public class Card {

private int hashedRepresentation;

public Card(String rank, String suit) {

  hashed Reprsentation = stringToInt(rank, suit);


public String intToString(int hashed) {

  /* your code goes here */


public int stringToInt(String rank, String suit) {

  /* your code goes here */


public int getHash() {

   return hashedRepresentation;


Hashing Assignment #2

Create a system of storing library books with two hash numbers each, one for the author name and one for the title. Note that collisions are possible since the same author has likely written more than one book. Likewise it is possible for two authors to have a book of the same title.

The goal is to create a set of two numbers for each book that will allow the user to look up quickly to see if a given book (author and title strings) is stored in the library or not.

Here is an example:

The book ("David Copperfield", "Charles Dickens") might translate to:

(3423490, 2349008)

You then store these numbers in a database. If someone is looking for

("David Copperfield", "Charles Dickens"), you would search your database and quickly determine that the book is present.

Look up on the internet to try to find a good way to hash Strings.

Hashing Assignment #3

A checksum (or hash sum) is used in the field of communications to detect transmission errors. When a message is sent from point A to point B, it is useful for the software at point B to recognize if an error has occurred in the transmission. (Often the software at point B will then respond to this error by asking point A to re-transmit the message).

In this example, we will assume that point A periodically transmits a bunch of social security numbers to point B. We will create a simple checksum as follows:

data #1:     171-23-197

data #2:     675-34-761

data #3:     898-00-795

checksum:  576-53-567

Here, the checksum is calculated in such a way that the sum of each column of numbers yields a zero. For example, the rightmost column's sum is 20 (7 + 1 + 5 + 7). But we discard the 2 and end up with a zero. This is the case for each column. You should take a moment now to verify this in the data table, above.

When transmitting the data from the original file from point A to point B we include the checksum as an additional datapoint. Note that the checksum is calculated using the same algorithm not matter how many datapoints are present.

Your assignment is to create two classes:

The Transmitter class should read an input file full of SSNs which are initially stored as Strings, one per line. It should then create a new output file (which will simulate transmission in our project) with all the same SSNs but with the checksum added at the end of the file. The checksum SSN should look like any other SSN.

The second class, Receiver, should read the output file created by the first class and determine if the checksum SSN is valid. It should print "VALID" or "INVALID" to the console depending on if the checksum checks out. This would signify that the transmission of the data has gone smoothly or has errors in it.

When you test your code, be sure to put in some incorrect checksums on occasion into the output file to verify that the Receiver class is working properly and is able to detect transmission errors by noting that the checksum is wrong.

Hashing Assignment #4

Most of today’s applications use login/password in order to authenticate. Users often use the same login/password for different kinds of applications. If the pair is stolen, everybody can access all the applications the user has access to.

Too often passwords are stored as clear text. Thus the password can be read directly by the database’s administrator, super users or SQL Injection attack etc. The backup media is also vulnerable. In order to solve this problem, passwords must be stored in an encrypted fashion.

Come up with a hashing algorithm that translates a password to an integer. Set up this hash so that if a hacker were to open the file with the hashed passwords, he/she would be unable to decode the original passwords.

This is harder than it seems because if a password is simply hashed,  identical passwords will generate the same hash. This will weaken the security of the system because the attacker can figure out the algorithm by pre-computing the hashed version of some fixed (known) passwords thereby learning the algorithm.

To get around this, you need to add SALT to each password. Watch this video to understand how adding SALT works:

Assume user passwords are a combination of letters and numbers. To make this exercise a bit easier, further assume all passwords are exactly 8 characters long. NOTE: You should use Java's built in SecureRandom class to generate your SALT character array.

1 point
I have completed the project(s) for this lesson and have demonstrated them to my instructor.