Saturday, August 27, 2016

Package Crypto - SHA256 Hash Algorithm Example

The Go package crypto/sha256 produces hexadecimal cryptographic hash (digest) of a message. You can play with the examples shared here and check the output in Go Playground (links shared below corresponding code snippets).

Let us see a working example:

Click to play with the above code

Here we've successfully encrypted "My secret message" using SHA256.
Things to note:

%X  is used to print elements of a Slice in hexadecimal, i.e. base 16 with upper-case letters for A-F)

Question
  • What happens if you replace %X with %x?
Let us see the next example where we're comparing the two outputs and printing true or false using %t :

Here the output is false. Why? It's simply because the two messages are different.

Click to play with the above code

Here's an excellent article about password encryption and security.