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.

HelloWorld
DIGEST_ALGORITHM: SHA-224
digest.length: 28
b07a0b24d54879214f2361e0a1ac320442fa4e53a0f607d126fbfb8a


Javarmi123
DIGEST_ALGORITHM: SHA-224
digest.length: 28
4a5437374a1c2abf873506202b72d734caf5b35b190f7f33b0cca650


HelloWorld
DIGEST_ALGORITHM: SHA-256
digest.length: 32
872e4e50ce9990d8b041330c47c9ddd11bec6b503ae9386a99da8584e9bb12c4


Javarmi123
DIGEST_ALGORITHM: SHA-256
digest.length: 32
f1d5f8d75bb55c777207c251d07d9091dc10fe7d6682db869106aacb4b7df678

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(System.in);
		
		// **** 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) {
			e.printStackTrace();
		}
		
		// **** instantiate the message digest ****
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance(DIGEST_ALGORITHM);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		
		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 ****
		System.out.println(byteArrayToHex(digest));

		// **** close scanner ****
		sc.close();
	}

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.

John

Follow me on Twitter:  @john_canesssa

 

Leave a Reply

Your email address will not be published.

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