Class: SSHData::PublicKey::ED25519

Inherits:
Base
  • Object
show all
Defined in:
lib/ssh_data/public_key/ed25519.rb

Direct Known Subclasses

SKED25519

Instance Attribute Summary collapse

Attributes inherited from Base

#algo

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#fingerprint, #openssh, #sign

Constructor Details

#initialize(algo:, pk:) ⇒ ED25519

Returns a new instance of ED25519.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ssh_data/public_key/ed25519.rb', line 23

def initialize(algo:, pk:)
  unless algo == self.class.algorithm_identifier
    raise DecodeError, "bad algorithm: #{algo.inspect}"
  end

  @pk = pk

  if self.class.enabled?
    @ed25519_key = Ed25519::VerifyKey.new(pk)
  end

  super(algo: algo)
end

Instance Attribute Details

#ed25519_keyObject (readonly)

Returns the value of attribute ed25519_key.



4
5
6
# File 'lib/ssh_data/public_key/ed25519.rb', line 4

def ed25519_key
  @ed25519_key
end

#pkObject (readonly)

Returns the value of attribute pk.



4
5
6
# File 'lib/ssh_data/public_key/ed25519.rb', line 4

def pk
  @pk
end

Class Method Details

.algorithm_identifierObject



19
20
21
# File 'lib/ssh_data/public_key/ed25519.rb', line 19

def self.algorithm_identifier
  ALGO_ED25519
end

.ed25519_gem_required!Object

Assert that the ed25519 gem has been loaded.

Returns nothing, raises AlgorithmError.

Raises:



15
16
17
# File 'lib/ssh_data/public_key/ed25519.rb', line 15

def self.ed25519_gem_required!
  raise AlgorithmError, "the ed25519 gem is not loaded" unless enabled?
end

.enabled?Boolean

ed25519 isn’t a hard requirement for using this Gem. We only do actual validation with the key if the ed25519 Gem has been loaded.

Returns:

  • (Boolean)


8
9
10
# File 'lib/ssh_data/public_key/ed25519.rb', line 8

def self.enabled?
  Object.const_defined?(:Ed25519)
end

Instance Method Details

#==(other) ⇒ Object

Is this public key equal to another public key?

other - Another SSHData::PublicKey::Base instance to compare with.

Returns boolean.



73
74
75
# File 'lib/ssh_data/public_key/ed25519.rb', line 73

def ==(other)
  super && other.pk == pk
end

#rfc4253Object

RFC4253 binary encoding of the public key.

Returns a binary String.



61
62
63
64
65
66
# File 'lib/ssh_data/public_key/ed25519.rb', line 61

def rfc4253
  Encoding.encode_fields(
    [:string, algo],
    [:string, pk],
  )
end

#verify(signed_data, signature) ⇒ Object

Verify an SSH signature.

signed_data - The String message that the signature was calculated over. signature - The binary String signature with SSH encoding.

Returns boolean.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ssh_data/public_key/ed25519.rb', line 43

def verify(signed_data, signature)
  self.class.ed25519_gem_required!

  sig_algo, raw_sig, _ = Encoding.decode_signature(signature)
  if sig_algo != self.class.algorithm_identifier
    raise DecodeError, "bad signature algorithm: #{sig_algo.inspect}"
  end

  begin
    ed25519_key.verify(raw_sig, signed_data)
  rescue Ed25519::VerifyError
    false
  end
end