Class: PQCrypto::Signature::SecretKey

Inherits:
Object
  • Object
show all
Defined in:
lib/pq_crypto/signature.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(algorithm, bytes, seed: nil, freshly_generated: false) ⇒ SecretKey

Returns a new instance of SecretKey.



360
361
362
363
364
365
366
367
# File 'lib/pq_crypto/signature.rb', line 360

def initialize(algorithm, bytes, seed: nil, freshly_generated: false)
  @algorithm = algorithm
  @bytes = Internal.binary_string(bytes)
  @seed = seed.nil? ? nil : Internal.binary_string(seed)
  validate_length!
  validate_seed_length! if @seed
  validate_structure! unless freshly_generated
end

Instance Attribute Details

#algorithmObject (readonly)

Returns the value of attribute algorithm.



358
359
360
# File 'lib/pq_crypto/signature.rb', line 358

def algorithm
  @algorithm
end

Class Method Details

.from_seed(algorithm, seed) ⇒ Object



375
376
377
378
379
380
381
# File 'lib/pq_crypto/signature.rb', line 375

def self.from_seed(algorithm, seed)
  seed_bytes = Internal.binary_string(seed)
  _public_key, expanded = PQCrypto.__send__(Signature.send(:native_method_for, algorithm, :keypair_from_seed), seed_bytes)
  new(algorithm, expanded, seed: seed_bytes, freshly_generated: true)
rescue ArgumentError => e
  raise InvalidKeyError, e.message
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



422
423
424
425
# File 'lib/pq_crypto/signature.rb', line 422

def ==(other)
  return false unless other.is_a?(SecretKey) && other.algorithm == algorithm
  Internal.constant_time_equal?(other.send(:bytes_for_native), @bytes)
end

#hashObject



429
430
431
# File 'lib/pq_crypto/signature.rb', line 429

def hash
  object_id.hash
end

#inspectObject



433
434
435
# File 'lib/pq_crypto/signature.rb', line 433

def inspect
  "#<#{self.class}:0x#{object_id.to_s(16)} algorithm=#{algorithm.inspect}>"
end

#sign(message, context: "".b) ⇒ Object



403
404
405
406
407
408
409
410
# File 'lib/pq_crypto/signature.rb', line 403

def sign(message, context: "".b)
  context = Signature.send(:validate_context!, context)
  begin
    PQCrypto.__send__(Signature.send(:native_method_for, @algorithm, :sign), Internal.binary_string(message), @bytes, context)
  rescue ArgumentError => e
    raise InvalidKeyError, e.message
  end
end

#sign_io(io, chunk_size: 1 << 20, context: "".b) ⇒ Object



412
413
414
# File 'lib/pq_crypto/signature.rb', line 412

def sign_io(io, chunk_size: 1 << 20, context: "".b)
  Signature.send(:_streaming_sign, self, io, chunk_size, context)
end

#to_bytesObject



383
384
385
# File 'lib/pq_crypto/signature.rb', line 383

def to_bytes
  @bytes.dup
end

#to_pkcs8_der(format: :expanded, passphrase: nil, iterations: PKCS8::ENCRYPTED_PKCS8_DEFAULT_ITERATIONS) ⇒ Object



395
396
397
# File 'lib/pq_crypto/signature.rb', line 395

def to_pkcs8_der(format: :expanded, passphrase: nil, iterations: PKCS8::ENCRYPTED_PKCS8_DEFAULT_ITERATIONS)
  PKCS8.encode_der(@algorithm, pkcs8_material(format), format: format, passphrase: passphrase, iterations: iterations)
end

#to_pkcs8_pem(format: :expanded, passphrase: nil, iterations: PKCS8::ENCRYPTED_PKCS8_DEFAULT_ITERATIONS) ⇒ Object



399
400
401
# File 'lib/pq_crypto/signature.rb', line 399

def to_pkcs8_pem(format: :expanded, passphrase: nil, iterations: PKCS8::ENCRYPTED_PKCS8_DEFAULT_ITERATIONS)
  PKCS8.encode_pem(@algorithm, pkcs8_material(format), format: format, passphrase: passphrase, iterations: iterations)
end

#to_pqc_container_derObject



387
388
389
# File 'lib/pq_crypto/signature.rb', line 387

def to_pqc_container_der
  Serialization.secret_key_to_pqc_container_der(@algorithm, @bytes)
end

#to_pqc_container_pemObject



391
392
393
# File 'lib/pq_crypto/signature.rb', line 391

def to_pqc_container_pem
  Serialization.secret_key_to_pqc_container_pem(@algorithm, @bytes)
end

#valid?Boolean

Returns:

  • (Boolean)


369
370
371
372
373
# File 'lib/pq_crypto/signature.rb', line 369

def valid?
  return true unless Signature.send(:secret_key_checkable?, @algorithm)

  PQCrypto.__send__(Signature.send(:native_method_for, @algorithm, :check_secret_key), @bytes)
end

#wipe!Object



416
417
418
419
420
# File 'lib/pq_crypto/signature.rb', line 416

def wipe!
  PQCrypto.secure_wipe(@bytes)
  PQCrypto.secure_wipe(@seed) if @seed
  self
end