Created
April 10, 2019 16:45
-
-
Save csexton/1fa76d036c96037f70bb10296eece9f7 to your computer and use it in GitHub Desktop.
Attempt to generate a compressed public key from EC private key
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
#!/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