Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6925325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:46:31+00:00 2026-05-27T10:46:31+00:00

There is an Crypto.Random API inside the crypto-api package that specifies what it means

  • 0

There is an Crypto.Random API inside the crypto-api package that specifies what it means for something to be a “pseudorandom number generator”.

I have implemented this API using an instance of System.Random’s RandomGen class, namely, StdGen:

instance CryptoRandomGen StdGen where
  newGen bs = Right $ mkStdGen $ shift e1 24 + shift e2 16 + shift e3 8 + e4
    where (e1 : e2 : e3 : e4 : _) = Prelude.map fromIntegral $ unpack bs
  genSeedLength = Tagged 4
  genBytes n g = Right $ genBytesHelper n empty g
    where genBytesHelper 0 partial gen = (partial, gen)
          genBytesHelper n partial gen = genBytesHelper (n-1) (partial `snoc` nextitem) newgen
            where (nextitem, newgen) = randomR (0, 255) gen
  reseed bs _ = newGen bs

However, this implementation is only for the StdGen type, but it would really work for anything in System.Random’s RandomGen typeclass.

Is there a way to say that everything in RandomGen is a member of CryptoRandomGen using the given shim functions? I’d like to be able to do this in my own code, without having to change the source of either of those two libraries. My instincts would be to change the first line to something like

instance (RandomGen a) => CryptoRandomGen a where

but that doesn’t appear to be syntactically correct.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:46:32+00:00Added an answer on May 27, 2026 at 10:46 am

    Crypto-API author here. Please don’t do this – it’s really a violation of the implicit properties of CryptoRandomGen.

    That said, here’s how I’d do it: Just make a newtype that wraps your RandomGen and make that newtype an instance of CryptoRandomGen.

    newtype AsCRG g = ACRG { unACRG :: g}
    
    instance RandomGen g => CryptoRandomGen (AsCRG g) where
        newGen = -- This is not possible to implement with only a 'RandomGen' constraint.  Perhaps you want a 'Default' instance too?
        genSeedLength = -- This is also not possible from just 'RandomGen'
        genBytes nr g =
            let (g1,g2) = split g
                randInts :: [Word32]
                randInts = B.concat . map Data.Serialize.encode
                         . take ((nr + 3) `div` 4)
                         $ (randoms g1 :: [Word32])
            in (B.take nr randInts, g2)
        reseed _ _ = -- not possible w/o more constraints
        newGenIO = -- not possible w/o more constraints
    

    So you see, you can split the generator (or manage many intermediate generators), make the right number of Ints (or in my case, Word32s), encode them, and return the bytes.

    Because RandomGen is limited to just generation (and splitting), there isn’t any straight-forward way to support instatiation, reinstantiation, or querying properties such as the seed length.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the Erlang crypto library, there is no aes_cfb_ivec function. Does it mean that
Is there a way to generate random number on Windows by reading from a
Why would anybody use the standard random number generator from System.Random at all instead
Is it as simple as using FIPS 140 compliant crypto providers or is there
There is a conversion process that is needed when migrating Visual Studio 2005 web
There are numerous Agile software development methods. Which ones have you used in practice
In this question Erik needs to generate a secure random token in Node.js. There's
I'm working on a project that uses pkg_crypto to protect users' personal information. There
I need to implement some crypto protocol on C# and want to say that
I am trying to test the crypto library that comes with openssl, I downloaded

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.