In the process of cross-language development, we often encounter situations where we need to translate an algorithm or function from one language to another. In this process, block CRC32 translation is a common requirement. However, translating this block CRC32 from Go to JavaScript is not an easy task. In this article, PHP editor Xigua will introduce how to correctly translate this block CRC32 from Go to JavaScript to help developers solve this problem.
Question content
I have this function in go:
package main import ( "fmt" "github.com/snksoft/crc" ) var crctable *crc.table func init() { params := crc.crc32 params.finalxor = 0 params.reflectout = false crctable = crc.newtable(params) } func crccalculateblock(data []byte) uint32 { if len(data)%4 > 0 { panic("block size needs to be a multiple of 4") } h := crc.newhashwithtable(crctable) var buf [4]byte for i := 0; i < len(data); i += 4 { buf[0] = data[i+3] buf[1] = data[i+2] buf[2] = data[i+1] buf[3] = data[i+0] h.update(buf[:]) } return h.crc32() } func main() { data := []byte{1, 2, 3, 4, 5, 6, 7, 8} crc := crccalculateblock([]byte(data)) fmt.printf("crc is 0x%04x\n", crc) }
The result is: 0x948b389d
I'm trying to translate this to javascript, but I'm missing something:
var makeCRCTable = function(){ var c; var crcTable = []; for(var n =0; n < 256; n++){ c = n; for(var k =0; k < 8; k++){ c = ((c&1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1)); } crcTable[n] = c; } return crcTable; } var crc32 = function(u8array) { var crcTable = window.crcTable || (window.crcTable = makeCRCTable()); var crc = 0 ^ (-1); for (var i = 0; i < u8array.length; i+=4 ) { crc = (crc >>> 8) ^ crcTable[(crc ^ u8array[i+3]) & 0xFF]; crc = (crc >>> 8) ^ crcTable[(crc ^ u8array[i+2]) & 0xFF]; crc = (crc >>> 8) ^ crcTable[(crc ^ u8array[i+1]) & 0xFF]; crc = (crc >>> 8) ^ crcTable[(crc ^ u8array[i]) & 0xFF]; } return (crc ^ (-1)) >>> 0; }; console.log(crc32(Uint8Array.from([1,2,3,4,5,6,7,8])).toString(16))
But the results are different. (46e32ed6)
Even without the final XOR I get b91cd129
Can anyone explain to me how to correct this and why this is wrong?
Solution
There are two differences:
-
go implementation has called
reflect
(see https://www.php.cn/link/f23775b54b9e62e2d15498c3b9418630):if t.crcparams.reflectout != t.crcparams.reflectin { ret = reflect(ret, t.crcparams.width) }
- finalxor
in
go is
0
(params.finalxor = 0
) and in js it is-1
(return (crc ^ (-1)) phpcngt phpcn>> 0;phpcnendc phpcn)
This is an updated js implementation that generates the same hash value.
var makeCRCTable = function () { var c; var crcTable = []; for (var n = 0; n < 256; n++) { c = n; for (var k = 0; k < 8; k++) { c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1; } crcTable[n] = c; } return crcTable; }; var crc32 = function (u8array) { var crcTable = window.crcTable || (window.crcTable = makeCRCTable()); var crc = 0 ^ -1; for (var i = 0; i < u8array.length; i += 4) { crc = (crc >>> 8) ^ crcTable[(crc ^ u8array[i + 3]) & 0xff]; crc = (crc >>> 8) ^ crcTable[(crc ^ u8array[i + 2]) & 0xff]; crc = (crc >>> 8) ^ crcTable[(crc ^ u8array[i + 1]) & 0xff]; crc = (crc >>> 8) ^ crcTable[(crc ^ u8array[i]) & 0xff]; } crc = reverseBits(crc, 32); return (crc ^ 0) >>> 0; }; function reverseBits(integer, bitLength) { if (bitLength > 32) { throw Error( 'Bit manipulation is limited to <= 32 bit numbers in JavaScript.' ); } let result = 0; for (let i = 0; i < bitLength; i++) { result |= ((integer >> i) & 1) << (bitLength - 1 - i); } return result >>> 0; // >>> 0 makes it unsigned even if bit 32 (the sign bit) was set } console.log(crc32(Uint8Array.from([1, 2, 3, 4, 5, 6, 7, 8])).toString(16));
The above is the detailed content of How to correctly translate this block CRC32 from Go to JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Article discusses iterating through maps in Go, focusing on safe practices, modifying entries, and performance considerations for large maps.Main issue: Ensuring safe and efficient map iteration in Go, especially in concurrent environments and with l

The article discusses creating and manipulating maps in Go, including initialization methods and adding/updating elements.

The article discusses differences between arrays and slices in Go, focusing on size, memory allocation, function passing, and usage scenarios. Arrays are fixed-size, stack-allocated, while slices are dynamic, often heap-allocated, and more flexible.

The article discusses creating and initializing slices in Go, including using literals, the make function, and slicing existing arrays or slices. It also covers slice syntax and determining slice length and capacity.

The article explains how to create and initialize arrays in Go, discusses the differences between arrays and slices, and addresses the maximum size limit for arrays. Arrays vs. slices: fixed vs. dynamic, value vs. reference types.

Article discusses syntax and initialization of structs in Go, including field naming rules and struct embedding. Main issue: how to effectively use structs in Go programming.(Characters: 159)

The article explains creating and using pointers in Go, discussing benefits like efficient memory use and safe management practices. Main issue: safe pointer use.

The article discusses the benefits of using Go (Golang) in software development, focusing on its concurrency support, fast compilation, simplicity, and scalability advantages. Key industries benefiting include technology, finance, and gaming.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version
Recommended: Win version, supports code prompts!
