nonce-1.0.7/0000755000000000000000000000000013251115767011017 5ustar0000000000000000nonce-1.0.7/LICENSE0000644000000000000000000000306013251115767012023 0ustar0000000000000000Copyright (c) 2014, Prowdsponsor All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Felipe Lessa nor the names of other contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. nonce-1.0.7/Setup.hs0000644000000000000000000000005613251115767012454 0ustar0000000000000000import Distribution.Simple main = defaultMain nonce-1.0.7/nonce.cabal0000644000000000000000000000265513251115767013115 0ustar0000000000000000name: nonce version: 1.0.7 synopsis: Generate cryptographic nonces. homepage: https://github.com/prowdsponsor/nonce license: BSD3 license-file: LICENSE author: Felipe Lessa maintainer: Prowdsponsor copyright: (c) 2014 Prowdsponsor category: Cryptography build-type: Simple cabal-version: >= 1.10 description: According to the Wikipedia, a nonce is an arbitrary number used only once in a cryptographic communication. This package contain helper functions for generating nonces. . There are many kinds of nonces used in different situations. It's not guaranteed that by using the nonces from this package you won't have any security issues. Please make sure that the nonces generated via this package are usable on your design. source-repository head type: git location: git://github.com/prowdsponsor/nonce.git library exposed-modules: Crypto.Nonce build-depends: base >= 4.5 && < 5 , base64-bytestring == 1.0.* , bytestring >= 0.9 , text >= 0.9 , transformers >= 0.2 , entropy >= 0.3.7 && < 0.4.2 , unliftio , unliftio-core hs-source-dirs: src/ default-language: Haskell2010 ghc-options: -Wall default-extensions: DeriveDataTypeable nonce-1.0.7/src/0000755000000000000000000000000013251115767011606 5ustar0000000000000000nonce-1.0.7/src/Crypto/0000755000000000000000000000000013251115767013066 5ustar0000000000000000nonce-1.0.7/src/Crypto/Nonce.hs0000644000000000000000000000566613251115767014501 0ustar0000000000000000-- | Usage of this module is very simple. Here is a sample GHCi run: -- -- @ -- *Crypto.Nonce> g <- new -- *Crypto.Nonce> nonce128 g -- \"c\\164\\252\\162f\\207\\245\\ESC`\\180p\\DC4\\234\\223QP\" -- *Crypto.Nonce> nonce128 g -- \"\\203C\\190\\138aI\\158\\194\\146\\&7\\208\\&7\\ETX0\\f\\229\" -- *Crypto.Nonce> nonce128url g -- \"3RP-iEFT-6NrpCMsxigondMC\" -- *Crypto.Nonce> nonce128url g -- \"MVZH3Gi5zSKXJY-_qdtznxla\" -- *Crypto.Nonce> nonce128url g -- \"3f3cVNfuZT62-uGco1CBThci\" -- *Crypto.Nonce> nonce128urlT g -- \"iGMJyrRkw2QMp09SRy59s4Jx\" -- *Crypto.Nonce> nonce128urlT g -- \"WsHs0KwYiex3tsqQZ8b0119_\" -- *Crypto.Nonce> nonce128urlT g -- \"JWkLSX7qSFGu1Q3PHuExwurF\" -- @ -- -- The functions that generate nonces are not pure on purpose, -- since that makes it a lot harder to reuse the same nonce. module Crypto.Nonce ( Generator , new , delete , withGenerator , nonce128 , nonce128url , nonce128urlT ) where import Control.Monad (liftM) import Control.Monad.IO.Class (MonadIO, liftIO) import qualified System.Entropy as Entropy import Data.Typeable (Typeable) import Control.Monad.IO.Unlift (MonadUnliftIO) import UnliftIO.Exception (bracket) import qualified Data.ByteString as B import qualified Data.ByteString.Base64.URL as B64URL import qualified Data.Text as T import qualified Data.Text.Encoding as TE -- | An encapsulated nonce generator. newtype Generator = G Entropy.CryptHandle deriving (Typeable) instance Show Generator where show _ = "" -- | Create a new nonce generator using the system entropy. new :: MonadIO m => m Generator new = liftM G . liftIO $ Entropy.openHandle -- | Release the given generator's resources. The generator won't be -- usable afterwards. delete :: MonadIO m => Generator -> m () delete (G v) = liftIO $ Entropy.closeHandle v -- | An exception-safe convenience function. -- -- @ -- withGenerator = bracket new delete -- @ -- withGenerator :: MonadUnliftIO m => (Generator -> m a) -> m a withGenerator = bracket new delete -- | (Internal) Generate the given number of bytes from the DRG. genBytes :: MonadIO m => Int -> Generator -> m B.ByteString genBytes n (G v) = liftIO $ Entropy.hGetEntropy v n -- | Generate a 128 bit nonce as a 'B.ByteString' of 16 bytes. -- Each byte may have any value from @0@ to @255@. nonce128 :: MonadIO m => Generator -> m B.ByteString nonce128 = genBytes 16 -- | Generate a 128 bit nonce as a 'B.ByteString' of 24 bytes. -- Each byte is either a letter (upper or lowercase), a digit, a -- dash (@-@) or an underscore (@_@), which is the set of -- characters from the base64url encoding. In order to avoid any -- issues with padding, the generated nonce actually has 144 bits. nonce128url :: MonadIO m => Generator -> m B.ByteString nonce128url = liftM B64URL.encode . genBytes 18 -- | Same as 'nonce128url', but returns its result as 'T.Text' -- instead. nonce128urlT :: MonadIO m => Generator -> m T.Text nonce128urlT = liftM TE.decodeUtf8 . nonce128url