Class: SSHData::PrivateKey::ED25519

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

Instance Attribute Summary collapse

Attributes inherited from Base

#algo, #comment, #public_key

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#issue_certificate

Constructor Details

#initialize(algo:, pk:, sk:, comment:) ⇒ ED25519

Returns a new instance of ED25519.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ssh_data/private_key/ed25519.rb', line 28

def initialize(algo:, pk:, sk:, comment:)
  unless algo == PublicKey::ALGO_ED25519
    raise DecodeError, "bad algorithm: #{algo.inspect}"
  end

  # openssh stores the pk twice, once as half of the sk...
  if sk.bytesize != 64 || sk.byteslice(32, 32) != pk
    raise DecodeError, "bad sk"
  end

  @pk = pk
  @sk = sk

  super(algo: algo, comment: comment)

  if PublicKey::ED25519.enabled?
    @ed25519_key = Ed25519::SigningKey.new(sk.byteslice(0, 32))

    if @ed25519_key.verify_key.to_bytes != pk
      raise DecodeError, "bad pk"
    end
  end

  @public_key = PublicKey::ED25519.new(algo: algo, pk: pk)
end

Instance Attribute Details

#ed25519_keyObject (readonly)

Returns the value of attribute ed25519_key.



4
5
6
# File 'lib/ssh_data/private_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/private_key/ed25519.rb', line 4

def pk
  @pk
end

#skObject (readonly)

Returns the value of attribute sk.



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

def sk
  @sk
end

Class Method Details

.from_ed25519(key) ⇒ Object

Create from a ::Ed25519::SigningKey instance.

key - A ::Ed25519::SigningKey instance.

Returns a ED25519 instance.



19
20
21
22
23
24
25
26
# File 'lib/ssh_data/private_key/ed25519.rb', line 19

def self.from_ed25519(key)
  new(
    algo: PublicKey::ALGO_ED25519,
    pk: key.verify_key.to_bytes,
    sk: key.to_bytes + key.verify_key.to_bytes,
    comment: "",
  )
end

.generateObject

Generate a new private key.

Returns a PublicKey::Base subclass instance.



9
10
11
12
# File 'lib/ssh_data/private_key/ed25519.rb', line 9

def self.generate
  PublicKey::ED25519.ed25519_gem_required!
  from_ed25519(Ed25519::SigningKey.generate)
end

Instance Method Details

#sign(signed_data, algo: nil) ⇒ Object

Make an SSH signature.

signed_data - The String message over which to calculated the signature.

Returns a binary String signature.

Raises:



59
60
61
62
63
64
65
# File 'lib/ssh_data/private_key/ed25519.rb', line 59

def sign(signed_data, algo: nil)
  PublicKey::ED25519.ed25519_gem_required!
  algo ||= self.algo
  raise AlgorithmError unless algo == self.algo
  raw_sig = ed25519_key.sign(signed_data)
  Encoding.encode_signature(algo, raw_sig)
end