Skip to content

Instantly share code, notes, and snippets.

@osmanmakal
Last active October 6, 2019 02:26
Show Gist options
  • Save osmanmakal/bfe32c22ed59a0924a5ba8c395fea8a4 to your computer and use it in GitHub Desktop.
Save osmanmakal/bfe32c22ed59a0924a5ba8c395fea8a4 to your computer and use it in GitHub Desktop.
xxHash 64bit usage example in golang
package main
import (
"fmt"
"hash"
"io"
"os"
"github.com/pierrec/xxHash/xxHash64"
)
func main() {
h, e := FileHash("file.qcow")
fmt.Println(h, e)
}
// Regex: ^[a-f0-9]{16}$
func FileHash(filename string) (string, error) {
var xxh hash.Hash
xxh = xxHash64.New(0)
f, err := os.Open(filename)
if err != nil {
return "", err
}
defer f.Close()
defer xxh.Reset()
if _, err := io.Copy(xxh, f); err != nil {
return "", err
}
return hex.EncodeToString(xxh.Sum(nil)), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment