adding toByteString and fromByteString to PRK.

This commit is contained in:
Kazu Yamamoto 2016-11-08 13:18:36 +09:00
parent 47cb6ebdea
commit e00c89fb25

View File

@ -15,6 +15,8 @@ module Crypto.KDF.HKDF
, extract , extract
, extractSkip , extractSkip
, expand , expand
, toByteString
, fromByteString
) where ) where
import Data.Word import Data.Word
@ -22,11 +24,23 @@ import Crypto.Hash
import Crypto.MAC.HMAC import Crypto.MAC.HMAC
import Crypto.Internal.ByteArray (ScrubbedBytes, Bytes, ByteArray, ByteArrayAccess) import Crypto.Internal.ByteArray (ScrubbedBytes, Bytes, ByteArray, ByteArrayAccess)
import qualified Crypto.Internal.ByteArray as B import qualified Crypto.Internal.ByteArray as B
import qualified Data.ByteString as BS
-- | Pseudo Random Key -- | Pseudo Random Key
data PRK a = PRK (HMAC a) | PRK_NoExpand ScrubbedBytes data PRK a = PRK (HMAC a) | PRK_NoExpand ScrubbedBytes
deriving (Eq) deriving (Eq)
instance Show (PRK a) where
show (PRK hm) = show (hmacGetDigest hm)
show (PRK_NoExpand sb) = show sb
toByteString :: PRK a -> BS.ByteString
toByteString (PRK hm) = B.convert hm
toByteString (PRK_NoExpand sb) = B.convert sb
fromByteString :: BS.ByteString -> PRK a
fromByteString = extractSkip
-- | Extract a Pseudo Random Key using the parameter and the underlaying hash mechanism -- | Extract a Pseudo Random Key using the parameter and the underlaying hash mechanism
extract :: (HashAlgorithm a, ByteArrayAccess salt, ByteArrayAccess ikm) extract :: (HashAlgorithm a, ByteArrayAccess salt, ByteArrayAccess ikm)
=> salt -- ^ Salt => salt -- ^ Salt
@ -38,7 +52,7 @@ extract salt ikm = PRK $ hmac salt ikm
-- --
-- Only use when guaranteed to have a good quality and random data to use directly as key. -- Only use when guaranteed to have a good quality and random data to use directly as key.
-- This effectively skip a HMAC with key=salt and data=key. -- This effectively skip a HMAC with key=salt and data=key.
extractSkip :: (HashAlgorithm a, ByteArrayAccess ikm) extractSkip :: ByteArrayAccess ikm
=> ikm => ikm
-> PRK a -> PRK a
extractSkip ikm = PRK_NoExpand $ B.convert ikm extractSkip ikm = PRK_NoExpand $ B.convert ikm