Skip to content

Instantly share code, notes, and snippets.

@RickCarlino
Last active March 10, 2020 13:08
Show Gist options
  • Save RickCarlino/73fe09fdb7e5c87fe73a24bb89bb9a80 to your computer and use it in GitHub Desktop.
Save RickCarlino/73fe09fdb7e5c87fe73a24bb89bb9a80 to your computer and use it in GitHub Desktop.
Verification of SSB messages in Ruby

Verifying SSB Messages in Ruby

First we need a few dependencies:

require "json"
require "base64"
require "ed25519"

Here's the message I want to verify:

original_message = {
  previous: nil,
  author: "@z2M8msI2EUubNHnrEJncglDIy2/SUd+36jCyJnfeiHk=.ed25519",
  sequence: 1,
  timestamp: 1554164725521,
  hash: "sha256",
  content: {
    type: "about",
    about: "@z2M8msI2EUubNHnrEJncglDIy2/SUd+36jCyJnfeiHk=.ed25519",
    image: "&jqMB109+asDMUVWkeAsqK/4KlbF+6M2x+jtTdFIdVw8=.sha256",
    name: "Netscape Navigator",
  },
  signature: "3er8E88P7WPSjnm+L3QmoPqNxhqAn/pOjvo6Owk0KNn69FgOjAYLOgRdrGnuihBp4QYWYPJ5bS1Gw9weQKj9DQ==.sig.ed25519",
}

The original message was JSON.

We need to delete the signature from the message before we can verify:

original_message.delete(:signature)

We also need a copy of our public key. I could have been fancy and exracted the value from original_message, but will instead copy/paste for readability:

public_key = Base64.urlsafe_decode64("z2M8msI2EUubNHnrEJncglDIy2/SUd+36jCyJnfeiHk=")

Same thing with the signature. I am just copy/pasting the value found in original_message:

signature = Base64.urlsafe_decode64("3er8E88P7WPSjnm+L3QmoPqNxhqAn/pOjvo6Owk0KNn69FgOjAYLOgRdrGnuihBp4QYWYPJ5bS1Gw9weQKj9DQ==")

Since JSON is not deterministic, we need to serialize the message exactly how we found it. Luckily the Ruby JSON lib follows those rules when using pretty_generate. Don't forget to call .chomp to remove any trailing carriage returns (they will invalidate the signature):

message = JSON.pretty_generate(original_message).chomp

We now have a message and a private_key. We are ready to verify:

Ed25519::VerifyKey.new(public_key).verify(signature, message)

The program will return true if OK (:tada:) or throw a Ed25519::VerifyError exception if verification fails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment