summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Data/Digest/CRC24.hs25
1 files changed, 25 insertions, 0 deletions
diff --git a/Data/Digest/CRC24.hs b/Data/Digest/CRC24.hs
new file mode 100644
index 0000000..5c8c27c
--- /dev/null
+++ b/Data/Digest/CRC24.hs
@@ -0,0 +1,25 @@
1-- CRC24.hs: OpenPGP (RFC4880) CRC24 implementation
2-- Copyright Ⓒ 2012 Clint Adams
3-- This software is released under the terms of the Expat (MIT) license.
4-- (See the LICENSE file).
5
6module Data.Digest.CRC24 (
7 crc24
8) where
9
10import Data.Bits (shiftL, (.&.), xor)
11import Data.ByteString (ByteString)
12import qualified Data.ByteString as B
13import Data.Word (Word8, Word32)
14
15crc24_init :: Word32
16crc24_init = 0xB704CE
17
18crc24_poly :: Word32
19crc24_poly = 0x1864CFB
20
21crc24_update :: Word32 -> Word8 -> Word32
22crc24_update c b = (last . take 9 $ iterate (\x -> if (shiftL x 1) .&. 0x1000000 == 0x1000000 then shiftL x 1 `xor` crc24_poly else shiftL x 1) (c `xor` shiftL (fromIntegral b) 16)) .&. 0xFFFFFF
23
24crc24 :: ByteString -> Word32
25crc24 bs = B.foldl crc24_update crc24_init bs