Class: SimplyAES::Cipher
- Inherits:
-
Object
- Object
- SimplyAES::Cipher
- Defined in:
- lib/simply-aes/cipher.rb,
lib/simply-aes/cipher/error.rb
Overview
Defined Under Namespace
Classes: Error
Constant Summary collapse
Instance Method Summary collapse
- #dump(plaintext, options = {}) ⇒ Object (also: #encrypt)
- #format(options = {}) ⇒ SimplyAES::Format private
-
#initialize(*args) ⇒ Cipher
constructor
rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.
- #inspect ⇒ String private
-
#key(options = {}) ⇒ String
Formatted string.
- #load(iv_ciphertext, options = {}) ⇒ Object (also: #decrypt)
Constructor Details
#initialize(options) ⇒ Cipher #initialize(key, options) ⇒ Cipher
rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/simply-aes/cipher.rb', line 23 def initialize(*args) = (args.last.is_a?(Hash) ? args.pop.dup : {}) @format = Format[.delete(:format) { :bytes }] # extract the given key, or securely generate one @key = format.load(args.pop) unless args.empty? @key ||= native_cipher.random_key # validate initialisation fail(ArgumentError, 'invalid key length') unless @key.bytesize == 32 fail(ArgumentError, 'wrong number of arguments') unless args.empty? fail(ArgumentError, "unknown options: #{}") unless .empty? rescue => err raise Error, "failed to initialize #{self.class.name} (#{err})" end |
Instance Method Details
#dump(plaintext, options = {}) ⇒ Object Also known as: encrypt
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/simply-aes/cipher.rb', line 55 def dump(plaintext, = {}) encipher = native_cipher(:encrypt) # ensure a 16-byte initialisation vector iv = .fetch(:iv) { encipher.random_iv } fail(ArgumentError, 'iv must be 16 bytes') unless iv.bytesize == 16 encipher.iv = iv ciphertext = encipher.update(plaintext) + encipher.final format().dump(iv + ciphertext) rescue => err raise DumpError, err. end |
#format(options = {}) ⇒ SimplyAES::Format
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
102 103 104 |
# File 'lib/simply-aes/cipher.rb', line 102 def format( = {}) Format[.fetch(:format) { @format }] end |
#inspect ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
96 97 98 |
# File 'lib/simply-aes/cipher.rb', line 96 def inspect "<#{self.class.name}:#{__id__}>" end |
#key(options = {}) ⇒ String
Returns formatted string.
44 45 46 |
# File 'lib/simply-aes/cipher.rb', line 44 def key( = {}) format().dump(@key.dup) end |
#load(iv_ciphertext, options = {}) ⇒ Object Also known as: decrypt
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/simply-aes/cipher.rb', line 75 def load(iv_ciphertext, = {}) @key || fail(ArgumentError, 'key not provided!') # if the IV is given as an argument, inject it to the ciphertext given_iv = [:iv] given_iv && (iv_ciphertext = given_iv + iv_ciphertext) # shift the 16-byte initialisation vector from the front iv, ciphertext = format().load(iv_ciphertext).unpack('a16a*') decipher = native_cipher(:decrypt) decipher.iv = iv decipher.update(ciphertext) + decipher.final rescue => err raise LoadError, err. end |