crc16-xmodem.ts

Simple implementation of crc16-xmodem
git clone git://git.finwo.net/lib/crc16-xmodem.ts
Log | Files | Refs | README | LICENSE

README.md (1293B)


      1 # CRC16-XMODEM
      2 
      3 <small>Generate CRC-16/XMODEM codes consistently</small>
      4 
      5 ## Why
      6 
      7 While a fairly simple task, I was implementing this throughout multiple projects
      8 of mine. Now if there's a bug, speed improvement to be had, or simple because
      9 typescript's typing system changes, I want to be able to update it in 1 location
     10 instead of having to fix it in every individual project.
     11 
     12 TL;DR; I'm lazy, and want a single version of the code
     13 
     14 ## Usage
     15 
     16 This package does only one thing.
     17 Usage is really simple:
     18 
     19 ```ts
     20 import { crc16, crc16b } from '@finwo/crc16-xmodem';
     21 
     22 // Note the 2 zero bytes at the end, they're useful later
     23 const message = Buffer.from('Hi!\0\0');
     24 
     25 // Output 12797 and <Buffer 31 fd>
     26 console.log(crc16(message));
     27 console.log(crc16b(message));
     28 
     29 // Either copy in the check code with buffer manipulation:
     30 const check            = crc16b(message);
     31 const checkableMessage = Buffer.concat([message]);
     32 check.copy(checkableMessage, checkableMessage.length - check.length);
     33 
     34 // Or, be lazy and just copy the crc over the zeroes directly as an uint16be
     35 message.writeUInt16BE(crc16(message), message.length - 2);
     36 
     37 // Now if you want to check if your message has travelled the network without errors:
     38 if (crc16(message) != 0) {
     39   throw new Error('Message got corrupted in transit');
     40 }
     41 ```