hypersig
gleam add hypersig@1
import hypersig/signature
import hypersig/key
import gleam/http
import gleam/http/request
pub fn main() -> Nil {
let assert Ok(#(public_key, private_key)) = key.generate_key_pair()
// This would be some outgoing request to an ActivityPub server
let req =
request.new()
|> request.set_method(http.Post)
|> request.set_path("/some/api/path")
|> request.set_header("host", "mastodon.social")
|> request.set_header("date", "Fri, 13 Oct 2023 11:11:11 GMT")
|> request.set_header("content-type", "application/activity+json")
// The request is signed with the private key and a "signature" header is added
let assert Ok(req) =
signature.new(private_key, "some-key-id")
|> signature.with_header("content-type")
|> signature.sign(req)
// The request is now ready to be sent using whatever http client you prefer
// On the other end, the verification would look something like this
let assert Ok(True) =
signature.from_request(req)
|> signature.verify(with: public_key)
}
Further documentation can be found at https://hexdocs.pm/hypersig.
Development
gleam test # Run the tests