Last active
October 6, 2019 02:26
-
-
Save osmanmakal/bfe32c22ed59a0924a5ba8c395fea8a4 to your computer and use it in GitHub Desktop.
xxHash 64bit usage example in golang
This file contains hidden or 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 ( | |
"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