PageRenderTime 70ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/cipher-blowfish/Crypto/Cipher/Blowfish.hs

http://github.com/vincenthz/hs-cryptocipher
Haskell | 61 lines | 38 code | 10 blank | 13 comment | 0 complexity | 254b730d39108eaab5bc7a2b0fd26b77 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. {-# LANGUAGE CPP #-}
  2. -- |
  3. -- Module : Crypto.Cipher.Blowfish
  4. -- License : BSD-style
  5. -- Maintainer : Vincent Hanquez <vincent@snarc.org>
  6. -- Stability : stable
  7. -- Portability : good
  8. --
  9. module Crypto.Cipher.Blowfish
  10. ( Blowfish
  11. , Blowfish64
  12. , Blowfish128
  13. , Blowfish256
  14. , Blowfish448
  15. ) where
  16. import Data.Byteable
  17. import Crypto.Cipher.Types
  18. import Crypto.Cipher.Blowfish.Primitive
  19. -- | variable keyed blowfish state
  20. newtype Blowfish = Blowfish Context
  21. -- | 64 bit keyed blowfish state
  22. newtype Blowfish64 = Blowfish64 Context
  23. -- | 128 bit keyed blowfish state
  24. newtype Blowfish128 = Blowfish128 Context
  25. -- | 256 bit keyed blowfish state
  26. newtype Blowfish256 = Blowfish256 Context
  27. -- | 448 bit keyed blowfish state
  28. newtype Blowfish448 = Blowfish448 Context
  29. instance Cipher Blowfish where
  30. cipherName _ = "blowfish"
  31. cipherKeySize _ = KeySizeRange 6 56
  32. cipherInit k = either error Blowfish $ initBlowfish (toBytes k)
  33. instance BlockCipher Blowfish where
  34. blockSize _ = 8
  35. ecbEncrypt (Blowfish bf) = encrypt bf
  36. ecbDecrypt (Blowfish bf) = decrypt bf
  37. #define INSTANCE_CIPHER(CSTR, NAME, KEYSIZE) \
  38. instance Cipher CSTR where \
  39. { cipherName _ = NAME \
  40. ; cipherKeySize _ = KeySizeFixed KEYSIZE \
  41. ; cipherInit k = either error CSTR $ initBlowfish (toBytes k) \
  42. }; \
  43. instance BlockCipher CSTR where \
  44. { blockSize _ = 8 \
  45. ; ecbEncrypt (CSTR bf) = encrypt bf \
  46. ; ecbDecrypt (CSTR bf) = decrypt bf \
  47. };
  48. INSTANCE_CIPHER(Blowfish64, "blowfish64", 8)
  49. INSTANCE_CIPHER(Blowfish128, "blowfish128", 16)
  50. INSTANCE_CIPHER(Blowfish256, "blowfish256", 32)
  51. INSTANCE_CIPHER(Blowfish448, "blowfish448", 56)