Class: Ronin::Support::Crypto::Cipher
- Inherits:
-
OpenSSL::Cipher
- Object
- OpenSSL::Cipher
- Ronin::Support::Crypto::Cipher
- Defined in:
- lib/ronin/support/crypto/cipher.rb,
lib/ronin/support/crypto/cipher/aes.rb,
lib/ronin/support/crypto/cipher/aes128.rb,
lib/ronin/support/crypto/cipher/aes256.rb
Overview
Represents a cryptographic cipher.
Examples
Encrypt Data
aes256 = Crypto::Cipher.new('aes-256-cbc', direction: :encrypt,
password: 'secret')
aes256.encrypt("message in a bottle")
# => "\x18\xC7\x00~\xA2\xA1\x80\x84c\x98,81mo\xBAZ\xDD\xF4\xF2\xEF\xA9\xDE\xB3\xD6!\xB9\xA8WT\x9D\xE0"
Decrypt Data
aes256 = Crypto::Cipher.new('aes-256-cbc', direction: :decrypt,
password: 'secret')
aes256.decrypt("\x18\xC7\x00~\xA2\xA1\x80\x84c\x98,81mo\xBAZ\xDD\xF4\xF2\xEF\xA9\xDE\xB3\xD6!\xB9\xA8WT\x9D\xE0")
# => "message in a bottle"
Direct Known Subclasses
Defined Under Namespace
Class Method Summary collapse
-
.supported ⇒ Array<String>
The list of supported ciphers.
Instance Method Summary collapse
-
#decrypt(data = nil) ⇒ String
Decrypts the given data.
-
#encrypt(data = nil) ⇒ String
Encrypts the given data.
-
#initialize(name, direction:, key: nil, hash: :sha256, password: nil, iv: nil, padding: nil) ⇒ Cipher
constructor
Initializes the cipher.
-
#stream(io, block_size: 16384, output: nil) {|block| ... } ⇒ String
Pipes the IO stream through the cipher.
Constructor Details
#initialize(name, direction:, key: nil, hash: :sha256, password: nil, iv: nil, padding: nil) ⇒ Cipher
Initializes the cipher.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/ronin/support/crypto/cipher.rb', line 79 def initialize(name, direction: , key: nil, hash: :sha256, password: nil, iv: nil, padding: nil) super(name) case direction when :encrypt then self.encrypt when :decrypt then self.decrypt end if password && hash self.key = OpenSSL::Digest.const_get(hash.upcase).digest(password) elsif key self.key = key else raise(ArgumentError,"the the key: or password: keyword argument must be given") end self.iv = iv if iv self.padding = padding if padding end |
Class Method Details
Instance Method Details
#decrypt(data = nil) ⇒ String
Decrypts the given data.
140 141 142 143 144 145 146 |
# File 'lib/ronin/support/crypto/cipher.rb', line 140 def decrypt(data=nil) if data update(data) + final else super() end end |
#encrypt(data = nil) ⇒ String
Encrypts the given data.
123 124 125 126 127 128 129 |
# File 'lib/ronin/support/crypto/cipher.rb', line 123 def encrypt(data=nil) if data update(data) + final else super() end end |
#stream(io, block_size: 16384, output: nil) {|block| ... } ⇒ String
Pipes the IO stream through the cipher.
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/ronin/support/crypto/cipher.rb', line 170 def stream(io, block_size: 16384, output: nil) unless block_given? output ||= String.new(encoding: Encoding::ASCII_8BIT) end until io.eof? block = update(io.read(block_size)) if block_given? then yield block else output << block end end if block_given? yield final else output << final return output end end |