admin管理员组文章数量:1406937
I've read the famous article A PAINLESS GUIDE TO CRC ERROR DETECTION ALGORITHMS, now I want to try this out. I'm using a nodejs module node-crc, it looks easy to use:
var crc = require('crc');
crc.crc32('hello');
# => "3610a686"
The result of function crc32
, I believe, is the checksum of hello
using polynomial 0x04C11DB7
. Please correct me if I' wrong.
So the next step is appending the checksum to the original data. Here's my code:
var crc = require('crc');
var bf = Buffer("test");
charlist = crc.crc32(bf).toString();
var k = []
for (var i=0; i< 8; i++) {
k.push(parseInt(charlist[i], 16));
}
console.log(k)
var checksum = Buffer(k);
console.log(crc.crc32(Buffer.concat([bf, checksum])));
output
[ 13, 8, 7, 15, 7, 14, 0, 12 ]
646b4f9b
I'm expecting the second output to be 0
, but it's not.
Therefore I'm here asking for help. What is the correct way to do this(appending and checking if the message was correctly received)? Thank you.
Edit
So I understand that the second output isn't necessarilly 0
. Here's the final solution:
var crc = require('crc');
var bf = Buffer("test");
charlist = crc.crc32(bf).toString();
var k = []
for (var i=0; i< 8; i++) {
k.push(parseInt(charlist[i], 16));
}
var checksum = Buffer(k);
transfer_data = Buffer.concat([bf, checksum]);
////////////////////// Network Transfer /////////////////////////
received_data = transfer_data
checksum = received_data.slice(-8);
charlist = '';
for (var i=0; i<8; i++) {
charlist += checksum.readUInt8(i).toString(16);
}
// these two should be equal if data received correctly
console.log(crc.crc32(received_data.slice(0, -8)));
console.log(charlist)
Output
d87f7e0c
d87f7e0c
I've read the famous article A PAINLESS GUIDE TO CRC ERROR DETECTION ALGORITHMS, now I want to try this out. I'm using a nodejs module node-crc, it looks easy to use:
var crc = require('crc');
crc.crc32('hello');
# => "3610a686"
The result of function crc32
, I believe, is the checksum of hello
using polynomial 0x04C11DB7
. Please correct me if I' wrong.
So the next step is appending the checksum to the original data. Here's my code:
var crc = require('crc');
var bf = Buffer("test");
charlist = crc.crc32(bf).toString();
var k = []
for (var i=0; i< 8; i++) {
k.push(parseInt(charlist[i], 16));
}
console.log(k)
var checksum = Buffer(k);
console.log(crc.crc32(Buffer.concat([bf, checksum])));
output
[ 13, 8, 7, 15, 7, 14, 0, 12 ]
646b4f9b
I'm expecting the second output to be 0
, but it's not.
Therefore I'm here asking for help. What is the correct way to do this(appending and checking if the message was correctly received)? Thank you.
Edit
So I understand that the second output isn't necessarilly 0
. Here's the final solution:
var crc = require('crc');
var bf = Buffer("test");
charlist = crc.crc32(bf).toString();
var k = []
for (var i=0; i< 8; i++) {
k.push(parseInt(charlist[i], 16));
}
var checksum = Buffer(k);
transfer_data = Buffer.concat([bf, checksum]);
////////////////////// Network Transfer /////////////////////////
received_data = transfer_data
checksum = received_data.slice(-8);
charlist = '';
for (var i=0; i<8; i++) {
charlist += checksum.readUInt8(i).toString(16);
}
// these two should be equal if data received correctly
console.log(crc.crc32(received_data.slice(0, -8)));
console.log(charlist)
Output
d87f7e0c
d87f7e0c
Share
edited Jul 10, 2014 at 9:49
laike9m
asked Jul 8, 2014 at 17:23
laike9mlaike9m
19.5k23 gold badges116 silver badges152 bronze badges
3
- 1 "I'm expecting the second output to be 0" Why's that? – Lightness Races in Orbit Commented Jul 8, 2014 at 17:26
-
@LightnessRacesinOrbit From cs.jhu.edu/~scheideler/courses/600.344_S02/CRC.html,
When a message is received the corresponding polynomial is divided by G(x). If the remainder is non-zero, an error is detected. Otherwise, the message is assumed to be correct.
– laike9m Commented Jul 9, 2014 at 2:08 - @LightnessRacesinOrbit I was wrong, I know. – laike9m Commented Jul 9, 2014 at 2:32
1 Answer
Reset to default 3Yes, the crc32
function uses the polynomial you quoted, but it also is pre and post processed by taking the one's plement. That eliminates the property that the (pure) CRC of a message with its CRC appended is zero.
The correct way to check a CRC, which is extensible to any manner of check value, is to simply pute the specified check algorithm on the message, and then pare the result with the check value appended to the message.
本文标签: javascriptHow to use CRC exactlyStack Overflow
版权声明:本文标题:javascript - How to use CRC exactly? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745054130a2639829.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论