Class: Cryptosphere::AsymmetricCipher

Inherits:
Object
  • Object
show all
Defined in:
lib/cryptosphere/crypto/asymmetric_cipher.rb

Overview

Asymmetric encryption cipher: 2048-bit RSA

Constant Summary collapse

KEY_SIZE =
2048

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ AsymmetricCipher

Returns a new instance of AsymmetricCipher.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/cryptosphere/crypto/asymmetric_cipher.rb', line 12

def initialize(key)
  openssl_key = OpenSSL::PKey::RSA.new(key)

  if openssl_key.private?
    @private_key = openssl_key
    @public_key  = openssl_key.public_key
  else
    @private_key = nil
    @public_key  = openssl_key
  end
end

Class Method Details

.generate_keyObject



8
9
10
# File 'lib/cryptosphere/crypto/asymmetric_cipher.rb', line 8

def self.generate_key
  OpenSSL::PKey::RSA.generate(KEY_SIZE).to_pem
end

Instance Method Details

#private_decrypt(ciphertext) ⇒ Object

Decrypt a value using the private key Ciphertext must be encrypted with public key



57
58
59
# File 'lib/cryptosphere/crypto/asymmetric_cipher.rb', line 57

def private_decrypt(ciphertext)
  @private_key.private_decrypt(ciphertext)
end

#private_encrypt(plaintext) ⇒ Object

Encrypt a value using the private key Value can be decrypted with the public key



51
52
53
# File 'lib/cryptosphere/crypto/asymmetric_cipher.rb', line 51

def private_encrypt(plaintext)
  @private_key.private_encrypt(plaintext)
end

#private_keyObject

Serialize canonical private key with Distinguished Encoding Rules (DER)



25
26
27
# File 'lib/cryptosphere/crypto/asymmetric_cipher.rb', line 25

def private_key
  @private_key.to_der
end

#private_key?Boolean

Is a private key present?

Returns:

  • (Boolean)


35
36
37
# File 'lib/cryptosphere/crypto/asymmetric_cipher.rb', line 35

def private_key?
  !!@private_key
end

#private_key_pemObject

Serialize private key in Privacy Enhanced Mail (PEM) format



30
31
32
# File 'lib/cryptosphere/crypto/asymmetric_cipher.rb', line 30

def private_key_pem
  @private_key.to_pem
end

#public_decrypt(ciphertext) ⇒ Object

Decrypt a value using the public key Ciphertext must be encrypted with private key



69
70
71
# File 'lib/cryptosphere/crypto/asymmetric_cipher.rb', line 69

def public_decrypt(ciphertext)
  @public_key.public_decrypt(ciphertext)
end

#public_encrypt(plaintext) ⇒ Object

Encrypt a value using the public key Value can only be decrypted with the private key



63
64
65
# File 'lib/cryptosphere/crypto/asymmetric_cipher.rb', line 63

def public_encrypt(plaintext)
  @public_key.public_encrypt(plaintext)
end

#public_keyObject

Serialize canonical public key with Distinguished Encoding Rules (DER)



40
41
42
# File 'lib/cryptosphere/crypto/asymmetric_cipher.rb', line 40

def public_key
  @public_key.to_der
end

#public_key_fingerprintObject

Obtain the fingerprint for this public key



45
46
47
# File 'lib/cryptosphere/crypto/asymmetric_cipher.rb', line 45

def public_key_fingerprint
  Cryptosphere.kdf(public_key).unpack('H*').first.scan(/.{4}/).join(":")
end