Skip to content

Instantly share code, notes, and snippets.

@seungjin
Created February 28, 2023 06:13
Show Gist options
  • Save seungjin/78e80840eb928170444d800eea435125 to your computer and use it in GitHub Desktop.
Save seungjin/78e80840eb928170444d800eea435125 to your computer and use it in GitHub Desktop.
require 'http'
require 'openssl'
document = File.read('create-hello-world.json')
date = Time.now.utc.httpdate
sha256 = OpenSSL::Digest::SHA256.new
digest = sha256.digest(document)
ed = 'SHA-256='+Base64.encode64(digest).strip
keypair = OpenSSL::PKey::RSA.new(File.read('private.pem'))
signed_string = "(request-target): post /inbox\nhost: mstd.seungjin.net\ndate: #{date}\ndigest: #{ed}"
signature= Base64.strict_encode64(keypair.sign(OpenSSL::Digest::SHA256.new, signed_string))
header = 'keyId="https://ap.dev.seungjin.net/actor#main-key",algorithm="rsa-sha256",headers="(request-target) host date digest",signature="' + signature + '"'
print HTTP.headers({ 'Host': 'mstd.seungjin.net',
'Date': date,
'Accept': 'application/activity+json',
'Digest': ed,
'Signature': header })
.post('https://mstd.seungjin.net/inbox', body: document)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment