Class: Cryptos::Der

Inherits:
Struct
  • Object
show all
Includes:
Utils::Hashes, Utils::Hexas
Defined in:
lib/cryptos/der.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Hashes

#hash160, #hash256, #ripemd160, #sha256

Methods included from Utils::Hexas

#bignum_to_hex, #bin_to_hex, #byte_to_hex, #bytes_to_hex, #hex_size, #hex_to_little, #int_to_hex, #long_to_hex

Methods included from Utils::Bytes

#bignum_to_bytes, #bytes_to_bignum

Constructor Details

#initialize(der: 0x30, length: 0x44, ri: 0x02, rl: 0x20, r: nil, si: 0x02, sl: 0x20, s: nil, sighash_type: 0x01) ⇒ Der

Returns a new instance of Der.



5
6
7
# File 'lib/cryptos/der.rb', line 5

def initialize(der: 0x30, length: 0x44, ri: 0x02, rl: 0x20, r: nil, si: 0x02, sl: 0x20, s: nil, sighash_type: 0x01)
  super der, length, ri, rl, r, si, sl, s, sighash_type
end

Instance Attribute Details

#derObject

Returns the value of attribute der

Returns:

  • (Object)

    the current value of der



2
3
4
# File 'lib/cryptos/der.rb', line 2

def der
  @der
end

#lengthObject

Returns the value of attribute length

Returns:

  • (Object)

    the current value of length



2
3
4
# File 'lib/cryptos/der.rb', line 2

def length
  @length
end

#rObject

Returns the value of attribute r

Returns:

  • (Object)

    the current value of r



2
3
4
# File 'lib/cryptos/der.rb', line 2

def r
  @r
end

#riObject

Returns the value of attribute ri

Returns:

  • (Object)

    the current value of ri



2
3
4
# File 'lib/cryptos/der.rb', line 2

def ri
  @ri
end

#rlObject

Returns the value of attribute rl

Returns:

  • (Object)

    the current value of rl



2
3
4
# File 'lib/cryptos/der.rb', line 2

def rl
  @rl
end

#sObject

Returns the value of attribute s

Returns:

  • (Object)

    the current value of s



2
3
4
# File 'lib/cryptos/der.rb', line 2

def s
  @s
end

#siObject

Returns the value of attribute si

Returns:

  • (Object)

    the current value of si



2
3
4
# File 'lib/cryptos/der.rb', line 2

def si
  @si
end

#sighash_typeObject

Returns the value of attribute sighash_type

Returns:

  • (Object)

    the current value of sighash_type



2
3
4
# File 'lib/cryptos/der.rb', line 2

def sighash_type
  @sighash_type
end

#slObject

Returns the value of attribute sl

Returns:

  • (Object)

    the current value of sl



2
3
4
# File 'lib/cryptos/der.rb', line 2

def sl
  @sl
end

Class Method Details

.parse(signature) ⇒ Object



22
23
24
25
# File 'lib/cryptos/der.rb', line 22

def self.parse(signature)
  fields = *[signature].pack('H*').unpack('CCCCH66CCH64C')
  Der.new r: fields[4], s: fields[7], sighash_type: fields[8]
end

Instance Method Details

#serializeObject



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

def serialize
  r_bytes = bignum_to_bytes(r, 32, false)
  if r_bytes.first & 0x80 == 128
    r_bytes = [0x00] + r_bytes
    self.length += 1
    self.rl += 1
  end
  byte_to_hex(der) + byte_to_hex(length) +
    byte_to_hex(ri) + byte_to_hex(rl) + bytes_to_hex(r_bytes) +
    byte_to_hex(si) + byte_to_hex(sl) + bignum_to_hex(s) +
    byte_to_hex(sighash_type)
end