Class: Scl::RSA::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/scl/rsa.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rsa) ⇒ Key

Returns a new instance of Key.



37
38
39
40
# File 'lib/scl/rsa.rb', line 37

def initialize(rsa)
  @rsa = rsa
  @aes = Scl::AES.new
end

Instance Attribute Details

#rsaObject (readonly)

Returns the value of attribute rsa.



36
37
38
# File 'lib/scl/rsa.rb', line 36

def rsa
  @rsa
end

Instance Method Details

#decrypt(ciphertext) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/scl/rsa.rb', line 60

def decrypt(ciphertext)
  encrypted_key, iv, ciphertext = ciphertext.split(DELIMITER, 3)
  decrypted_key =
    case
    when rsa.private? then rsa.private_decrypt(encrypted_key)
    else rsa.public_decrypt(encrypted_key)
    end
  @aes.decrypt(ciphertext, decrypted_key, iv)
end

#encrypt(plaintext, key = nil, iv = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/scl/rsa.rb', line 50

def encrypt(plaintext, key=nil, iv=nil)
  ciphertext, key, iv = @aes.encrypt(plaintext, key, iv)
  encrypted_key =
    case
    when rsa.private? then rsa.private_encrypt(key)
    else rsa.public_encrypt(key)
    end
  [encrypted_key, iv, ciphertext].join(DELIMITER)
end

#exportObject



70
71
72
# File 'lib/scl/rsa.rb', line 70

def export
  rsa.export
end

#sign(data) ⇒ Object



42
43
44
# File 'lib/scl/rsa.rb', line 42

def sign(data)
  rsa.sign(OpenSSL::Digest::SHA256.new, data)
end

#verify(signature, data) ⇒ Object



46
47
48
# File 'lib/scl/rsa.rb', line 46

def verify(signature, data)
  rsa.verify(OpenSSL::Digest::SHA256.new, signature, data)
end