diff options
author | Clint Adams <clint@debian.org> | 2012-03-31 18:53:45 -0400 |
---|---|---|
committer | Clint Adams <clint@debian.org> | 2012-03-31 19:01:12 -0400 |
commit | e295e4669be452c3aa855065cf6816c68d42df7a (patch) | |
tree | 57634089c31ae5597010e48c3496f300445befa3 | |
parent | e4980130c6f880b6cad10cbe26a938ebba6c5075 (diff) |
Implement CRC24 for ASCII armor.
-rw-r--r-- | Data/Digest/CRC24.hs | 25 |
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 | |||
6 | module Data.Digest.CRC24 ( | ||
7 | crc24 | ||
8 | ) where | ||
9 | |||
10 | import Data.Bits (shiftL, (.&.), xor) | ||
11 | import Data.ByteString (ByteString) | ||
12 | import qualified Data.ByteString as B | ||
13 | import Data.Word (Word8, Word32) | ||
14 | |||
15 | crc24_init :: Word32 | ||
16 | crc24_init = 0xB704CE | ||
17 | |||
18 | crc24_poly :: Word32 | ||
19 | crc24_poly = 0x1864CFB | ||
20 | |||
21 | crc24_update :: Word32 -> Word8 -> Word32 | ||
22 | crc24_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 | |||
24 | crc24 :: ByteString -> Word32 | ||
25 | crc24 bs = B.foldl crc24_update crc24_init bs | ||