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 = String(bytes).b
  @seed = seed.nil? ? nil : String(seed).b
  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 = String(seed).b
  _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?



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

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

#hashObject



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

def hash
  object_id.hash
end

#inspectObject



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

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

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



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

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

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



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

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
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/pq_crypto/signature.rb', line 374

def to_pkcs8_der(format: :expanded, passphrase: nil, iterations: PKCS8::ENCRYPTED_PKCS8_DEFAULT_ITERATIONS)
  case format
  when :expanded
    PKCS8.encode_der(@algorithm, @bytes, format: :expanded, passphrase: passphrase, iterations: iterations)
  when :seed
    ensure_seed_available!(format)
    PKCS8.encode_der(@algorithm, @seed, format: :seed, passphrase: passphrase, iterations: iterations)
  when :both
    ensure_seed_available!(format)
    PKCS8.encode_der(@algorithm, [@seed, @bytes], format: :both, passphrase: passphrase, iterations: iterations)
  else
    raise SerializationError, "Unsupported PKCS#8 private key format: #{format.inspect}"
  end
end

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



389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/pq_crypto/signature.rb', line 389

def to_pkcs8_pem(format: :expanded, passphrase: nil, iterations: PKCS8::ENCRYPTED_PKCS8_DEFAULT_ITERATIONS)
  case format
  when :expanded
    PKCS8.encode_pem(@algorithm, @bytes, format: :expanded, passphrase: passphrase, iterations: iterations)
  when :seed
    ensure_seed_available!(format)
    PKCS8.encode_pem(@algorithm, @seed, format: :seed, passphrase: passphrase, iterations: iterations)
  when :both
    ensure_seed_available!(format)
    PKCS8.encode_pem(@algorithm, [@seed, @bytes], format: :both, passphrase: passphrase, iterations: iterations)
  else
    raise SerializationError, "Unsupported PKCS#8 private key format: #{format.inspect}"
  end
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



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

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