Skip to content

Commit 5d20129

Browse files
PierreRGabriella439
authored andcommitted
Replace cryptohash with cryptonite (#313)
Fixes #305
1 parent da41f8c commit 5d20129

File tree

5 files changed

+45
-52
lines changed

5 files changed

+45
-52
lines changed

default.nix

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{ mkDerivation, ansi-terminal, ansi-wl-pprint, base
2-
, base16-bytestring, bytestring, case-insensitive, charset
3-
, containers, contravariant, cryptohash, deepseq, directory
4-
, exceptions, filepath, haskeline, http-client, http-client-tls
5-
, insert-ordered-containers, lens-family-core, mtl
2+
, base16-bytestring, bytestring, case-insensitive, containers
3+
, contravariant, cryptonite, deepseq, directory, exceptions
4+
, filepath, haskeline, http-client, http-client-tls
5+
, insert-ordered-containers, lens-family-core, memory, mtl
66
, optparse-generic, parsers, prettyprinter
77
, prettyprinter-ansi-terminal, repline, scientific, stdenv, tasty
88
, tasty-hunit, text, text-format, transformers, trifecta
@@ -15,20 +15,20 @@ mkDerivation {
1515
isLibrary = true;
1616
isExecutable = true;
1717
libraryHaskellDepends = [
18-
ansi-terminal ansi-wl-pprint base base16-bytestring bytestring
19-
case-insensitive charset containers contravariant cryptohash
20-
directory exceptions filepath http-client http-client-tls
21-
insert-ordered-containers lens-family-core parsers prettyprinter
18+
ansi-wl-pprint base base16-bytestring bytestring case-insensitive
19+
containers contravariant cryptonite directory exceptions filepath
20+
http-client http-client-tls insert-ordered-containers
21+
lens-family-core memory parsers prettyprinter
2222
prettyprinter-ansi-terminal scientific text text-format
2323
transformers trifecta unordered-containers vector
2424
];
2525
executableHaskellDepends = [
26-
ansi-terminal base containers haskeline mtl optparse-generic
27-
prettyprinter prettyprinter-ansi-terminal repline text trifecta
26+
ansi-terminal base haskeline mtl optparse-generic prettyprinter
27+
prettyprinter-ansi-terminal repline text trifecta
2828
];
2929
testHaskellDepends = [
30-
base containers deepseq insert-ordered-containers prettyprinter
31-
tasty tasty-hunit text vector
30+
base deepseq insert-ordered-containers prettyprinter tasty
31+
tasty-hunit text vector
3232
];
3333
description = "A configuration language guaranteed to terminate";
3434
license = stdenv.lib.licenses.bsd3;

dhall.cabal

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,15 @@ Library
159159
case-insensitive < 1.3 ,
160160
containers >= 0.5.0.0 && < 0.6 ,
161161
contravariant < 1.5 ,
162-
cryptohash < 0.12,
163-
exceptions >= 0.8.3 && < 0.10 ,
162+
cryptonite >= 0.23 && < 1.0 ,
163+
exceptions >= 0.8.3 && < 0.10,
164164
directory >= 1.3 && < 1.4 ,
165165
filepath >= 1.4 && < 1.5 ,
166166
http-client >= 0.4.30 && < 0.6 ,
167167
http-client-tls >= 0.2.0 && < 0.4 ,
168168
insert-ordered-containers >= 0.1.0.1 && < 0.3 ,
169169
lens-family-core >= 1.0.0 && < 1.3 ,
170+
memory >= 0.14 && < 0.15,
170171
parsers >= 0.12.4 && < 0.13,
171172
prettyprinter >= 1.2.0.1 && < 1.3 ,
172173
prettyprinter-ansi-terminal >= 1.1.1 && < 1.2 ,

src/Dhall/Core.hs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ module Dhall.Core (
5252
import Control.Applicative (Applicative(..), (<$>))
5353
#endif
5454
import Control.Applicative (empty)
55+
import Crypto.Hash (SHA256)
5556
import Data.Bifunctor (Bifunctor(..))
5657
import Data.Foldable
5758
import Data.HashMap.Strict.InsOrd (InsOrdHashMap)
@@ -70,9 +71,7 @@ import Numeric.Natural (Natural)
7071
import Prelude hiding (succ)
7172

7273
import qualified Control.Monad
73-
import qualified Data.ByteString
74-
import qualified Data.ByteString.Char8
75-
import qualified Data.ByteString.Base16
74+
import qualified Crypto.Hash
7675
import qualified Data.HashMap.Strict.InsOrd
7776
import qualified Data.HashSet
7877
import qualified Data.Sequence
@@ -137,17 +136,13 @@ data PathMode = Code | RawText deriving (Eq, Ord, Show)
137136

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

144143
instance Buildable PathHashed where
145144
build (PathHashed Nothing p) = build p
146-
build (PathHashed (Just h) p) = build p <> "sha256:" <> build string <> " "
147-
where
148-
bytes = Data.ByteString.Base16.encode h
149-
150-
string = Data.ByteString.Char8.unpack bytes
145+
build (PathHashed (Just h) p) = build p <> "sha256:" <> build (show h) <> " "
151146

152147
-- | Path to an external resource
153148
data Path = Path
@@ -1201,18 +1196,18 @@ denote (Embed a ) = Embed a
12011196

12021197
{-| Reduce an expression to its normal form, performing beta reduction and applying
12031198
any custom definitions.
1204-
1199+
12051200
`normalizeWith` is designed to be used with function `typeWith`. The `typeWith`
1206-
function allows typing of Dhall functions in a custom typing context whereas
1207-
`normalizeWith` allows evaluating Dhall expressions in a custom context.
1201+
function allows typing of Dhall functions in a custom typing context whereas
1202+
`normalizeWith` allows evaluating Dhall expressions in a custom context.
12081203
12091204
To be more precise `normalizeWith` applies the given normalizer when it finds an
12101205
application term that it cannot reduce by other means.
12111206
12121207
Note that the context used in normalization will determine the properties of normalization.
12131208
That is, if the functions in custom context are not total then the Dhall language, evaluated
1214-
with those functions is not total either.
1215-
1209+
with those functions is not total either.
1210+
12161211
-}
12171212
normalizeWith :: Normalizer a -> Expr s a -> Expr t a
12181213
normalizeWith ctx e0 = loop (denote e0)
@@ -1565,8 +1560,8 @@ normalizeWith ctx e0 = loop (denote e0)
15651560
type Normalizer a = forall s. Expr s a -> Maybe (Expr s a)
15661561

15671562
-- | Check if an expression is in a normal form given a context of evaluation.
1568-
-- Unlike `isNormalized`, this will fully normalize and traverse through the expression.
1569-
--
1563+
-- Unlike `isNormalized`, this will fully normalize and traverse through the expression.
1564+
--
15701565
-- It is much more efficient to use `isNormalized`.
15711566
isNormalizedWith :: (Eq s, Eq a) => Normalizer a -> Expr s a -> Bool
15721567
isNormalizedWith ctx e = e == (normalizeWith ctx e)

src/Dhall/Import.hs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,27 +71,27 @@
7171
> $ export BAZ='λ(x : Bool) → x == False'
7272
> $ dhall <<< "{ foo = env:FOO , bar = env:BAR , baz = env:BAZ }"
7373
> { bar : Text, baz : ∀(x : Bool) → Bool, foo : Integer }
74-
>
74+
>
7575
> { bar = "Hi", baz = λ(x : Bool) → x == False, foo = 1 }
7676
7777
If you wish to import the raw contents of a path as @Text@ then add
7878
@as Text@ to the end of the import:
7979
8080
> $ dhall <<< "http://example.com as Text"
8181
> Text
82-
>
82+
>
8383
> "<!doctype html>\n<html>\n<head>\n <title>Example Domain</title>\n\n <meta
8484
> charset=\"utf-8\" />\n <meta http-equiv=\"Content-type\" content=\"text/html
85-
> ; charset=utf-8\" />\n <meta name=\"viewport\" content=\"width=device-width,
85+
> ; charset=utf-8\" />\n <meta name=\"viewport\" content=\"width=device-width,
8686
> initial-scale=1\" />\n <style type=\"text/css\">\n body {\n backgro
8787
> und-color: #f0f0f2;\n margin: 0;\n padding: 0;\n font-famil
88-
> y: \"Open Sans\", \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n \n
88+
> y: \"Open Sans\", \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n \n
8989
> }\n div {\n width: 600px;\n margin: 5em auto;\n paddi
9090
> ng: 50px;\n background-color: #fff;\n border-radius: 1em;\n }\n
9191
> a:link, a:visited {\n color: #38488f;\n text-decoration: none;
9292
> \n }\n @media (max-width: 700px) {\n body {\n background
9393
> -color: #fff;\n }\n div {\n width: auto;\n m
94-
> argin: 0 auto;\n border-radius: 0;\n padding: 1em;\n
94+
> argin: 0 auto;\n border-radius: 0;\n padding: 1em;\n
9595
> }\n }\n </style> \n</head>\n\n<body>\n<div>\n <h1>Example Domain</
9696
> h1>\n <p>This domain is established to be used for illustrative examples in d
9797
> ocuments. You may use this\n domain in examples without prior coordination or
@@ -124,6 +124,7 @@ import Control.Monad (join)
124124
import Control.Monad.Catch (throwM, MonadCatch(catch))
125125
import Control.Monad.IO.Class (MonadIO(..))
126126
import Control.Monad.Trans.State.Strict (StateT)
127+
import Crypto.Hash (SHA256)
127128
import Data.ByteString.Lazy (ByteString)
128129
import Data.CaseInsensitive (CI)
129130
import Data.Map (Map)
@@ -160,9 +161,9 @@ import Text.Trifecta (Result(..))
160161
import Text.Trifecta.Delta (Delta(..))
161162

162163
import qualified Control.Monad.Trans.State.Strict as State
163-
import qualified Crypto.Hash.SHA256
164+
import qualified Crypto.Hash
165+
import qualified Data.ByteArray
164166
import qualified Data.ByteString
165-
import qualified Data.ByteString.Base16
166167
import qualified Data.ByteString.Char8
167168
import qualified Data.ByteString.Lazy
168169
import qualified Data.CaseInsensitive
@@ -534,8 +535,8 @@ instance Exception InternalError
534535

535536
-- | Exception thrown when an integrity check fails
536537
data HashMismatch = HashMismatch
537-
{ expectedHash :: Data.ByteString.ByteString
538-
, actualHash :: Data.ByteString.ByteString
538+
{ expectedHash :: Crypto.Hash.Digest SHA256
539+
, actualHash :: Crypto.Hash.Digest SHA256
539540
} deriving (Typeable)
540541

541542
instance Exception HashMismatch
@@ -547,14 +548,11 @@ instance Show HashMismatch where
547548
<> "\n"
548549
<> "Expected hash:\n"
549550
<> "\n"
550-
<> "" <> toString expectedHash <> "\n"
551+
<> "" <> show expectedHash <> "\n"
551552
<> "\n"
552553
<> "Actual hash:\n"
553554
<> "\n"
554-
<> "" <> toString actualHash <> "\n"
555-
where
556-
toString =
557-
Data.ByteString.Char8.unpack . Data.ByteString.Base16.encode
555+
<> "" <> show actualHash <> "\n"
558556

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

851849
-- | Hash a fully resolved expression
852-
hashExpression :: Expr s X -> Data.ByteString.ByteString
853-
hashExpression expr = Crypto.Hash.SHA256.hashlazy actualBytes
850+
hashExpression :: Expr s X -> (Crypto.Hash.Digest SHA256)
851+
hashExpression expr = Crypto.Hash.hashlazy actualBytes
854852
where
855853
text = Dhall.Core.pretty (Dhall.Core.normalize expr)
856854
actualBytes = Data.Text.Lazy.Encoding.encodeUtf8 text
@@ -866,7 +864,7 @@ hashExpressionToCode expr = "sha256:" <> lazyText
866864
where
867865
bytes = hashExpression expr
868866

869-
bytes16 = Data.ByteString.Base16.encode bytes
867+
bytes16 = Data.ByteArray.convert bytes
870868

871869
-- Notes that `decodeUtf8` is partial, but the base16-encoded bytestring
872870
-- should always successfully decode

src/Dhall/Parser.hs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import Text.Trifecta
4444
import Text.Trifecta.Delta (Delta)
4545

4646
import qualified Control.Monad
47-
import qualified Data.ByteString.Base16.Lazy
47+
import qualified Crypto.Hash
4848
import qualified Data.ByteString.Lazy
4949
import qualified Data.Char
5050
import qualified Data.HashMap.Strict.InsOrd
@@ -1509,10 +1509,9 @@ pathHashed_ = do
15091509
whitespace
15101510
let lazyText = Data.Text.Lazy.Builder.toLazyText builder
15111511
let lazyBytes = Data.Text.Lazy.Encoding.encodeUtf8 lazyText
1512-
let (hash, suffix) = Data.ByteString.Base16.Lazy.decode lazyBytes
1513-
if Data.ByteString.Lazy.null suffix
1514-
then return (Data.ByteString.Lazy.toStrict hash)
1515-
else fail "Invalid sha256 hash"
1512+
case Crypto.Hash.digestFromByteString (Data.ByteString.Lazy.toStrict lazyBytes) of
1513+
Nothing -> fail "Invalid sha256 hash"
1514+
Just h -> pure h
15161515

15171516
import_ :: Parser Path
15181517
import_ = (do

0 commit comments

Comments
 (0)