Skip to content

Instantly share code, notes, and snippets.

@divarvel
Created November 30, 2020 23:06
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 divarvel/39737925df95ca3a7a8bd94788d793c4 to your computer and use it in GitHub Desktop.
Save divarvel/39737925df95ca3a7a8bd94788d793c4 to your computer and use it in GitHub Desktop.
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
module Inline where
import Data.ByteString (ByteString, copy, packCStringLen)
import Foreign.Marshal.Alloc
import qualified Language.C.Inline as C
C.context (C.baseCtx <> C.bsCtx)
C.include "biscuit_auth.h"
main :: IO ()
main = putStrLn "x"
genKp :: ByteString
-> IO (ByteString, ByteString)
genKp seed =
allocaBytes 32 $ \keyPairBuf ->
allocaBytes 32 $ \pubKeyBuf -> do
[C.block|
void {
KeyPair * kp = key_pair_new($bs-ptr:seed, $bs-len:seed);
PublicKey * pubkey = key_pair_public(kp);
key_pair_serialize(kp, $(char* keyPairBuf));
public_key_serialize(pubkey, $(char* pubKeyBuf));
//public_key_free(pubkey);
key_pair_free(kp);
return;
}
|]
(,) <$> packCStringLen (keyPairBuf, 32) <*> packCStringLen (pubKeyBuf, 32)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment