Java SHA-256

There was a full moon this morning providing some light before the sun came up. That was nice.

This week I have been taking care of some items that I could not during the past two. My wife and I were able to renew our TSA Pre Checked status for another five years. Was not sure why we had to answer the same questions we did five years ago, and get fingerprinted again. As far as I understand, fingerprints do not change over time. If they would, they would be totally useless. I do not recall if last time we had to present our passports or not. We did have to get our pictures taken. I do agree that the look of people may change in five years. The renewal will kick in before our current one expires.

My driver license is about to expire. My wife and I stopped at the DMV office in Eagan, MN. I decided to get the Real ID version. That would allow me to get on a commercial flight without having to carry my passport. Filled the form and got in line. Apparently you also need to present, in addition to your passport, your Social Security card. I am perplexed what is going on. If you have a passport how can you not have a Social Security card? I did not have my card so we had to go back.

On the second pass around, both my wife and I decided to get the Real ID driver license. My wife had renewed hers driver license last year so her fee was discounted. In addition to your passport, you need to present a bill (i.e., utilities, insurance, etc) that verifies your address. After a picture we got our current driver license punched with small holes. It used to be that the DMV would cut one corner. That is no longer the case.

I have also got done my first blood donation for this year to the Red Cross. Every year I do one in spring and one in fall. If you are able to do so, please donate. It is a limited resource that when needed will save lives.

This morning I received a message from HackerRank to try the Java SHA-256 challenge. After reading the requirements, it seems that I inadvertently solved it on a previous post Java MD5.

Following are some test examples that I ran on for the Java solution I wrote using the Eclipse IDE.

digest.length: 28

digest.length: 28

digest.length: 32

digest.length: 32

I used the two strings provided in the challenge, but enabled a couple debug statements so one can appreciate the size of the hashes produced by two of the algorithms, SHA-224 and SHA-256 which is the subject on this challenge. Of course, I had to comment out the messages before submitting the code.

The main code for the challenge follows:

	 * Enter your code here. Read input from STDIN. Print output to STDOUT.
	public static void main(String[] args) {

		// **** select a digest algorithm ****
//		final String DIGEST_ALGORITHM	= "MD5";
//		final String DIGEST_ALGORITHM	= "SHA-1";
//		final String DIGEST_ALGORITHM	= "SHA-224";
		final String DIGEST_ALGORITHM	= "SHA-256";	
//		final String DIGEST_ALGORITHM	= "SHA-384";
//		final String DIGEST_ALGORITHM	= "SHA-512";

		// **** open scanner ****
		Scanner sc = new Scanner(;
		// **** read string ****
		String s = sc.nextLine();
//		System.out.println("s ==>" + s + "<==");
		// **** convert string to byte array ****
		byte[] sb = null;
		try {
			sb = s.getBytes("UTF-8");
		} catch (UnsupportedEncodingException e) {
		// **** instantiate the message digest ****
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance(DIGEST_ALGORITHM);
		} catch (NoSuchAlgorithmException e) {
		System.out.println("DIGEST_ALGORITHM: " + DIGEST_ALGORITHM);
		// **** generate the digest ****
		byte[] digest = md.digest(sb);
		System.out.println("digest.length: " + digest.length);
		// **** display the digest in hex ****

		// **** close scanner ****

I define a set of strings to be able to switch between algorithms. As you can see the same code works for all. If you are interested, the entire code for my solution is in my GitHub repository.

Hope you have enjoyed this post. If you have comments or questions regarding this or any other post in this blog, or if you need some help on a software development project, please leave me a note bellow. Requests for assistance will be kept in confidence.

Keep on reading and experimenting. It is the only way to learn.


Follow me on Twitter:  @john_canesssa


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.