Last active
February 14, 2016 19:40
-
-
Save ghettoeinstein/c064f4b56c9c8d4a7c17 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Edited to change
print_hash
toprintHash
. Have to remember, in go land to follow conventions!