Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!samsung!olivea!apple!agate!garnet.berkeley.edu!physi-hf From: physi-hf@garnet.berkeley.edu (Howard Haruo Fukuda) Newsgroups: comp.sys.mac.programmer Subject: Re: Calculating BinHex 4.0's Checksums Message-ID: <1990Dec6.070518.23170@agate.berkeley.edu> Date: 6 Dec 90 07:05:18 GMT References: <5063.275cde88@cc.curtin.edu.au> Sender: usenet@agate.berkeley.edu (USENET Administrator) Organization: University of California, Berkeley Lines: 26 In article <5063.275cde88@cc.curtin.edu.au> Lewis_P@cc.curtin.edu.au (Peter Lewis) writes: >Hi All, > Could someone tell me how the CRC is calculated in a BinHex 4.0 encoded >file? I know the format of .hqx files, and I can decode them, but I can't >calculate and check the CRCs. > >Any information on this would be greatly appreciated, > Peter. BinHex 4.0 uses a 16-bit CRC with a 0x1021 seed. The general algorithm is to take data 1 bit at a time and process it through the following: 1) Take the old CRC (use 0x0000 if there is no previous CRC) and shift it to the left by 1. 2) Put the new data bit in the least significant position (right bit). 3) If the bit shifted out in (1) was a 1 then xor the CRC with 0x1021. 4) Loop back to (1) until all the data has been processed. You should be careful that when BinHex has a 2 Byte location for the CRC (such as at then end of the header), you should feed in 2 bytes of 0x00 before you compare the CRCs. -Howard