[Haskell-beginners] crypto random UUID generation

Ovidiu Deac ovidiudeac at gmail.com
Mon Dec 12 17:52:17 UTC 2016


I have to produce a crypto random UUID.

I haven't found simple examples. and I used the one from hre (see type
CRand)
http://hackage.haskell.org/package/monadcryptorandom-0.7.0/docs/Control-Monad-CryptoRandom.html#v:getCRandomR

My attempt is the following:

cryptoRandomUUID :: IO UUID.UUID
cryptoRandomUUID = do
  g <- newGenIO:: IO SystemRandom
  case runCRand impl g of
    Left err -> throwIO err
    Right (v, g') -> return v

  where impl = do
          w1 <- getCRandom
          w2 <- getCRandom
          w3 <- getCRandom
          w4 <- getCRandom
          return $ UUID.fromWords w1 w2 w3 w4

...but the compilation fails miserably with:

    • Ambiguous type variable ‘e0’ arising from a use of ‘runCRand’
      prevents the constraint ‘(ContainsGenError e0)’ from being solved.
      Relevant bindings include
        impl :: CRandT
                  SystemRandom e0 Data.Functor.Identity.Identity UUID.UUID
          (bound at src/Party.hs:75:9)
      Probable fix: use a type annotation to specify what ‘e0’ should be.
      These potential instance exist:
        instance ContainsGenError GenError
          -- Defined in ‘Control.Monad.CryptoRandom’
    • In the expression: runCRand impl g
      In a stmt of a 'do' block:
        case runCRand impl g of {
          Left err -> throwIO err
          Right (v, g') -> return v }
      In the expression:
        do { g <- newGenIO :: IO SystemRandom;
             case runCRand impl g of {
               Left err -> throwIO err
               Right (v, g') -> return v } }
      ...

What's the problem here?
Are there some good examples for generating crypto-randoms?

Thanks!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20161212/7014f93c/attachment.html>


More information about the Beginners mailing list