Module: AesShared
- Included in:
- AesAlg
- Defined in:
- lib/ruby-aes/aes_shared.rb
Overview
This file is a part of ruby-aes <rubyforge.org/projects/ruby-aes>
Written by Alex Boussinet <[email protected]>
It contains the code shared by all the implementations
Instance Method Summary collapse
- #decrypt_blocks(buffer) ⇒ Object
- #decrypt_buffer(buffer) ⇒ Object
- #encrypt_blocks(buffer) ⇒ Object
- #encrypt_buffer(buffer) ⇒ Object
Instance Method Details
#decrypt_blocks(buffer) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/ruby-aes/aes_shared.rb', line 23 def decrypt_blocks(buffer) raise "Bad block length" unless (buffer.length % 16).zero? pt = "" block = "" buffer.each_byte do |char| block << char if block.length == 16 pt << decrypt_block(block) block = "" end end pt end |
#decrypt_buffer(buffer) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/ruby-aes/aes_shared.rb', line 55 def decrypt_buffer(buffer) pt = "" block = "" buffer.each_byte do |char| block << char if block.length == 16 pt << decrypt_block(block) block = "" end end if block.length != 1 raise 'Bad Block Padding' elsif (c = block[-1]).zero? pt else if block * c == pt[-c..-1] pt[0..-c-1] else raise "Bad Block Padding" end end end |
#encrypt_blocks(buffer) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/ruby-aes/aes_shared.rb', line 9 def encrypt_blocks(buffer) raise "Bad block length" unless (buffer.length % 16).zero? ct = "" block = "" buffer.each_byte do |char| block << char if block.length == 16 ct << encrypt_block(block) block = "" end end ct end |
#encrypt_buffer(buffer) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/ruby-aes/aes_shared.rb', line 37 def encrypt_buffer(buffer) ct = "" block = "" buffer.each_byte do |char| block << char if block.length == 16 ct << encrypt_block(block) block = "" end end c = "\000" if (m = 16 - block.length % 16) != 16 c = m.chr ct << encrypt_block(block << c * m) end ct << c end |