Skip to content

Instantly share code, notes, and snippets.

@paulvictor
Created January 6, 2021 07:10
Show Gist options
  • Save paulvictor/f88ed6e6ed27352d468df7c49b59e186 to your computer and use it in GitHub Desktop.
Save paulvictor/f88ed6e6ed27352d468df7c49b59e186 to your computer and use it in GitHub Desktop.
Rsa public private key demo
#!/usr/bin/env nix-shell
#! nix-shell -i runghc -p "ghc.withPackages (pkgs: [ pkgs.bytestring pkgs.jose-jwt ])"
#! nix-shell -I nixpkgs=https://github.com/nixos/nixpkgs-channels/archive/nixos-20.03.tar.gz
{-# LANGUAGE OverloadedStrings #-}
module Main(main) where
import Control.Exception(assert)
import Jose.Jwa (Alg(Encrypted), JweAlg(RSA_OAEP), Enc(A256GCM))
import Jose.Jwe(rsaEncode, rsaDecode)
import Jose.Jwk (Jwk(..), generateRsaKeyPair, KeyUse(Enc))
import Jose.Jwt (Jwt(..), KeyId(..))
main = do
let
payload = "foo"
(RsaPublicJwk pubK _ _ _, RsaPrivateJwk privK _ _ _) <-
generateRsaKeyPair 256 (KeyId "fookey") Enc (Just (Encrypted RSA_OAEP))
Right (Jwt jwtpl) <- rsaEncode RSA_OAEP A256GCM pubK payload
Right (_, payload') <- rsaDecode privK jwtpl
assert (payload' == payload) (pure ())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment