Class: PQCrypto::Signature::SecretKey

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(algorithm, bytes) ⇒ SecretKey

Returns a new instance of SecretKey.



340
341
342
343
344
# File 'lib/pq_crypto/signature.rb', line 340

def initialize(algorithm, bytes)
  @algorithm = algorithm
  @bytes = String(bytes).b
  validate_length!
end

Instance Attribute Details

#algorithmObject (readonly)

Returns the value of attribute algorithm.



338
339
340
# File 'lib/pq_crypto/signature.rb', line 338

def algorithm
  @algorithm
end

Instance Method Details

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



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

def ==(other)
  return false unless other.is_a?(SecretKey) && other.algorithm == algorithm
  PQCrypto.__send__(:native_ct_equals, other.to_bytes, @bytes)
end

#hashObject



404
405
406
# File 'lib/pq_crypto/signature.rb', line 404

def hash
  object_id.hash
end

#inspectObject



408
409
410
# File 'lib/pq_crypto/signature.rb', line 408

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

#sign(message) ⇒ Object



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

def sign(message)
  PQCrypto.__send__(Signature.send(:native_method_for, @algorithm, :sign), String(message).b, @bytes)
rescue ArgumentError => e
  raise InvalidKeyError, e.message
end

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



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

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

#to_bytesObject



346
347
348
# File 'lib/pq_crypto/signature.rb', line 346

def to_bytes
  @bytes.dup
end

#to_pkcs8_der(format: :expanded) ⇒ Object



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

def to_pkcs8_der(format: :expanded)
  case format
  when :expanded
    PKCS8.encode_der(@algorithm, @bytes, format: :expanded)
  when :seed, :both
    raise SerializationError,
          "ML-DSA seed/both PKCS#8 export requires original seed material; use PQCrypto::PKCS8.encode_der/encode_pem directly"
  else
    raise SerializationError, "Unsupported PKCS#8 private key format: #{format.inspect}"
  end
end

#to_pkcs8_pem(format: :expanded) ⇒ Object



370
371
372
373
374
375
376
377
378
379
380
# File 'lib/pq_crypto/signature.rb', line 370

def to_pkcs8_pem(format: :expanded)
  case format
  when :expanded
    PKCS8.encode_pem(@algorithm, @bytes, format: :expanded)
  when :seed, :both
    raise SerializationError,
          "ML-DSA seed/both PKCS#8 export requires original seed material; use PQCrypto::PKCS8.encode_der/encode_pem directly"
  else
    raise SerializationError, "Unsupported PKCS#8 private key format: #{format.inspect}"
  end
end

#to_pqc_container_derObject



350
351
352
# File 'lib/pq_crypto/signature.rb', line 350

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

#to_pqc_container_pemObject



354
355
356
# File 'lib/pq_crypto/signature.rb', line 354

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

#wipe!Object



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

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