Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Attempt to generate a compressed public key from EC private key
#!/usr/bin/env ruby
require 'bundler/inline'
gemfile do
gem "openssl"
end
# See https://github.com/ruby/openssl/issues/29
def real_public_key(k)
# TODO? .point_conversion_form = :compressed
point = k.public_key
pub = OpenSSL::PKey::EC.new(point.group)
pub.public_key = point
pub
end
ec1 = OpenSSL::PKey::EC.new('secp256k1').generate_key
# Write a file to file to covert it via the CLI
File.open("test-compressed-key.pem", 'w') {|f| f.write(ec1.to_pem) }
# CLI Version:
puts `openssl ec -in test-compressed-key.pem -pubout -conv_form compressed`
# Ruby Version:
puts real_public_key(ec1).to_pem
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.