Skip to content

Instantly share code, notes, and snippets.

@cnaude
Last active July 26, 2021 16:41
Show Gist options
  • Save cnaude/a79e2a525c1eca00c7c601967131acc3 to your computer and use it in GitHub Desktop.
Save cnaude/a79e2a525c1eca00c7c601967131acc3 to your computer and use it in GitHub Desktop.
crc-32 mpeg-2 implementation in golang
package main
// based on https://stackoverflow.com/questions/54339800/how-to-modify-crc-32-to-crc-32-mpeg-2
import (
"fmt"
)
// crc32mpeg2 : calculate crc-32-mpeg-2 checksum
func crc32mpeg2(b []byte) uint32 {
var crc uint32 = 0xFFFFFFFF
for _, v := range b {
// xor next byte to upper bits of crc
crc ^= uint32(v) << uint32(24)
for j := 0; j < 8; j++ {
m := crc >> 31
crc <<= 1
crc ^= (0 - m) & 0x04c11db7
}
}
return crc
}
func main() {
s := "abc123"
h := crc32b([]byte(s))
fmt.Printf("%s -> %08x\n", s, h)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment