Class: Cryptor::SymmetricEncryption
- Inherits:
-
Object
- Object
- Cryptor::SymmetricEncryption
- Defined in:
- lib/cryptor/symmetric_encryption.rb
Overview
Easy-to-use authenticated symmetric encryption
Class Method Summary collapse
Instance Method Summary collapse
- #decrypt(ciphertext) ⇒ Object
- #encrypt(plaintext) ⇒ Object
-
#initialize(key) ⇒ SymmetricEncryption
constructor
A new instance of SymmetricEncryption.
Constructor Details
#initialize(key) ⇒ SymmetricEncryption
Returns a new instance of SymmetricEncryption.
13 14 15 |
# File 'lib/cryptor/symmetric_encryption.rb', line 13 def initialize(key) @key = key end |
Class Method Details
.random_key(cipher) ⇒ Object
9 10 11 |
# File 'lib/cryptor/symmetric_encryption.rb', line 9 def self.random_key(cipher) Cipher[cipher].random_key end |
Instance Method Details
#decrypt(ciphertext) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/cryptor/symmetric_encryption.rb', line 30 def decrypt(ciphertext) begin = ORDO::Message.parse(ciphertext) rescue ORDO::ParseError => ex raise InvalidMessageError, ex.to_s end fingerprint = ['Key-Fingerprint'] fail ArgumentError, "no key configured for: #{fingerprint}" if @key.fingerprint != fingerprint @key.decrypt .body end |
#encrypt(plaintext) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/cryptor/symmetric_encryption.rb', line 17 def encrypt(plaintext) ciphertext = @key.encrypt(plaintext) base64 = Base64.strict_encode64(ciphertext) ORDO::Message.new( base64, 'Cipher' => @key.cipher.algorithm, 'Content-Length' => base64.bytesize, 'Content-Transfer-Encoding' => 'base64', 'Key-Fingerprint' => @key.fingerprint ).to_string end |