Last active
July 26, 2021 16:41
-
-
Save cnaude/a79e2a525c1eca00c7c601967131acc3 to your computer and use it in GitHub Desktop.
crc-32 mpeg-2 implementation in golang
This file contains 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 | |
// 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