@@ -19,24 +19,21 @@ import qualified Crypto.Hash.SHA1 as SHA1
1919import qualified Crypto.Hash.SHA256 as SHA256
2020import qualified Crypto.Hash.SHA512 as SHA512
2121import qualified Data.ByteString as BS
22- import qualified Data.ByteString.Base16 as Base16
23- import qualified System.Nix.Base32 as Base32 -- Nix has own Base32 encoding
24- import qualified Data.ByteString.Base64 as Base64
2522import Data.Bits (xor )
2623import qualified Data.ByteString.Lazy as BSL
2724import qualified Data.Hashable as DataHashable
2825import Data.List (foldl' )
2926import Data.Proxy (Proxy (Proxy ))
3027import Data.Text (Text )
3128import qualified Data.Text as T
32- import qualified Data.Text.Encoding as T
3329import Data.Word (Word8 )
3430import qualified GHC.TypeLits as Kind
3531 (Nat , KnownNat , natVal )
3632import Data.Coerce (coerce )
3733import System.Nix.Internal.Base
3834 ( BaseEncoding (Base16 ,NixBase32 ,Base64 )
3935 , encodeWith
36+ , decodeWith
4037 )
4138
4239-- | The universe of supported hash algorithms.
@@ -116,9 +113,9 @@ mkNamedDigest name sriHash =
116113 _ -> Left $ " Unknown hash name: " <> T. unpack name
117114 decodeGo :: forall a . (NamedAlgo a , ValidAlgo a ) => Text -> Either String (Digest a )
118115 decodeGo h
119- | size == base16Len = decodeBase Base16 h
120- | size == base32Len = decodeBase NixBase32 h
121- | size == base64Len = decodeBase Base64 h
116+ | size == base16Len = decodeDigestWith Base16 h
117+ | size == base32Len = decodeDigestWith NixBase32 h
118+ | size == base64Len = decodeDigestWith Base64 h
122119 | otherwise = Left $ T. unpack sriHash <> " is not a valid " <> T. unpack name <> " hash. Its length (" <> show size <> " ) does not match any of " <> show [base16Len, base32Len, base64Len]
123120 where
124121 size = T. length h
@@ -154,18 +151,8 @@ encodeDigestWith b = encodeWith b . coerce
154151
155152
156153-- | Take BaseEncoding type of the input -> take the input itself -> decodeBase into Digest
157- decodeBase :: BaseEncoding -> T. Text -> Either String (Digest a )
158- #if MIN_VERSION_base16_bytestring(1,0,0)
159- decodeBase Base16 = fmap Digest . Base16. decode . T. encodeUtf8
160- #else
161- decodeBase Base16 = lDecode -- this tacit sugar simply makes GHC pleased with number of args
162- where
163- lDecode t = case Base16. decode (T. encodeUtf8 t) of
164- (x, " " ) -> Right $ Digest x
165- _ -> Left $ " Unable to decode base16 string" <> T. unpack t
166- #endif
167- decodeBase NixBase32 = fmap Digest . Base32. decode
168- decodeBase Base64 = fmap Digest . Base64. decode . T. encodeUtf8
154+ decodeDigestWith :: BaseEncoding -> T. Text -> Either String (Digest a )
155+ decodeDigestWith b x = Digest <$> decodeWith b x
169156
170157
171158-- | Uses "Crypto.Hash.MD5" from cryptohash-md5.
0 commit comments