packages feed

crypton-2.3.1 / Crypto / Number / Serialize.hs

{-# LANGUAGE BangPatterns #-}-- |-- Module      : Crypto.Number.Serialize-- License     : BSD-style-- Maintainer  : Vincent Hanquez <vincent@snarc.org>-- Stability   : experimental-- Portability : Good---- Fast serialization primitives for integermodule Crypto.Number.Serialize (    i2osp,    os2ip,    i2ospOf,    i2ospOf_,) whereimport qualified Crypto.Internal.ByteArray as Bimport Crypto.Internal.Compat (unsafeDoIO)import Crypto.Number.Basicimport qualified Crypto.Number.Serialize.Internal as Internal-- | @os2ip@ converts a byte string into a positive integer.os2ip :: B.ByteArrayAccess ba => ba -> Integeros2ip bs = unsafeDoIO $ B.withByteArray bs (\p -> Internal.os2ip p (B.length bs))-- | @i2osp@ converts a positive integer into a byte string.---- The first byte is MSB (most significant byte); the last byte is the LSB (least significant byte)i2osp :: B.ByteArray ba => Integer -> bai2osp 0 = B.allocAndFreeze 1 (\p -> Internal.i2osp 0 p 1 >> return ())i2osp m = B.allocAndFreeze sz (\p -> Internal.i2osp m p sz >> return ())  where    !sz = numBytes m-- | Just like 'i2osp', but takes an extra parameter for size.-- If the number is too big to fit in @len@ bytes, 'Nothing' is returned-- otherwise the number is padded with 0 to fit the @len@ required.{-# INLINEABLE i2ospOf #-}i2ospOf :: B.ByteArray ba => Int -> Integer -> Maybe bai2ospOf len m    | len <= 0 = Nothing    | m < 0 = Nothing    | sz > len = Nothing    | otherwise =        Just $ B.unsafeCreate len (\p -> Internal.i2ospOf m p len >> return ())  where    !sz = numBytes m-- | Just like 'i2ospOf' except that it doesn't expect a failure: i.e.-- an integer larger than the number of output bytes requested.---- For example if you just took a modulo of the number that represent-- the size (example the RSA modulo n).i2ospOf_ :: B.ByteArray ba => Int -> Integer -> bai2ospOf_ len = maybe (error "i2ospOf_: integer is larger than expected") id . i2ospOf len