diff options
Diffstat (limited to 'src/Network/WireGuard/Foreign/Key.hs')
-rw-r--r-- | src/Network/WireGuard/Foreign/Key.hs | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/Network/WireGuard/Foreign/Key.hs b/src/Network/WireGuard/Foreign/Key.hs new file mode 100644 index 0000000..2a0e7b8 --- /dev/null +++ b/src/Network/WireGuard/Foreign/Key.hs @@ -0,0 +1,29 @@ +module Network.WireGuard.Foreign.Key + ( Key + , fromByteString + , toByteString + ) where + +import qualified Data.ByteString as BS + +import Foreign + +import Network.WireGuard.Internal.Constant (keyLength) + +newtype Key = Key { fromKey :: [Word8] } + +instance Storable Key where + sizeOf _ = sizeOf (undefined :: Word8) * keyLength + alignment _ = alignment (undefined :: Word8) + peek ptr = Key <$> peekArray keyLength (castPtr ptr) + poke ptr (Key k) + | length k == keyLength = pokeArray (castPtr ptr) k + | otherwise = error "Key.poke: key length mismatch" + +fromByteString :: BS.ByteString -> Key +fromByteString bs + | BS.length bs == keyLength = Key (BS.unpack bs) + | otherwise = error "Key.fromByteString: key length mismatch" + +toByteString :: Key -> BS.ByteString +toByteString = BS.pack . fromKey |