Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wai-app-static: use cryptonite instead of cryptohash #519

Merged
merged 3 commits into from Mar 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions wai-app-static/WaiAppStatic/Storage/Embedded/Runtime.hs
Expand Up @@ -16,8 +16,7 @@ import qualified Data.Text as T
import Data.Ord
import qualified Data.ByteString as S
import Crypto.Hash (hash, MD5, Digest)
import Data.Byteable (toBytes)
import qualified Data.ByteString.Base64 as B64
import Data.ByteArray.Encoding
import WaiAppStatic.Storage.Filesystem (defaultFileServerSettings)
import System.FilePath (isPathSeparator)

Expand Down Expand Up @@ -95,4 +94,4 @@ bsToFile name bs = File
}

runHash :: ByteString -> ByteString
runHash = B64.encode . toBytes . (hash :: S.ByteString -> Digest MD5)
runHash = convertToBase Base64 . (hash :: S.ByteString -> Digest MD5)
16 changes: 9 additions & 7 deletions wai-app-static/WaiAppStatic/Storage/Filesystem.hs
@@ -1,5 +1,6 @@
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE BangPatterns #-}
-- | Access files on the filesystem.
module WaiAppStatic.Storage.Filesystem
( -- * Types
Expand All @@ -12,6 +13,7 @@ module WaiAppStatic.Storage.Filesystem

import WaiAppStatic.Types
import System.FilePath ((</>))
import System.IO (withBinaryFile, IOMode(..))
import System.Directory (doesFileExist, doesDirectoryExist, getDirectoryContents)
import Data.List (foldl')
import Control.Monad (forM)
Expand All @@ -23,10 +25,9 @@ import WaiAppStatic.Listing
import Network.Mime
import System.PosixCompat.Files (fileSize, getFileStatus, modificationTime, isRegularFile)
import Data.Maybe (catMaybes)
import qualified Crypto.Hash.Conduit (hashFile)
import Data.Byteable (toBytes)
import Crypto.Hash (MD5, Digest)
import qualified Data.ByteString.Base64 as B64
import Data.ByteArray.Encoding
import Crypto.Hash (hashlazy, MD5, Digest)
import qualified Data.ByteString.Lazy as BL (hGetContents)
import qualified Data.Text as T

-- | Construct a new path from a root and some @Pieces@.
Expand Down Expand Up @@ -119,9 +120,10 @@ webAppLookup hashFunc prefix pieces =
-- | MD5 hash and base64-encode the file contents. Does not check if the file
-- exists.
hashFile :: FilePath -> IO ByteString
hashFile fp = do
h <- Crypto.Hash.Conduit.hashFile fp
return $ B64.encode $ toBytes (h :: Digest MD5)
hashFile fp = withBinaryFile fp ReadMode $ \h -> do
f <- BL.hGetContents h
let !hash = hashlazy f :: Digest MD5
return $ convertToBase Base64 hash

hashFileIfExists :: ETagLookup
hashFileIfExists fp = do
Expand Down
7 changes: 2 additions & 5 deletions wai-app-static/wai-app-static.cabal
Expand Up @@ -38,9 +38,8 @@ library
, file-embed >= 0.0.3.1
, text >= 0.7
, blaze-builder >= 0.2.1.4
, base64-bytestring >= 0.1
, byteable
, cryptohash >= 0.11
, cryptonite >= 0.6
, memory >= 0.7
, http-date
, blaze-html >= 0.5
, blaze-markup >= 0.5.1
Expand All @@ -52,8 +51,6 @@ library
, wai-extra >= 3.0 && < 3.1
, optparse-applicative >= 0.7
, warp >= 3.0.11 && < 3.3
-- Used exclusively for hashFile function
, cryptohash-conduit

exposed-modules: Network.Wai.Application.Static
WaiAppStatic.Storage.Filesystem
Expand Down