class CryptoUtils::EcdsaUtil

ASYMMETRIC SIGNING UTIL

Public Instance Methods

create_key_pair() click to toggle source

for a 256-bit ECDSA curve, the uncompressed pubkey is 512 bits (256 bits of x, 256 bits of y, no sign bit). The compressed pubkey is 257 bits (256 bits of x, one bit of the sign of y).

# File lib/ig-crypto-utils.rb, line 86
def create_key_pair
  group_name = 'secp256k1'

  # set compression to true for key generation on the group
  group = OpenSSL::PKey::EC::Group.new(group_name)
  group.point_conversion_form = :compressed

  # instantiate the curve and generate the keys
  curve = OpenSSL::PKey::EC.new(group)
  curve.generate_key

  public_key = curve.public_key
  private_key = curve.private_key

  # get binary representation of keys
  pk_bn_bin = public_key.to_bn.to_s(2)
  sk_bn_bin = private_key.to_s(2)

  #base64 encode keys
  encoded_pk = Base64.encode64(pk_bn_bin)
  encoded_sk = Base64.encode64(sk_bn_bin)

  {:pk => encoded_pk, :sk => encoded_sk}

end
sign(encoded_data, encoded_private_key) click to toggle source
# File lib/ig-crypto-utils.rb, line 112
def sign(encoded_data, encoded_private_key)
  group_name = 'secp256k1'

  decoded_data = Base64.decode64 encoded_data
  decoded_private_key = Base64.decode64 encoded_private_key

  curve = OpenSSL::PKey::EC.new(group_name)
  curve.private_key = OpenSSL::BN.new(decoded_private_key, 2)

  result = curve.dsa_sign_asn1 decoded_data

  Base64.encode64 result
end
validate_signature(encoded_digest, encoded_signature, encoded_public_key) click to toggle source
# File lib/ig-crypto-utils.rb, line 126
def validate_signature(encoded_digest, encoded_signature, encoded_public_key)
  group_name = 'secp256k1'

  decoded_signature = Base64.decode64 encoded_signature
  decoded_digest = Base64.decode64 encoded_digest
  decoded_public_key = Base64.decode64 encoded_public_key

  curve = OpenSSL::PKey::EC.new(group_name)
  key_bn = OpenSSL::BN.new(decoded_public_key, 2)
  group = OpenSSL::PKey::EC::Group.new(group_name)
  curve.public_key = OpenSSL::PKey::EC::Point.new(group, key_bn)

  curve.dsa_verify_asn1(decoded_digest, decoded_signature)
end