Last active
September 24, 2019 18:39
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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