Video Coming Soon...

Created by Zed A. Shaw Updated 2025-10-23 14:49:14

37: sha2sum

This exercise is pending. Quick notes about this exercise:

The Code

View Source file go-coreutils/sha512sum/main.go Only

package main

import (
    "fmt"
    "flag"
    "os"
    "log"
    "crypto/sha512"
)

type Opts struct {
    Inputs []string
}

func parse_opts() Opts {
    var opts Opts

    flag.Parse()
    opts.Inputs = flag.Args()

    return opts
}

func to_hex(hash [sha512.Size]byte) string {
    result := ""

    for _, b := range hash {
        result += fmt.Sprintf("%x", b)
    }

    return result
}

func main() {
    opts := parse_opts()

    for _, fname := range opts.Inputs {
        in_data, err := os.ReadFile(fname)
        if err != nil { log.Fatal(err) }

        hash := sha512.Sum512(in_data)

        fmt.Println(to_hex(hash), fname)
    }
}
Previous Lesson Next Lesson

Register for Learn Go the Hard Way

Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.