2

I'm trying to hash text using Crypto.Hash modules on replit.com, I don't know how to resolve this failure: Could not find module ‘Crypto.Hash’

Code:

import Crypto.Hash (hashWith, SHA256(..))
import Data.ByteString (ByteString)


main = do
  putStrLn "Hello"
  putStrLn "World"
  hashWith SHA256 ("hello" :: ByteString)

I will be very grateful for your help!!!

  • 2
    Haskell / GHC doesn't come with any crypto stuff. The `Crypto.Hash` module you want is (probably) the one from [the `cryptonite` library](https://hackage.haskell.org/package/cryptonite-0.29/docs/Crypto-Hash.html), so you need to depend on that package. Not sure if that's possible on replit. – leftaroundabout Mar 01 '22 at 17:10

1 Answers1

4

It looks like replit.com is Nix based, and it uses a replit.nix configuration file to configure the Nix environment, including loaded GHC packages.

So, one way to get this working is to edit the replit.nix file. (By default, it won't be shown in the "Files" tab, but you can click the vertical "..." in the top-right corner and select "Show hidden files" to view it.) Modify it to look something like:

{ pkgs }: {
    deps = [
        (pkgs.haskellPackages.ghcWithPackages (pkgs: [
          pkgs.cryptonite
        ]))
        pkgs.haskell-language-server
    ];
}

Now, when you run the source, it should reconfigure the Nix environment and load the required cryptonite package. You may need to modify your code slightly, too, as it uses an OverloadedStrings extension, and hashWith isn't an IO action. I got the following Main.hs to work:

{-# LANGUAGE OverloadedStrings #-}

import Crypto.Hash (hashWith, SHA256(..))
import Data.ByteString (ByteString)

main = do
  print $ hashWith SHA256 ("hello" :: ByteString)
K. A. Buhr
  • 45,621
  • 3
  • 45
  • 71