Skip to content

Commit 581ebcb

Browse files
committed
Core: Internal: Hash: truncateDigest: refactor
1 parent 500a934 commit 581ebcb

File tree

1 file changed

+10
-6
lines changed
  • hnix-store-core/src/System/Nix/Internal

1 file changed

+10
-6
lines changed

hnix-store-core/src/System/Nix/Internal/Hash.hs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ import qualified Data.Text as T
2929
import Data.Word (Word8)
3030
import qualified GHC.TypeLits as Kind
3131
(Nat, KnownNat, natVal)
32-
import Data.Coerce (coerce)
3332
import System.Nix.Internal.Base
3433
( BaseEncoding(Base16,NixBase32,Base64)
3534
, encodeWith
3635
, decodeWith
3736
)
37+
import Data.Bool (bool)
38+
import Data.Coerce (coerce)
3839

3940
-- | The universe of supported hash algorithms.
4041
--
@@ -203,15 +204,18 @@ truncateDigest
203204
truncateDigest (Digest c) =
204205
Digest $ BS.pack $ fmap truncOutputByte [0.. n-1]
205206
where
206-
n = fromIntegral $ Kind.natVal (Proxy @n)
207+
n = fromIntegral $ Kind.natVal $ Proxy @n
207208

208209
truncOutputByte :: Int -> Word8
209210
truncOutputByte i = foldl' (aux i) 0 [0 .. BS.length c - 1]
210211

211212
inputByte :: Int -> Word8
212-
inputByte j = BS.index c (fromIntegral j)
213+
inputByte j = BS.index c j
213214

214215
aux :: Int -> Word8 -> Int -> Word8
215-
aux i x j = if j `mod` fromIntegral n == fromIntegral i
216-
then xor x (inputByte $ fromIntegral j)
217-
else x
216+
aux i x j =
217+
bool
218+
id
219+
(`xor` inputByte j)
220+
(j `mod` n == i)
221+
x

0 commit comments

Comments
 (0)