Class: CryptoToolchain::DiffieHellman::PeerInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/crypto_toolchain/diffie_hellman/peer_info.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(peer:, channel:, pubkey: nil, p: nil, g: nil) ⇒ PeerInfo

Returns a new instance of PeerInfo.



4
5
6
7
8
9
10
# File 'lib/crypto_toolchain/diffie_hellman/peer_info.rb', line 4

def initialize(peer: , channel: , pubkey: nil, p: nil, g: nil)
  @peer = peer
  @channel = channel
  @pubkey = pubkey
  @p = p
  @g = g
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



11
12
13
# File 'lib/crypto_toolchain/diffie_hellman/peer_info.rb', line 11

def channel
  @channel
end

#gObject

Returns the value of attribute g.



12
13
14
# File 'lib/crypto_toolchain/diffie_hellman/peer_info.rb', line 12

def g
  @g
end

#pObject

Returns the value of attribute p.



12
13
14
# File 'lib/crypto_toolchain/diffie_hellman/peer_info.rb', line 12

def p
  @p
end

#peerObject (readonly)

Returns the value of attribute peer.



11
12
13
# File 'lib/crypto_toolchain/diffie_hellman/peer_info.rb', line 11

def peer
  @peer
end

#pubkeyObject

Returns the value of attribute pubkey.



12
13
14
# File 'lib/crypto_toolchain/diffie_hellman/peer_info.rb', line 12

def pubkey
  @pubkey
end

#shared_secretObject (readonly)

Returns the value of attribute shared_secret.



11
12
13
# File 'lib/crypto_toolchain/diffie_hellman/peer_info.rb', line 11

def shared_secret
  @shared_secret
end

Instance Method Details

#session_keyObject



28
29
30
31
32
33
# File 'lib/crypto_toolchain/diffie_hellman/peer_info.rb', line 28

def session_key
  if shared_secret.nil?
    raise ArgumentError.new("Session key requires a shared secret")
  end
  @session_key ||= CryptoToolchain::Utilities::SHA1.bindigest(shared_secret.to_s)[0..15]
end

#set_shared_secret(privkey, override: nil) ⇒ Object



24
25
26
# File 'lib/crypto_toolchain/diffie_hellman/peer_info.rb', line 24

def set_shared_secret(privkey, override: nil)
  @shared_secret = override ? override : pubkey.modexp(privkey, p)
end

#to_hObject



14
15
16
17
18
19
20
21
22
# File 'lib/crypto_toolchain/diffie_hellman/peer_info.rb', line 14

def to_h
  {
    name:   peer.name,
    p:      p,
    g:      g,
    pubkey: pubkey,
    secret: shared_secret
  }
end

#update(hsh) ⇒ Object



35
36
37
38
39
# File 'lib/crypto_toolchain/diffie_hellman/peer_info.rb', line 35

def update(hsh)
  hsh.each do |k, v|
    self.send("#{k}=", v) unless v.nil?
  end
end