[Haskell-beginners] crypto random UUID generation
David McBride
toad3k at gmail.com
Mon Dec 12 18:15:09 UTC 2016
The problem is with
Left err -> throwIO err
Because of the type of 'runCRand', we know err is an instance of
ContainsGenError e0, but which one? We need a concrete error type before
we can run this code. Looking at the docs there seems to be only one
instance of ContainsGenError, GenError, so a quick an dirty solution would
be to change it to
Left err -> throwIO (err :: GenError) -- should work
But keep in mind, if there were any other ContainsGenError instances, like
from an external library that is adding a new type of random generator to
this library that fails in a new way, you would not be catching that.
On Mon, Dec 12, 2016 at 12:52 PM, Ovidiu Deac <ovidiudeac at gmail.com> wrote:
> 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!
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20161212/cac008e6/attachment.html>
More information about the Beginners
mailing list