Skip to content

Instantly share code, notes, and snippets.

@havenwood
Last active September 24, 2019 18:39
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 havenwood/5f8d3ae5f4d9d3963225d9ecd0864ec2 to your computer and use it in GitHub Desktop.
Save havenwood/5f8d3ae5f4d9d3963225d9ecd0864ec2 to your computer and use it in GitHub Desktop.
Sign a payload that expires in ten seconds with JWTs and twisted Edwards curves (Ruby in Hollywood)
# gem install jwt rbnacl
require 'jwt'
require 'securerandom'
command = 'flee to the hills!'
ten_seconds_from_now = Time.now.to_i + 10
payload = {
data: command,
exp: ten_seconds_from_now
}
##
# Encrypt your payload.
signing_key = RbNaCl::Signatures::Ed25519::SigningKey.new SecureRandom.bytes 32
signature = signing_key.to_bytes # Save your signature to sign other things.
password = signing_key.verify_key.to_bytes # Use your password to decrypt.
token = JWT.encode payload, signing_key, 'ED25519' # Share the encrypted payload.
##
# Decrypt your payload.
verify_key = RbNaCl::Signatures::Ed25519::VerifyKey.new password
begin
decrypted_payload, _ = JWT.decode token, verify_key, true, {algorithm: 'ED25519'}
rescue JWT::ExpiredSignature
abort 'Too late!'
end
puts decrypted_payload['data']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment