Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ed25519 signature in ruby
require 'ed25519'
payload = 'plain text'
# generate key pair
signing_key = Ed25519::SigningKey.generate
verify_key = signing_key.verify_key
p signing_key
p verify_key
# sign
signature = signing_key.sign payload
puts "signature: #{signature.unpack 'H*'}"
#verify
result = verify_key.verify signature, payload
puts "verification result: #{result}"
wrong_payload = 'Different from original message'
begin
verify_key.verify signature, wrong_payload
rescue => e
puts e.class
puts e.message
end
# serialize keys
signing_key_bytes = signing_key.to_bytes
verify_key_bytes = verify_key.to_bytes
puts "signing_key_bytes: #{signing_key_bytes.unpack 'H*'}"
puts "verify_key_bytes: #{verify_key_bytes.unpack 'H*'}"
# You can make instance form both serialized keys
Ed25519::SigningKey.new signing_key_bytes
restored_verify_key = Ed25519::VerifyKey.new verify_key_bytes
second_result = restored_verify_key.verify signature, payload
puts "verification result: #{second_result}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment