Skip to content

Instantly share code, notes, and snippets.

@mcschroeder
Created February 10, 2016 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcschroeder/b59c7859db87f27b812f to your computer and use it in GitHub Desktop.
Save mcschroeder/b59c7859db87f27b812f to your computer and use it in GitHub Desktop.
Getting values out of types at runtime
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Key (Key, makeKey) where
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import Data.Proxy
import GHC.TypeLits
newtype Key (size :: Nat) = Key ByteString
deriving (Show)
makeKey :: forall size. KnownNat size => ByteString -> Maybe (Key size)
makeKey s | B.length s == (keyLength `div` 8) = Just (Key s)
| otherwise = Nothing
where
keyLength = fromInteger $ natVal (Proxy :: Proxy size)
--encrypt :: Key 96 -> Key 256 -> String -> String
--encrypt (Key nonce) (Key cipherKey) plaintext = undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment