{-
- ``Data/Random/Source/DevRandom''
-}{-# LANGUAGE
MultiParamTypeClasses, GADTs
#-}moduleData.Random.Source.DevRandom(DevRandom(..))whereimportData.Random.SourceimportData.Random.Internal.PrimitivesimportSystem.IO(openBinaryFile,hGetBuf,Handle,IOMode(..))importForeign-- |On systems that have it, \/dev\/random is a handy-dandy ready-to-use source-- of nonsense. Keep in mind that on some systems, Linux included, \/dev\/random-- collects \"real\" entropy, and if you don't have a good source of it, such as-- special hardware for the purpose or a *lot* of network traffic, it's pretty easy-- to suck the entropy pool dry with entropy-intensive applications. For many-- purposes other than cryptography, \/dev\/urandom is preferable because when it-- runs out of real entropy it'll still churn out pseudorandom data.dataDevRandom=DevRandom|DevURandomderiving(Eq,Show){-# NOINLINE devRandom #-}devRandom::HandledevRandom=unsafePerformIO(openBinaryFile"/dev/random"ReadMode){-# NOINLINE devURandom #-}devURandom::HandledevURandom=unsafePerformIO(openBinaryFile"/dev/urandom"ReadMode)dev::DevRandom->HandledevDevRandom=devRandomdevDevURandom=devURandominstanceRandomSourceIODevRandomwheregetRandomPrimFromsrc=getPrimWheresupportedgetPrimwheresupported::Prima->BoolsupportedPrimWord8=TruesupportedPrimWord16=TruesupportedPrimWord32=TruesupportedPrimWord64=Truesupported_=FalsegetPrim::Prima->IOagetPrimPrimWord8=allocaBytes1$\buf->do1<-hGetBuf(devsrc)buf1peekbufgetPrimPrimWord16=allocaBytes2$\buf->do2<-hGetBuf(devsrc)buf2peek(castPtrbuf)getPrimPrimWord32=allocaBytes4$\buf->do4<-hGetBuf(devsrc)buf4peek(castPtrbuf)getPrimPrimWord64=allocaBytes8$\buf->do8<-hGetBuf(devsrc)buf8peek(castPtrbuf)getPrimprim=error("getRandomPrimFrom/"++showsrc++": unsupported prim requested: "++showprim)