packages feed

random-string (empty) → 0.1.0.1

raw patch · 5 files changed

+90/−0 lines, 5 filesdep +basedep +base16-bytestringdep +base58-bytestringsetup-changed

Dependencies added: base, base16-bytestring, base58-bytestring, base64-bytestring, bytestring, entropy

Files

+ ChangeLog.md view
@@ -0,0 +1,5 @@+# Revision history for random-string++## 0.1.0.0 -- YYYY-mm-dd++* First version. Released on an unsuspecting world.
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2018, Thomas M. DuBuisson++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 Thomas M. DuBuisson 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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ random-string.cabal view
@@ -0,0 +1,30 @@+-- Initial random-string.cabal generated by cabal init.  For further +-- documentation, see http://haskell.org/cabal/users-guide/++name:                random-string+version:             0.1.0.1+synopsis:            Generate a random base 16, 58, or 64 string+description:         Uses the fastest strong random available for a random+                     string of the given base and length (in bytes of entropy).+license:             BSD3+license-file:        LICENSE+author:              Thomas M. DuBuisson+maintainer:          tommd@galois.com+copyright:           Galois 2018+category:            Data+build-type:          Simple+extra-source-files:  ChangeLog.md+cabal-version:       >=1.10++library+  exposed-modules:     System.RandomString+  -- other-modules:       +  -- other-extensions:    +  build-depends:       base >=4.10 && <5+                     , bytestring >=0.10 && <0.11+                     , base16-bytestring+                     , base58-bytestring+                     , base64-bytestring+                     , entropy >= 0.4 && < 0.5+  hs-source-dirs:      src+  default-language:    Haskell2010
+ src/System/RandomString.hs view
@@ -0,0 +1,23 @@+module System.RandomString where++import           Data.String (IsString(..))+import qualified Data.ByteString.Char8 as B8+import qualified Data.ByteString.Base58 as Base58+import qualified Data.ByteString.Base64 as Base64+import qualified Data.ByteString.Base16 as Base16+import           Control.Monad.IO.Class+import           System.Entropy++data StringOpts = StringOpts { alphabet :: Alphabet, nrBytes :: Int }+data Alphabet = Base58 | Base16 | Base64++randomString :: (MonadIO m, IsString s) => StringOpts -> m s+randomString opts =+    let getE n = maybe (getEntropy n) pure =<< getHardwareEntropy n+        conv = fromString . B8.unpack+        enc = case alphabet opts of+                Base58 -> Base58.encodeBase58 Base58.bitcoinAlphabet+                Base16 -> Base16.encode+                Base64 -> Base64.encode+    in (conv . enc) <$> liftIO (getE (nrBytes opts))+