Skip to content

Commit

Permalink
Replace cryptohash with cryptonite (#313)
Browse files Browse the repository at this point in the history
Fixes #305
  • Loading branch information
PierreR authored and Gabriella439 committed Mar 1, 2018
1 parent da41f8c commit 5d20129
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 52 deletions.
24 changes: 12 additions & 12 deletions default.nix
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{ mkDerivation, ansi-terminal, ansi-wl-pprint, base
, base16-bytestring, bytestring, case-insensitive, charset
, containers, contravariant, cryptohash, deepseq, directory
, exceptions, filepath, haskeline, http-client, http-client-tls
, insert-ordered-containers, lens-family-core, mtl
, base16-bytestring, bytestring, case-insensitive, containers
, contravariant, cryptonite, deepseq, directory, exceptions
, filepath, haskeline, http-client, http-client-tls
, insert-ordered-containers, lens-family-core, memory, mtl
, optparse-generic, parsers, prettyprinter
, prettyprinter-ansi-terminal, repline, scientific, stdenv, tasty
, tasty-hunit, text, text-format, transformers, trifecta
Expand All @@ -15,20 +15,20 @@ mkDerivation {
isLibrary = true;
isExecutable = true;
libraryHaskellDepends = [
ansi-terminal ansi-wl-pprint base base16-bytestring bytestring
case-insensitive charset containers contravariant cryptohash
directory exceptions filepath http-client http-client-tls
insert-ordered-containers lens-family-core parsers prettyprinter
ansi-wl-pprint base base16-bytestring bytestring case-insensitive
containers contravariant cryptonite directory exceptions filepath
http-client http-client-tls insert-ordered-containers
lens-family-core memory parsers prettyprinter
prettyprinter-ansi-terminal scientific text text-format
transformers trifecta unordered-containers vector
];
executableHaskellDepends = [
ansi-terminal base containers haskeline mtl optparse-generic
prettyprinter prettyprinter-ansi-terminal repline text trifecta
ansi-terminal base haskeline mtl optparse-generic prettyprinter
prettyprinter-ansi-terminal repline text trifecta
];
testHaskellDepends = [
base containers deepseq insert-ordered-containers prettyprinter
tasty tasty-hunit text vector
base deepseq insert-ordered-containers prettyprinter tasty
tasty-hunit text vector
];
description = "A configuration language guaranteed to terminate";
license = stdenv.lib.licenses.bsd3;
Expand Down
5 changes: 3 additions & 2 deletions dhall.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,15 @@ Library
case-insensitive < 1.3 ,
containers >= 0.5.0.0 && < 0.6 ,
contravariant < 1.5 ,
cryptohash < 0.12,
exceptions >= 0.8.3 && < 0.10 ,
cryptonite >= 0.23 && < 1.0 ,
exceptions >= 0.8.3 && < 0.10,
directory >= 1.3 && < 1.4 ,
filepath >= 1.4 && < 1.5 ,
http-client >= 0.4.30 && < 0.6 ,
http-client-tls >= 0.2.0 && < 0.4 ,
insert-ordered-containers >= 0.1.0.1 && < 0.3 ,
lens-family-core >= 1.0.0 && < 1.3 ,
memory >= 0.14 && < 0.15,
parsers >= 0.12.4 && < 0.13,
prettyprinter >= 1.2.0.1 && < 1.3 ,
prettyprinter-ansi-terminal >= 1.1.1 && < 1.2 ,
Expand Down
27 changes: 11 additions & 16 deletions src/Dhall/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ module Dhall.Core (
import Control.Applicative (Applicative(..), (<$>))
#endif
import Control.Applicative (empty)
import Crypto.Hash (SHA256)
import Data.Bifunctor (Bifunctor(..))
import Data.Foldable
import Data.HashMap.Strict.InsOrd (InsOrdHashMap)
Expand All @@ -70,9 +71,7 @@ import Numeric.Natural (Natural)
import Prelude hiding (succ)

import qualified Control.Monad
import qualified Data.ByteString
import qualified Data.ByteString.Char8
import qualified Data.ByteString.Base16
import qualified Crypto.Hash
import qualified Data.HashMap.Strict.InsOrd
import qualified Data.HashSet
import qualified Data.Sequence
Expand Down Expand Up @@ -137,17 +136,13 @@ data PathMode = Code | RawText deriving (Eq, Ord, Show)

-- | A `PathType` extended with an optional hash for semantic integrity checks
data PathHashed = PathHashed
{ hash :: Maybe Data.ByteString.ByteString
{ hash :: Maybe (Crypto.Hash.Digest SHA256)
, pathType :: PathType
} deriving (Eq, Ord, Show)

instance Buildable PathHashed where
build (PathHashed Nothing p) = build p
build (PathHashed (Just h) p) = build p <> "sha256:" <> build string <> " "
where
bytes = Data.ByteString.Base16.encode h

string = Data.ByteString.Char8.unpack bytes
build (PathHashed (Just h) p) = build p <> "sha256:" <> build (show h) <> " "

-- | Path to an external resource
data Path = Path
Expand Down Expand Up @@ -1201,18 +1196,18 @@ denote (Embed a ) = Embed a

{-| Reduce an expression to its normal form, performing beta reduction and applying
any custom definitions.
`normalizeWith` is designed to be used with function `typeWith`. The `typeWith`
function allows typing of Dhall functions in a custom typing context whereas
`normalizeWith` allows evaluating Dhall expressions in a custom context.
function allows typing of Dhall functions in a custom typing context whereas
`normalizeWith` allows evaluating Dhall expressions in a custom context.
To be more precise `normalizeWith` applies the given normalizer when it finds an
application term that it cannot reduce by other means.
Note that the context used in normalization will determine the properties of normalization.
That is, if the functions in custom context are not total then the Dhall language, evaluated
with those functions is not total either.
with those functions is not total either.
-}
normalizeWith :: Normalizer a -> Expr s a -> Expr t a
normalizeWith ctx e0 = loop (denote e0)
Expand Down Expand Up @@ -1565,8 +1560,8 @@ normalizeWith ctx e0 = loop (denote e0)
type Normalizer a = forall s. Expr s a -> Maybe (Expr s a)

-- | Check if an expression is in a normal form given a context of evaluation.
-- Unlike `isNormalized`, this will fully normalize and traverse through the expression.
--
-- Unlike `isNormalized`, this will fully normalize and traverse through the expression.
--
-- It is much more efficient to use `isNormalized`.
isNormalizedWith :: (Eq s, Eq a) => Normalizer a -> Expr s a -> Bool
isNormalizedWith ctx e = e == (normalizeWith ctx e)
Expand Down
32 changes: 15 additions & 17 deletions src/Dhall/Import.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,27 +71,27 @@
> $ export BAZ='λ(x : Bool) → x == False'
> $ dhall <<< "{ foo = env:FOO , bar = env:BAR , baz = env:BAZ }"
> { bar : Text, baz : ∀(x : Bool) → Bool, foo : Integer }
>
>
> { bar = "Hi", baz = λ(x : Bool) → x == False, foo = 1 }
If you wish to import the raw contents of a path as @Text@ then add
@as Text@ to the end of the import:
> $ dhall <<< "http://example.com as Text"
> Text
>
>
> "<!doctype html>\n<html>\n<head>\n <title>Example Domain</title>\n\n <meta
> charset=\"utf-8\" />\n <meta http-equiv=\"Content-type\" content=\"text/html
> ; charset=utf-8\" />\n <meta name=\"viewport\" content=\"width=device-width,
> ; charset=utf-8\" />\n <meta name=\"viewport\" content=\"width=device-width,
> initial-scale=1\" />\n <style type=\"text/css\">\n body {\n backgro
> und-color: #f0f0f2;\n margin: 0;\n padding: 0;\n font-famil
> y: \"Open Sans\", \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n \n
> y: \"Open Sans\", \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n \n
> }\n div {\n width: 600px;\n margin: 5em auto;\n paddi
> ng: 50px;\n background-color: #fff;\n border-radius: 1em;\n }\n
> a:link, a:visited {\n color: #38488f;\n text-decoration: none;
> \n }\n @media (max-width: 700px) {\n body {\n background
> -color: #fff;\n }\n div {\n width: auto;\n m
> argin: 0 auto;\n border-radius: 0;\n padding: 1em;\n
> argin: 0 auto;\n border-radius: 0;\n padding: 1em;\n
> }\n }\n </style> \n</head>\n\n<body>\n<div>\n <h1>Example Domain</
> h1>\n <p>This domain is established to be used for illustrative examples in d
> ocuments. You may use this\n domain in examples without prior coordination or
Expand Down Expand Up @@ -124,6 +124,7 @@ import Control.Monad (join)
import Control.Monad.Catch (throwM, MonadCatch(catch))
import Control.Monad.IO.Class (MonadIO(..))
import Control.Monad.Trans.State.Strict (StateT)
import Crypto.Hash (SHA256)
import Data.ByteString.Lazy (ByteString)
import Data.CaseInsensitive (CI)
import Data.Map (Map)
Expand Down Expand Up @@ -160,9 +161,9 @@ import Text.Trifecta (Result(..))
import Text.Trifecta.Delta (Delta(..))

import qualified Control.Monad.Trans.State.Strict as State
import qualified Crypto.Hash.SHA256
import qualified Crypto.Hash
import qualified Data.ByteArray
import qualified Data.ByteString
import qualified Data.ByteString.Base16
import qualified Data.ByteString.Char8
import qualified Data.ByteString.Lazy
import qualified Data.CaseInsensitive
Expand Down Expand Up @@ -534,8 +535,8 @@ instance Exception InternalError

-- | Exception thrown when an integrity check fails
data HashMismatch = HashMismatch
{ expectedHash :: Data.ByteString.ByteString
, actualHash :: Data.ByteString.ByteString
{ expectedHash :: Crypto.Hash.Digest SHA256
, actualHash :: Crypto.Hash.Digest SHA256
} deriving (Typeable)

instance Exception HashMismatch
Expand All @@ -547,14 +548,11 @@ instance Show HashMismatch where
<> "\n"
<> "Expected hash:\n"
<> "\n"
<> "" <> toString expectedHash <> "\n"
<> "" <> show expectedHash <> "\n"
<> "\n"
<> "Actual hash:\n"
<> "\n"
<> "" <> toString actualHash <> "\n"
where
toString =
Data.ByteString.Char8.unpack . Data.ByteString.Base16.encode
<> "" <> show actualHash <> "\n"

parseFromFileEx
:: Text.Trifecta.Parser a
Expand Down Expand Up @@ -849,8 +847,8 @@ load :: Expr Src Path -> IO (Expr Src X)
load = loadWithContext Dhall.Context.empty

-- | Hash a fully resolved expression
hashExpression :: Expr s X -> Data.ByteString.ByteString
hashExpression expr = Crypto.Hash.SHA256.hashlazy actualBytes
hashExpression :: Expr s X -> (Crypto.Hash.Digest SHA256)
hashExpression expr = Crypto.Hash.hashlazy actualBytes
where
text = Dhall.Core.pretty (Dhall.Core.normalize expr)
actualBytes = Data.Text.Lazy.Encoding.encodeUtf8 text
Expand All @@ -866,7 +864,7 @@ hashExpressionToCode expr = "sha256:" <> lazyText
where
bytes = hashExpression expr

bytes16 = Data.ByteString.Base16.encode bytes
bytes16 = Data.ByteArray.convert bytes

-- Notes that `decodeUtf8` is partial, but the base16-encoded bytestring
-- should always successfully decode
Expand Down
9 changes: 4 additions & 5 deletions src/Dhall/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import Text.Trifecta
import Text.Trifecta.Delta (Delta)

import qualified Control.Monad
import qualified Data.ByteString.Base16.Lazy
import qualified Crypto.Hash
import qualified Data.ByteString.Lazy
import qualified Data.Char
import qualified Data.HashMap.Strict.InsOrd
Expand Down Expand Up @@ -1509,10 +1509,9 @@ pathHashed_ = do
whitespace
let lazyText = Data.Text.Lazy.Builder.toLazyText builder
let lazyBytes = Data.Text.Lazy.Encoding.encodeUtf8 lazyText
let (hash, suffix) = Data.ByteString.Base16.Lazy.decode lazyBytes
if Data.ByteString.Lazy.null suffix
then return (Data.ByteString.Lazy.toStrict hash)
else fail "Invalid sha256 hash"
case Crypto.Hash.digestFromByteString (Data.ByteString.Lazy.toStrict lazyBytes) of
Nothing -> fail "Invalid sha256 hash"
Just h -> pure h

import_ :: Parser Path
import_ = (do
Expand Down

0 comments on commit 5d20129

Please sign in to comment.