Class: Ed25519::SigningKey

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

Overview

Private key for producing digital signatures

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seed) ⇒ SigningKey

Create a new Ed25519::SigningKey from the given seed value

Parameters:

  • seed (String)

    32-byte seed value from which the key should be derived



30
31
32
33
34
35
36
# File 'lib/ed25519/signing_key.rb', line 30

def initialize(seed)
  Ed25519.validate_key_bytes(seed)

  @seed = seed
  @keypair = Ed25519.provider.create_keypair(seed)
  @verify_key = VerifyKey.new(@keypair[32, 32])
end

Instance Attribute Details

#keypairObject (readonly)

Returns the value of attribute keypair.



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

def keypair
  @keypair
end

#seedObject (readonly)

Returns the value of attribute seed.



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

def seed
  @seed
end

#verify_keyObject (readonly)

Returns the value of attribute verify_key.



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

def verify_key
  @verify_key
end

Class Method Details

.from_keypair(keypair) ⇒ Object

Create a SigningKey from a 64-byte Ed25519 keypair (i.e. public + private)

Parameters:

  • keypair (String)

    64-byte keypair value containing both seed + public key

Raises:

  • (TypeError)


18
19
20
21
22
23
24
25
# File 'lib/ed25519/signing_key.rb', line 18

def self.from_keypair(keypair)
  raise TypeError, "expected String, got #{keypair.class}" unless keypair.is_a?(String)
  raise ArgumentError, "expected 64-byte String, got #{keypair.bytesize}" unless keypair.bytesize == 64

  new(keypair[0, KEY_SIZE]).tap do |key|
    raise ArgumentError, "corrupt keypair" unless keypair[KEY_SIZE, KEY_SIZE] == key.verify_key.to_bytes
  end
end

.generateObject

Generate a random Ed25519 signing key (i.e. private scalar)



11
12
13
# File 'lib/ed25519/signing_key.rb', line 11

def self.generate
  new SecureRandom.random_bytes(Ed25519::KEY_SIZE)
end

Instance Method Details

#inspectObject

String inspection that does not leak secret values



48
49
50
# File 'lib/ed25519/signing_key.rb', line 48

def inspect
  to_s
end

#sign(message) ⇒ String

Sign the given message, returning an Ed25519 signature

Parameters:

  • message (String)

    message to be signed

Returns:

  • (String)

    64-byte Ed25519 signature



43
44
45
# File 'lib/ed25519/signing_key.rb', line 43

def sign(message)
  Ed25519.provider.sign(@keypair, message)
end

#to_bytesString Also known as: to_str

Return a bytestring representation of this signing key

Returns:

  • (String)

    signing key converted to a bytestring



55
56
57
# File 'lib/ed25519/signing_key.rb', line 55

def to_bytes
  seed
end