Class: Voynich::AES
- Inherits:
-
Object
- Object
- Voynich::AES
- Defined in:
- lib/voynich/aes.rb
Constant Summary collapse
- AUTH_TAG_BITS =
128
- CIPHER_MODE =
'aes-256-gcm'
- DEFAULT_SERIALIZER =
Marshal
Instance Method Summary collapse
- #decrypt(content, iv:, tag:) ⇒ Object
- #deserialize(data) ⇒ Object
- #encrypt(plaintext) ⇒ Object
-
#initialize(secret, adata, serializer: DEFAULT_SERIALIZER) ⇒ AES
constructor
A new instance of AES.
- #serialize(data) ⇒ Object
Constructor Details
#initialize(secret, adata, serializer: DEFAULT_SERIALIZER) ⇒ AES
Returns a new instance of AES.
10 11 12 13 14 |
# File 'lib/voynich/aes.rb', line 10 def initialize(secret, adata, serializer: DEFAULT_SERIALIZER) @secret = secret @auth_data = adata @serializer = serializer end |
Instance Method Details
#decrypt(content, iv:, tag:) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/voynich/aes.rb', line 32 def decrypt(content, iv:, tag:) cipher = OpenSSL::Cipher.new(CIPHER_MODE) cipher.decrypt cipher.key = @secret cipher.iv = Base64.decode64(iv) cipher.auth_tag = Base64.decode64(tag) cipher.auth_data = @auth_data decrypted_data = cipher.update(Base64.decode64(content)) + cipher.final deserialize(decrypted_data) end |
#deserialize(data) ⇒ Object
47 48 49 |
# File 'lib/voynich/aes.rb', line 47 def deserialize(data) @serializer.load(data) end |
#encrypt(plaintext) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/voynich/aes.rb', line 16 def encrypt(plaintext) cipher = OpenSSL::Cipher.new(CIPHER_MODE) cipher.encrypt cipher.key = @secret iv = cipher.random_iv cipher.auth_data = @auth_data encrypted_data = cipher.update(serialize(plaintext)) + cipher.final tag = cipher.auth_tag(AUTH_TAG_BITS / 8) { content: Base64.strict_encode64(encrypted_data), tag: Base64.strict_encode64(tag), iv: Base64.strict_encode64(iv), auth_data: @auth_data } end |
#serialize(data) ⇒ Object
43 44 45 |
# File 'lib/voynich/aes.rb', line 43 def serialize(data) @serializer.dump(data) end |