PageRenderTime 76ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/README.md

http://github.com/vincenthz/hs-cryptocipher
Markdown | 76 lines | 50 code | 26 blank | 0 comment | 0 complexity | 796c2444887fdd87031277428dd3490c MD5 | raw file
Possible License(s): BSD-3-Clause
  1. crypto-cipher suite
  2. ===================
  3. Documentation: [crypto-cipher-types on hackage](http://hackage.haskell.org/package/crypto-cipher-types)
  4. Using ciphers
  5. --------------
  6. Here a simple example on how to encrypt using ECB, with the AES256 cipher:
  7. import Crypto.Cipher.Types
  8. import Crypto.Cipher
  9. -- the bytestring need to have a length of 32 bytes
  10. -- otherwise the simplified error handling will raise an exception.
  11. initAES256 :: ByteString -> AES256
  12. initAES256 = either (error . show) cipherInit . makeKey
  13. -- real code would not create a new context every time, but
  14. -- initialize once, and reuse the context.
  15. cryptKey key msg = encryptECB ctx msg
  16. where ctx = initAES256 key
  17. And another using CBC mode with Blowfish cipher:
  18. import Crypto.Cipher.Types
  19. import Crypto.Cipher
  20. initBlowfish :: ByteString -> Blowfish
  21. initBlowfish = either (error . show) cipherInit . makeKey
  22. cryptKey key iv msg = encryptCBC ctx (makeIV iv) msg
  23. where ctx = initBlowfish key
  24. Phantom types
  25. -------------
  26. [crypto-cipher-types](http://hackage.haskell.org/package/crypto-cipher-types) use
  27. Phantom types for Keys and IVs, which are all the same underlaying types but allow
  28. to differentiate between valid keys of differents ciphers.
  29. For example a "Key Blowfish" is different than a "Key AES256". This is similar for IV.
  30. One must use makeIV and makeKey to create those types.
  31. makeKey "\x00\x11\x22\x33\x44\x55\x66" :: Either KeyError (Key MyCipher)
  32. and:
  33. makeIV "\x00\x11\x22\x33\x44" :: Maybe (IV MyCipher)
  34. In simple context, the haskell compiler cannot infer the cipher that need to be
  35. use, and the user need to add annotation in signatures as to which cipher need
  36. to be chosen.
  37. This only need to be done, either on the initialized Cipher (cipherInit),
  38. the Key or the IV.
  39. Writing tests
  40. -------------
  41. Tests for blockciphers are already all included in crypto-cipher-tests.
  42. import Crypto.Cipher.Tests
  43. main = defaultMain
  44. [ testBlockCipher defaultKATs (undefined :: BlockCipherType)
  45. ]
  46. TODO
  47. ----
  48. * cipher-des: slow
  49. * cipher-blowfish: slow
  50. * cipher-camellia: slow, endianness problem