Skip to content

Instantly share code, notes, and snippets.

@ghettoeinstein
Last active February 14, 2016 19:40
Show Gist options
  • Save ghettoeinstein/c064f4b56c9c8d4a7c17 to your computer and use it in GitHub Desktop.
Save ghettoeinstein/c064f4b56c9c8d4a7c17 to your computer and use it in GitHub Desktop.
// Write a program that prints the SHA256 hash of its standard
// input by default but supports a command-line flag to print
// the SHA384 or SHA512 hash instead.
// Exercise 4.2 from "The Go Programming Language"
package main
import (
"bufio"
"crypto/sha256"
"fmt"
"os"
)
func printHash(input []byte) {
// Print the SHA256 hash of standard input from its slice of bytes.
fmt.Printf("%s %x\n", input, sha256.Sum256(input))
}
func main() {
input := bufio.NewScanner(os.Stdin)
for input.Scan() {
// Create a byte slice from the standard input text
nbytes := []byte(input.Text())
printHash(nbytes)
}
}
@ghettoeinstein
Copy link
Author

Edited to change print_hash to printHash. Have to remember, in go land to follow conventions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment