aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Benjamin Barenblat <bbaren@google.com>2018-09-21 14:19:10 -0400
committerGravatar Benjamin Barenblat <bbaren@google.com>2018-09-21 14:19:10 -0400
commit021932a9ee2a81435b0a341030f68730dd9abd3b (patch)
treed8f41a370c3cb46750ec51e7c18a1c44ed3972f0
parentad7c615864605c8cc8eea7fc7bd6e917404e5562 (diff)
Start using base16-bytestring in btls
We’re already using it in tests, so eliminate some hand-rolled hexification code.
-rw-r--r--btls.cabal1
-rw-r--r--src/BTLS/Types.hs14
-rw-r--r--src/Data/HMAC.hs4
3 files changed, 9 insertions, 10 deletions
diff --git a/btls.cabal b/btls.cabal
index 1ff34f0..9d27272 100644
--- a/btls.cabal
+++ b/btls.cabal
@@ -88,6 +88,7 @@ library
-- in OpenSSL.
extra-libraries: btls_crypto
build-depends: base >=4.9 && <4.10
+ , base16-bytestring >=0.1.1.6 && <0.2
, bytestring >=0.10 && <0.11
, monad-loops >=0.4.3 && <0.5
, transformers >=0.5.2 && <0.6
diff --git a/src/BTLS/Types.hs b/src/BTLS/Types.hs
index 44b29bc..4a4e518 100644
--- a/src/BTLS/Types.hs
+++ b/src/BTLS/Types.hs
@@ -14,11 +14,10 @@
module BTLS.Types where
-import Data.Bits (Bits((.&.)), shiftR)
import Data.ByteString (ByteString)
import qualified Data.ByteString as ByteString
-import Data.Char (intToDigit)
-import Data.Word (Word8)
+import qualified Data.ByteString.Base16 as ByteString.Base16
+import qualified Data.ByteString.Char8 as ByteString.Char8
import Foreign (Ptr)
import BTLS.BoringSSL.Base (EVPMD)
@@ -39,11 +38,7 @@ newtype Digest = Digest ByteString
deriving (Eq, Ord)
instance Show Digest where
- show (Digest d) = ByteString.foldr showHexPadded [] d
- where
- showHexPadded b xs =
- hexit (b `shiftR` 4 .&. 0x0f) : hexit (b .&. 0x0f) : xs
- hexit = intToDigit . fromIntegral :: Word8 -> Char
+ show (Digest d) = showHex d
-- | A salt. Equality comparisons on this type are variable-time.
newtype Salt = Salt ByteString
@@ -57,3 +52,6 @@ noSalt = Salt ByteString.empty
-- this type are variable-time.
newtype SecretKey = SecretKey ByteString
deriving (Eq, Ord, Show)
+
+showHex :: ByteString -> String
+showHex = ByteString.Char8.unpack . ByteString.Base16.encode \ No newline at end of file
diff --git a/src/Data/HMAC.hs b/src/Data/HMAC.hs
index c6edca3..d8c36f1 100644
--- a/src/Data/HMAC.hs
+++ b/src/Data/HMAC.hs
@@ -59,7 +59,7 @@ import BTLS.BoringSSL.HMAC
import BTLS.BoringSSL.Mem (cryptoMemcmp)
import BTLS.Buffer (onBufferOfMaxSize)
import BTLS.Result (Error, check)
-import BTLS.Types (Algorithm(Algorithm), Digest(Digest), SecretKey(SecretKey))
+import BTLS.Types (Algorithm(Algorithm), SecretKey(SecretKey), showHex)
import Data.Digest (md5, sha1, sha224, sha256, sha384, sha512)
-- | A hash-based message authentication code. Equality comparisons on this type
@@ -74,7 +74,7 @@ instance Eq HMAC where
(==0) <$> cryptoMemcmp a' b' size
instance Show HMAC where
- show (HMAC m) = show (Digest m)
+ show (HMAC m) = showHex m
-- | Creates an HMAC according to the given 'Algorithm'.
hmac :: Algorithm -> SecretKey -> Lazy.ByteString -> Either [Error] HMAC