Skip to content

Instantly share code, notes, and snippets.

@csexton
Created April 10, 2019 16:45
Show Gist options
  • Save csexton/1fa76d036c96037f70bb10296eece9f7 to your computer and use it in GitHub Desktop.
Save csexton/1fa76d036c96037f70bb10296eece9f7 to your computer and use it in GitHub Desktop.
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