Skip to content

Instantly share code, notes, and snippets.

@jehiah
Created May 23, 2014 16:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jehiah/ee7214c07294a1c0ed89 to your computer and use it in GitHub Desktop.
Save jehiah/ee7214c07294a1c0ed89 to your computer and use it in GitHub Desktop.
Utility for hashing the records of a file
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/csv"
"flag"
"fmt"
"log"
"os"
"strings"
)
func main() {
hmacKey := flag.String("hmac-key", "", "key string for hmac")
inputFilename := flag.String("input-file", "", "path to input csv")
outputFilename := flag.String("output-file", "out.log", "path to output file")
flag.Parse()
in, err := os.Open(*inputFilename) // For read access.
if err != nil {
log.Fatal(err)
}
out, err := os.Create(*outputFilename) // for write access
if err != nil {
log.Fatal(err)
}
r := csv.NewReader(in)
mac := hmac.New(sha256.New, []byte(*hmacKey))
for {
line, err := r.Read()
if len(line) == 0 || err != nil {
if err != nil && err.Error() != "EOF" {
log.Printf("Error %v", err)
}
break
}
mac.Reset()
record := strings.Trim(line[0], `"`)
mac.Write([]byte(record))
fmt.Fprintf(out, "%x\n", mac.Sum(nil))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment