Class: OMQ::Blake3ZMQ::Crypto::PrivateKey

Inherits:
Object
  • Object
show all
Defined in:
lib/omq/blake3zmq/crypto.rb

Overview

X25519 private key wrapper with key generation and Diffie-Hellman.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bytes) ⇒ PrivateKey

Returns a new instance of PrivateKey.

Parameters:

  • bytes (String)

    32-byte secret key



51
52
53
# File 'lib/omq/blake3zmq/crypto.rb', line 51

def initialize(bytes)
  @key = X25519::Scalar.new(bytes.b)
end

Class Method Details

.generatePrivateKey

Generates a new random private key.

Returns:



45
46
47
# File 'lib/omq/blake3zmq/crypto.rb', line 45

def self.generate
  new(X25519::Scalar.generate.to_bytes)
end

Instance Method Details

#diffie_hellman(peer_public_key) ⇒ String

Performs X25519 Diffie-Hellman with a peer’s public key.

Parameters:

  • peer_public_key (PublicKey)

    peer’s public key

Returns:

  • (String)

    32-byte shared secret



76
77
78
79
80
81
82
83
84
85
# File 'lib/omq/blake3zmq/crypto.rb', line 76

def diffie_hellman(peer_public_key)
  pk = case peer_public_key
       when PublicKey
         peer_public_key.key
       else
         X25519::MontgomeryU.new(peer_public_key.to_s.b)
       end

  @key.diffie_hellman(pk).to_bytes
end

#public_keyPublicKey

Returns the corresponding public key.

Returns:



59
60
61
# File 'lib/omq/blake3zmq/crypto.rb', line 59

def public_key
  PublicKey.new(@key.public_key.to_bytes)
end

#to_sString

Returns the raw 32-byte secret key.

Returns:

  • (String)

    32-byte binary string



67
68
69
# File 'lib/omq/blake3zmq/crypto.rb', line 67

def to_s
  @key.to_bytes
end