crc16/crc16.lua

Wed, 16 Feb 2011 20:29:33 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Wed, 16 Feb 2011 20:29:33 +0000
changeset 0
598d09faf89c
permissions
-rw-r--r--

There are no secrets better kept than the secrets that everybody guesses.

0
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local bit = require "bit";
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 module "crc16"
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 function hash(str)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local crc;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local function initCrc()
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 crc = 0xffff;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local function updCrc(byte)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 crc = bit.bxor(crc, byte);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 for i=1,8 do
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local j = bit.band(crc, 1);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 crc = bit.rshift(crc, 1);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 if j ~= 0 then
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 crc = bit.bxor(crc, 0x8408);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 local function getCrc(str)
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 initCrc();
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 for i = 1, #str do
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 updCrc(str:byte(i));
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 return crc;
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 return getCrc(str);
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 end
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32
598d09faf89c There are no secrets better kept than the secrets that everybody guesses.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 return { hash = hash }

mercurial