Search code examples
haskellrandomcryptographyrsa

Haskell RSA crypto random generator type


My goal is to generate a keypair using generateKeyPair from Codec.Crypto.RSA.

This function requires a generator of type RandomGen. I have tried to make this generator with newGenIO, but I'm having a hard time specifying its type.

import qualified Codec.Crypto.RSA as Crypto
import Crypto.Random

someFunc = do
  let g = newGenIO :: CryptoRandomGen (IO SystemRandom)
  let keyPair = Crypto.generateKeyPair g 10

Haskell suggested CryptoRandomGen SystemRandom when I didn't specify a type after newGenIO.

This is the error message that comes when trying to run the code.

    • Expected a type, but
      ‘CryptoRandomGen (IO SystemRandom)’ has kind
      ‘Constraint’

Does anyone know what type I have use to get it to work?


Solution

  • Try replacing that line with this one:

      g <- newGenIO :: IO SystemRandom
    

    When you want a specific instance of a class, you just use the type, rather than specifying the class again. And to get something out of IO, you need to bind into it rather than just using =.