RosettaCodeData/Task/CRC-32/Go/crc-32-2.go
Ingy döt Net 764da6cbbb CDE
2013-04-10 16:57:12 -07:00

31 lines
587 B
Go

package main
import "fmt"
var table [256]uint32
func init() {
for i := range table {
word := uint32(i)
for j := 0; j < 8; j++ {
if word&1 == 1 {
word = (word >> 1) ^ 0xedb88320
} else {
word >>= 1
}
}
table[i] = word
}
}
func crc32(s string) uint32 {
crc := ^uint32(0)
for i := 0; i < len(s); i++ {
crc = table[byte(crc)^s[i]] ^ (crc >> 8)
}
return ^crc
}
func main() {
fmt.Printf("%0x\n", crc32("The quick brown fox jumps over the lazy dog"))
}