Class: SymmetricEncryption::Reader
- Inherits:
-
Object
- Object
- SymmetricEncryption::Reader
- Defined in:
- lib/symmetric_encryption/reader.rb
Overview
Read from encrypted files and other IO streams
Features:
-
Decryption on the fly whilst reading files
-
Large file support by only buffering small amounts of data in memory
Instance Attribute Summary collapse
-
#pos ⇒ Object
readonly
Return the number of bytes read so far from the input stream.
-
#version ⇒ Object
readonly
Returns the Cipher encryption version used to encrypt this file Returns nil when the header was not present in the stream and no :version option was supplied.
Class Method Summary collapse
-
.decrypt(source:, target:, block_size: 65_535, **args) ⇒ Object
Decrypt an entire file.
-
.empty?(file_name_or_stream) ⇒ Boolean
Returns [true|false] whether the file or stream contains any data excluding the header should it have one.
-
.header_present?(file_name) ⇒ Boolean
Returns [true|false] whether the file contains the encryption header.
-
.open(file_name_or_stream, buffer_size: 16_384, **args, &block) ⇒ Object
Open a file for reading, or use the supplied IO Stream.
-
.read(file_name_or_stream, **args) ⇒ Object
Read the entire contents of a file or stream into memory.
Instance Method Summary collapse
-
#close(close_child_stream = true) ⇒ Object
Close the IO Stream.
-
#compressed? ⇒ Boolean
Returns whether the stream being read is compressed.
-
#each_line(sep_string = "\n") ⇒ Object
(also: #each)
ios.each(sep_string=“n”) {|line| block } => ios ios.each_line(sep_string=“n”) {|line| block } => ios Executes the block for every line in ios, where lines are separated by sep_string.
-
#eof? ⇒ Boolean
Returns whether the end of file has been reached for this stream.
-
#flush ⇒ Object
Flush the read stream Needed by XLS gem.
-
#gets(sep_string, length = nil) ⇒ Object
Reads a single decrypted line from the file up to and including the optional sep_string.
-
#header_present? ⇒ Boolean
After opening a file Returns [true|false] whether the file being read has an encryption header.
-
#initialize(ios, buffer_size: 4096, version: nil) ⇒ Reader
constructor
Decrypt data before reading from the supplied stream.
-
#read(length = nil) ⇒ Object
Read from the stream and return the decrypted data See IOS#read.
-
#readline(sep_string = "\n") ⇒ Object
Reads a single decrypted line from the file up to and including the optional sep_string.
-
#rewind ⇒ Object
Rewind back to the beginning of the file.
-
#seek(amount, whence = IO::SEEK_SET) ⇒ Object
Seeks to a given offset (Integer) in the stream according to the value of whence: IO::SEEK_CUR | Seeks to amount plus current position ————–+—————————————————- IO::SEEK_END | Seeks to amount plus end of stream (you probably | want a negative value for amount) ————–+—————————————————- IO::SEEK_SET | Seeks to the absolute location given by amount.
-
#size ⇒ Object
Return the size of the file rounded up to the nearest encryption block size Needed by XLS gem.
Constructor Details
#initialize(ios, buffer_size: 4096, version: nil) ⇒ Reader
Decrypt data before reading from the supplied stream
129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/symmetric_encryption/reader.rb', line 129 def initialize(ios, buffer_size: 4096, version: nil) @ios = ios @buffer_size = buffer_size @version = version @header_present = false @closed = false raise(ArgumentError, 'Buffer size cannot be smaller than 128') unless @buffer_size >= 128 read_header end |
Instance Attribute Details
#pos ⇒ Object (readonly)
Return the number of bytes read so far from the input stream
271 272 273 |
# File 'lib/symmetric_encryption/reader.rb', line 271 def pos @pos end |
#version ⇒ Object (readonly)
Returns the Cipher encryption version used to encrypt this file Returns nil when the header was not present in the stream and no :version
option was supplied
Note: When no header is present, the version is set to the one supplied
in the
162 163 164 |
# File 'lib/symmetric_encryption/reader.rb', line 162 def version @version end |
Class Method Details
.decrypt(source:, target:, block_size: 65_535, **args) ⇒ Object
Decrypt an entire file.
Returns [Integer] the number of unencrypted bytes written to the target file.
Params:
source: [String|IO]
Source file_name or IOStream
target: [String|IO]
Target file_name or IOStream
block_size: [Integer]
Number of bytes to read into memory for each read.
For very large files using a larger block size is faster.
Default: 65535
Notes:
-
The file contents are streamed so that the entire file is not loaded into memory.
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/symmetric_encryption/reader.rb', line 100 def self.decrypt(source:, target:, block_size: 65_535, **args) target_ios = target.is_a?(String) ? ::File.open(target, 'wb') : target bytes_written = 0 self.open(source, **args) do |input_ios| bytes_written += target_ios.write(input_ios.read(block_size)) until input_ios.eof? end bytes_written ensure target_ios.close if target_ios&.respond_to?(:closed?) && !target_ios.closed? end |
.empty?(file_name_or_stream) ⇒ Boolean
Returns [true|false] whether the file or stream contains any data excluding the header should it have one
113 114 115 |
# File 'lib/symmetric_encryption/reader.rb', line 113 def self.empty?(file_name_or_stream) open(file_name_or_stream, &:eof?) end |
.header_present?(file_name) ⇒ Boolean
Returns [true|false] whether the file contains the encryption header
118 119 120 |
# File 'lib/symmetric_encryption/reader.rb', line 118 def self.header_present?(file_name) ::File.open(file_name, 'rb') { |file| new(file).header_present? } end |
.open(file_name_or_stream, buffer_size: 16_384, **args, &block) ⇒ Object
Open a file for reading, or use the supplied IO Stream
Parameters:
file_name_or_stream:
The file_name to open if a string, otherwise the stream to use
The file or stream will be closed on completion, use .initialize to
avoid having the stream closed automatically
buffer_size:
Amount of data to read at a time.
Minimum Value 128
Default: 16384
Note: Decryption occurs before decompression
# Example: Read and decrypt a line at a time from a file SymmetricEncryption::Reader.open(‘test_file’) do |file|
file.each_line {|line| p line }
end
# Example: Read and decrypt entire file in memory # Not recommended for large files SymmetricEncryption::Reader.open(‘test_file’) {|f| f.read }
# Example: Reading a limited number of bytes at a time from the file SymmetricEncryption::Reader.open(‘test_file’) do |file|
file.read(1)
file.read(5)
file.read
end
# Example: Read and decrypt 5 bytes at a time until the end of file is reached SymmetricEncryption::Reader.open(‘test_file’) do |file|
while !file.eof? do
file.read(5)
end
end
# Example: Read, Unencrypt and decompress data in a file SymmetricEncryption::Reader.open(‘encrypted_compressed.zip’, compress: true) do |file|
file.each_line {|line| p line }
end
# Example: Reading from a CSV file
require ‘csv’ begin
csv = CSV.new(SymmetricEncryption::Reader.open('csv_encrypted'))
csv.each {|row| p row}
ensure
csv.close if csv
end
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/symmetric_encryption/reader.rb', line 62 def self.open(file_name_or_stream, buffer_size: 16_384, **args, &block) ios = file_name_or_stream.is_a?(String) ? ::File.open(file_name_or_stream, 'rb') : file_name_or_stream begin file = new(ios, buffer_size: buffer_size, **args) file = Zlib::GzipReader.new(file) if !file.eof? && file.compressed? block ? block.call(file) : file ensure file.close if block && file && (file.respond_to?(:closed?) && !file.closed?) end end |
.read(file_name_or_stream, **args) ⇒ Object
Read the entire contents of a file or stream into memory.
Notes:
-
Do not use this method for reading large files.
78 79 80 |
# File 'lib/symmetric_encryption/reader.rb', line 78 def self.read(file_name_or_stream, **args) self.open(file_name_or_stream, **args, &:read) end |
Instance Method Details
#close(close_child_stream = true) ⇒ Object
Close the IO Stream
Note: Also closes the passed in io stream or file
It is recommended to call Symmetric::EncryptedStream.open or Symmetric::EncryptedStream.io rather than creating an instance of Symmetric::EncryptedStream directly to ensure that the encrypted stream is closed before the stream itself is closed
171 172 173 174 175 |
# File 'lib/symmetric_encryption/reader.rb', line 171 def close(close_child_stream = true) return if closed? @ios.close if close_child_stream @closed = true end |
#compressed? ⇒ Boolean
Returns whether the stream being read is compressed
Should be called before any reads are performed to determine if the file or stream is compressed.
Returns true when the header is present in the stream and it is compressed Returns false when the header is present in the stream and it is not compressed Returns nil when the header is not present in the stream
Note: The file will not be decompressed automatically when compressed.
To decompress the data automatically call SymmetricEncryption.open
152 153 154 |
# File 'lib/symmetric_encryption/reader.rb', line 152 def compressed? @compressed end |
#each_line(sep_string = "\n") ⇒ Object Also known as: each
ios.each(sep_string=“n”) {|line| block } => ios ios.each_line(sep_string=“n”) {|line| block } => ios Executes the block for every line in ios, where lines are separated by sep_string. ios must be opened for reading or an IOError will be raised.
258 259 260 261 |
# File 'lib/symmetric_encryption/reader.rb', line 258 def each_line(sep_string = "\n") yield gets(sep_string) until eof? self end |
#eof? ⇒ Boolean
Returns whether the end of file has been reached for this stream
266 267 268 |
# File 'lib/symmetric_encryption/reader.rb', line 266 def eof? @read_buffer.empty? && @ios.eof? end |
#flush ⇒ Object
Flush the read stream
Needed by XLS gem
179 180 181 |
# File 'lib/symmetric_encryption/reader.rb', line 179 def flush @ios.flush end |
#gets(sep_string, length = nil) ⇒ Object
Reads a single decrypted line from the file up to and including the optional sep_string. A sep_string of nil reads the entire contents of the file Returns nil on eof The stream must be opened for reading or an IOError will be raised.
239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/symmetric_encryption/reader.rb', line 239 def gets(sep_string, length = nil) return read(length) if sep_string.nil? # Read more data until we get the sep_string while (index = @read_buffer.index(sep_string)).nil? && !@ios.eof? break if length && @read_buffer.length >= length read_block end index ||= -1 data = @read_buffer.slice!(0..index) @pos += data.length return nil if data.empty? && eof? data end |
#header_present? ⇒ Boolean
After opening a file Returns [true|false] whether the file being read has an encryption header
124 125 126 |
# File 'lib/symmetric_encryption/reader.rb', line 124 def header_present? @header_present end |
#read(length = nil) ⇒ Object
Read from the stream and return the decrypted data See IOS#read
Reads at most length bytes from the I/O stream, or to the end of file if length is omitted or is nil. length must be a non-negative integer or nil.
At end of file, it returns nil if no more data is available, or the last remaining bytes
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/symmetric_encryption/reader.rb', line 197 def read(length = nil) data = nil if length return '' if length.zero? return nil if eof? # Read length bytes read_block while (@read_buffer.length < length) && !@ios.eof? if @read_buffer.empty? data = nil elsif @read_buffer.length > length data = @read_buffer.slice!(0..length - 1) else data = @read_buffer @read_buffer = '' end else # Capture anything already in the buffer data = @read_buffer @read_buffer = '' unless @ios.eof? # Read entire file buf = @ios.read || '' data << @stream_cipher.update(buf) if buf && !buf.empty? data << @stream_cipher.final end end @pos += data.length data end |
#readline(sep_string = "\n") ⇒ Object
Reads a single decrypted line from the file up to and including the optional sep_string. Raises EOFError on eof The stream must be opened for reading or an IOError will be raised.
231 232 233 |
# File 'lib/symmetric_encryption/reader.rb', line 231 def readline(sep_string = "\n") gets(sep_string) || raise(EOFError, 'End of file reached when trying to read a line') end |
#rewind ⇒ Object
Rewind back to the beginning of the file
274 275 276 277 278 |
# File 'lib/symmetric_encryption/reader.rb', line 274 def rewind @read_buffer = '' @ios.rewind read_header end |
#seek(amount, whence = IO::SEEK_SET) ⇒ Object
Seeks to a given offset (Integer) in the stream according to the value of whence:
IO::SEEK_CUR | Seeks to _amount_ plus current position
--------------+----------------------------------------------------
IO::SEEK_END | Seeks to _amount_ plus end of stream (you probably
| want a negative value for _amount_)
--------------+----------------------------------------------------
IO::SEEK_SET | Seeks to the absolute location given by _amount_
WARNING: IO::SEEK_SET will jump to the beginning of the file and
then re-read upto the point specified
WARNING: IO::SEEK_END will read the entire file and then again
upto the point specified
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/symmetric_encryption/reader.rb', line 292 def seek(amount, whence = IO::SEEK_SET) offset = 0 case whence when IO::SEEK_SET offset = amount rewind when IO::SEEK_CUR if amount >= 0 offset = amount else offset = @pos + amount rewind end when IO::SEEK_END rewind # Read and decrypt entire file a block at a time to get its total # unencrypted size size = 0 until eof read_block size += @read_buffer.size @read_buffer = '' end rewind offset = size + amount else raise(ArgumentError, "unknown whence:#{whence} supplied to seek()") end read(offset) if offset.positive? 0 end |
#size ⇒ Object
Return the size of the file rounded up to the nearest encryption block size
Needed by XLS gem
185 186 187 |
# File 'lib/symmetric_encryption/reader.rb', line 185 def size @ios.size end |