Class: Gibberish::AES
- Inherits:
-
Object
- Object
- Gibberish::AES
- Defined in:
- lib/gibberish/aes.rb
Overview
Handles AES encryption and decryption in a way that is compatible with OpenSSL.
Defaults to 256-bit CBC encryption, ideally you should leave it this way
Basic Usage
Encrypting
cipher = Gibberish::AES.new('p4ssw0rd')
cipher.encrypt("some secret text")
#=> "U2FsdGVkX1/D7z2azGmmQELbMNJV/n9T/9j2iBPy2AM=\n"
cipher.encrypt_file("secret.txt", "secret.txt.enc")
Decrypting
cipher = Gibberish::AES.new('p4ssw0rd')
cipher.decrypt(""U2FsdGVkX1/D7z2azGmmQELbMNJV/n9T/9j2iBPy2AM=\n"")
#=> "some secret text"
cipher.decrypt_file("secret.txt.enc", "secret.txt")
OpenSSL Interop
echo "U2FsdGVkX1/D7z2azGmmQELbMNJV/n9T/9j2iBPy2AM=\n" | openssl enc -d -aes-256-cbc -a -k p4ssw0rd
openssl aes-256-cbc -d -in secret.txt.enc -out secret.txt -k p4ssw0rd
Constant Summary collapse
- BUFFER_SIZE =
4096
Instance Attribute Summary collapse
-
#cipher ⇒ Object
readonly
Returns the value of attribute cipher.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #decrypt(data, opts = {}) ⇒ Object (also: #dec, #d)
- #decrypt_file(from_file, to_file) ⇒ Object (also: #dec_file, #df)
- #decrypt_stream(in_stream, out_stream) ⇒ Object
- #encrypt(data, opts = {}) ⇒ Object (also: #enc, #e)
- #encrypt_file(from_file, to_file, opts = {}) ⇒ Object (also: #enc_file, #ef)
- #encrypt_stream(in_stream, out_stream, opts = {}) ⇒ Object
-
#initialize(password, size = 256, mode = "cbc") ⇒ AES
constructor
Initialize with the password.
Constructor Details
#initialize(password, size = 256, mode = "cbc") ⇒ AES
Initialize with the password
40 41 42 43 44 45 |
# File 'lib/gibberish/aes.rb', line 40 def initialize(password, size=256, mode="cbc") @password = password @size = size @mode = mode @cipher = OpenSSL::Cipher::Cipher.new("aes-#{size}-#{mode}") end |
Instance Attribute Details
#cipher ⇒ Object (readonly)
Returns the value of attribute cipher.
33 34 35 |
# File 'lib/gibberish/aes.rb', line 33 def cipher @cipher end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
33 34 35 |
# File 'lib/gibberish/aes.rb', line 33 def password @password end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
33 34 35 |
# File 'lib/gibberish/aes.rb', line 33 def size @size end |
Instance Method Details
#decrypt(data, opts = {}) ⇒ Object Also known as: dec, d
57 58 59 60 61 62 63 |
# File 'lib/gibberish/aes.rb', line 57 def decrypt(data, opts={}) data = Base64.decode64(data) unless opts[:binary] salt = data[8..15] data = data[16..-1] setup_cipher(:decrypt, salt) cipher.update(data) + cipher.final end |
#decrypt_file(from_file, to_file) ⇒ Object Also known as: dec_file, df
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/gibberish/aes.rb', line 84 def decrypt_file(from_file, to_file) buf = "" salt = "" File.open(to_file, "wb") do |outf| File.open(from_file, "rb") do |inf| inf.seek(8, IO::SEEK_SET) inf.read(8, salt) setup_cipher(:decrypt, salt) while inf.read(4096, buf) outf << self.cipher.update(buf) end outf << self.cipher.final end end end |
#decrypt_stream(in_stream, out_stream) ⇒ Object
109 110 111 112 113 114 |
# File 'lib/gibberish/aes.rb', line 109 def decrypt_stream(in_stream, out_stream) header = in_stream.read(16) salt = header[8..15] setup_cipher(:decrypt, salt) copy_stream in_stream, out_stream end |
#encrypt(data, opts = {}) ⇒ Object Also known as: enc, e
47 48 49 50 51 52 53 |
# File 'lib/gibberish/aes.rb', line 47 def encrypt(data, opts={}) salt = generate_salt(opts[:salt]) setup_cipher(:encrypt, salt) e = cipher.update(data) + cipher.final e = "Salted__#{salt}#{e}" #OpenSSL compatible opts[:binary] ? e : Base64.encode64(e) end |
#encrypt_file(from_file, to_file, opts = {}) ⇒ Object Also known as: enc_file, ef
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/gibberish/aes.rb', line 67 def encrypt_file(from_file, to_file, opts={}) salt = generate_salt(opts[:salt]) setup_cipher(:encrypt, salt) buf = "" File.open(to_file, "wb") do |outf| outf << "Salted__#{salt}" File.open(from_file, "rb") do |inf| while inf.read(4096, buf) outf << self.cipher.update(buf) end outf << self.cipher.final end end end |
#encrypt_stream(in_stream, out_stream, opts = {}) ⇒ Object
102 103 104 105 106 107 |
# File 'lib/gibberish/aes.rb', line 102 def encrypt_stream(in_stream, out_stream, opts={}) salt = generate_salt(opts[:salt]) setup_cipher(:encrypt, salt) out_stream << "Salted__#{salt}" copy_stream in_stream, out_stream end |