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) ⇒ SecretKey

Returns a new instance of SecretKey.



346
347
348
349
350
351
352
# File 'lib/pq_crypto/signature.rb', line 346

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

Instance Attribute Details

#algorithmObject (readonly)

Returns the value of attribute algorithm.



344
345
346
# File 'lib/pq_crypto/signature.rb', line 344

def algorithm
  @algorithm
end

Class Method Details

.from_seed(algorithm, seed) ⇒ Object



354
355
356
357
358
359
360
# File 'lib/pq_crypto/signature.rb', line 354

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)
rescue ArgumentError => e
  raise InvalidKeyError, e.message
end

Instance Method Details

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



401
402
403
404
# File 'lib/pq_crypto/signature.rb', line 401

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

#hashObject



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

def hash
  object_id.hash
end

#inspectObject



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

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

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



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

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



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

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

#to_bytesObject



362
363
364
# File 'lib/pq_crypto/signature.rb', line 362

def to_bytes
  @bytes.dup
end

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



374
375
376
# File 'lib/pq_crypto/signature.rb', line 374

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



378
379
380
# File 'lib/pq_crypto/signature.rb', line 378

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



366
367
368
# File 'lib/pq_crypto/signature.rb', line 366

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

#to_pqc_container_pemObject



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

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

#wipe!Object



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

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