Module: Duse::Encryption::Asymmetric

Extended by:
Asymmetric, Digest, Encoding
Included in:
Asymmetric
Defined in:
lib/duse/encryption.rb

Constant Summary collapse

PADDING =
OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING

Instance Method Summary collapse

Methods included from Encoding

decode, encode

Methods included from Digest

digest

Instance Method Details

#decrypt(private_key, text) ⇒ Object



40
41
42
# File 'lib/duse/encryption.rb', line 40

def decrypt(private_key, text)
  private_key.private_decrypt(decode(text), PADDING).force_encoding('utf-8')
end

#encrypt(private_key, public_key, text) ⇒ Object



30
31
32
33
34
# File 'lib/duse/encryption.rb', line 30

def encrypt(private_key, public_key, text)
  encrypted = public_key.public_encrypt text.force_encoding('ascii-8bit'), PADDING
  signature = sign(private_key, encrypted)
  [encode(encrypted), signature]
end

#sign(private_key, text) ⇒ Object



36
37
38
# File 'lib/duse/encryption.rb', line 36

def sign(private_key, text)
  encode(private_key.sign(digest, text))
end

#verify(public_key, signature, encrypted) ⇒ Object



44
45
46
# File 'lib/duse/encryption.rb', line 44

def verify(public_key, signature, encrypted)
  public_key.verify digest, decode(signature), decode(encrypted)
end