Golang hash/crc32.ChecksumIEEE function examples

package hash/crc32

ChecksumIEEE returns the CRC-32 checksum of data using the IEEE polynomial.

Golang hash/crc32.ChecksumIEEE function usage examples

Example 1:

 package main

 import (
 "fmt"
 "hash/crc32"
 )

 func main() {

 checksum := crc32.ChecksumIEEE([]byte("abcd"))

 fmt.Printf("Checksum : %x \n", checksum)

 }

Example 2:

 // Check checksum.
 var checksum uint32

 n, err := fmt.Fscanf(file, "x\n", &checksum)
 if err != nil {
 fmt.Println(err)
 } else if n != 1 {
 fmt.Println("checksum.err: bad.snapshot.file")
 }

 // Load remaining snapshot contents.
 b, err := ioutil.ReadAll(file)
 if err != nil {
 fmt.Println(err)
 }

 // Generate checksum.
 byteChecksum := crc32.ChecksumIEEE(b)
 if uint32(checksum) != byteChecksum {
 fmt.Println("bad snapshot file")
 }

References :

https://github.com/goraft/raft/blob/master/server.go

http://golang.org/pkg/hash/crc32/#ChecksumIEEE

Advertisement