Class: BitAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/bitauth.rb

Constant Summary collapse

VERSION =
"0.0.3"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ BitAuth

Returns a new instance of BitAuth.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/bitauth.rb', line 8

def initialize(opts = {})
  if opts[:private_key]
    self.private_key = opts[:private_key]
    self.public_key = pubkey_from_privkey private_key
    self.sin = generate_sin public_key
  elsif opts[:public_key]
    self.public_key = opts[:public_key]
    self.sin = generate_sin public_key
  else
    generate_keys
  end
end

Instance Attribute Details

#private_keyObject

Returns the value of attribute private_key.



6
7
8
# File 'lib/bitauth.rb', line 6

def private_key
  @private_key
end

#public_keyObject

Returns the value of attribute public_key.



6
7
8
# File 'lib/bitauth.rb', line 6

def public_key
  @public_key
end

#sinObject

Returns the value of attribute sin.



6
7
8
# File 'lib/bitauth.rb', line 6

def sin
  @sin
end

Instance Method Details

#sign(data) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/bitauth.rb', line 21

def sign(data)
  fail(ArgumentError, 'Missing Private Key') if public_key.nil?

  hash = OpenSSL::Digest::SHA256.new.hexdigest data

  ecdsa = OpenSSL::PKey::EC.new('secp256k1')
  ecdsa.private_key = OpenSSL::BN.new private_key, 16

  ecdsa.dsa_sign_asn1([hash].pack('H*')).unpack('H*')[0]
end

#verify(data, sig) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bitauth.rb', line 32

def verify(data, sig)
  hash = OpenSSL::Digest::SHA256.new.hexdigest data

  ecdsa = OpenSSL::PKey::EC.new('secp256k1')
  pub = OpenSSL::PKey::EC::Point.new(ecdsa.group, OpenSSL::BN.new(public_key, 16))

  ecdsa.public_key = pub
  ecdsa.dsa_verify_asn1([hash].pack('H*'), [sig].pack('H*'))
rescue
  false
end