Go Post #1: Simple Hashing

Ok so most people start learning a new language with hello world but that is just too simple to show off a new language. So instead of hello world I am going to post the first program I tried to write in go (of course after hello world). It was just a very simple hashing program which you pass in a string when you run the program and then you get back the string and what the hashed version of that string looks like.

package main

import(
    "fmt"
    "flag" // command line option parser
    "crypto/sha256"
    "hash"
)

var plaintext string

func main() {
    flag.Parse() // Scans the arg list and sets up flags
    if flag.NArg() > 0 {
        plaintext = flag.Arg(0)
    }

    h := sha256.New()  // h is a hash.Hash
    h.Write([]byte(plaintext))

    fmt.Printf("%s: %x\n", plaintext, h.Sum())
}

So lets looks at what exactly is going on in the example code. First you will notice import this is used just like in pretty much every other language and tells the compiler what libraries you want to include. Now if you don’t want sha256 hash you can change the import line that calls for crypto/sha256 with a different hashing algorithm. I was very happy at the selection of hashing algroithms you can chose from (md4, md5, sha1, sha256, sha384, sha512, md5sha1, ripemd160) basically to change the hash you use just change the import and later change the has you create.

Next in the code you see a variable definition it shows just a simple declaration of a string. It’s a bit different then most lanuagues but nothing to strange. After that you get to the real meat. The main function. Unlike your traditional C program I used the flag library in go which allos you to parse a list of flags (not shown here) as well is iterate through parameters. The if statement merely checks how many arguments were passes and if there is more then one it grabs the first one and sets it to the plaintext.

The last section is where the main work happens. I used go’s handy feature where you can define a variable when you first create it to create h which is the hash. The declaration happens with the := assignment. This is also the line that you chose your hashing algorithm. It should match whatever one you chose to import. In the next line I write the content of the plaintext into the hashing algorithm and you see how simple it is to cast a string into the byte array.

To finish it all off the last statement sums the hash and prints it out to the console. It’s that simple. Now at this point there are a few notes I would like to make. First, notice the lack of semicolons.  Yes, this language does have them but the only spot they are needed is in for loops and if you want multiple statements per line. Second, notice the bracing format. While I truly dislike the one true brace style its forced by the language and I have accepted it since Go provides so many neat features.

Well there you go that’s how you do a simple hash in Go and some of the language basics. Now since someone will complain if I don’t here is a more simple version of the hashing program using hello world.

package main

import (
“fmt”
“os”
“crypto/sha256″
)

func main() {
plaintext := “hello, world”
if len(os.Args) > 1 {
plaintext = os.Args[1]
}
h := sha256.New() // h is a hash.Hash
h.Write([]byte(plaintext))
fmt.Printf(“%s\n%x\n”, plaintext, h.Sum())
}

UPDATE: Added code from comments to replace hello world. (May come back and rework this article)
Advertisement

Tags: , ,

4 Responses to “Go Post #1: Simple Hashing”

  1. Anthony Starks says :

    // refactored: (the code above will not compile)
    // use os.Args instead of the flag package, remove unused hash package, default text added
    package main

    import (
    “fmt”
    “os”
    “crypto/sha256″
    )

    func main() {
    plaintext := “hello, world”
    if len(os.Args) > 1 {
    plaintext = os.Args[1]
    }
    h := sha256.New() // h is a hash.Hash
    h.Write([]byte(plaintext))
    fmt.Printf(“%s\n%x\n”, plaintext, h.Sum())
    }

  2. Rajender says :

    Your first version has only one bug. that is unused import of hash. if you remove that one line it works fine. And also use of flag package is recommended for parsing command line arguments.

    package main

    import(
    “fmt”
    “flag” // command line option parser
    “crypto/sha256″
    )

    var plaintext string

    func main() {
    flag.Parse() // Scans the arg list and sets up flags
    if flag.NArg() > 0 {
    plaintext = flag.Arg(0)
    }

    h := sha256.New() // h is a hash.Hash
    h.Write([]byte(plaintext))
    fmt.Printf(“%s: %x\n”, plaintext, h.Sum())
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.