Created
May 23, 2014 16:03
-
-
Save jehiah/ee7214c07294a1c0ed89 to your computer and use it in GitHub Desktop.
Utility for hashing the records of a file
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
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