Class: DRM::Wrapper
- Inherits:
-
Object
- Object
- DRM::Wrapper
- Defined in:
- lib/drm/wrapper.rb
Instance Attribute Summary collapse
-
#filekey ⇒ Object
readonly
the key used to wrap the file.
-
#metadata ⇒ Object
readonly
the DRM metadata for the wrapped file.
Instance Method Summary collapse
-
#initialize(length, mime_type) ⇒ Wrapper
constructor
new wrapper for a contents file the file has
length
bytes and its MIME type ismime_type
. -
#wrap(read_proc, write_proc) ⇒ Object
wraps a content file with DRM protection read_proc(length, offset) reads length bytes at offset from the file to be wrapped write_proc(data) appends the given data to the file containing the wrapped content.
Constructor Details
#initialize(length, mime_type) ⇒ Wrapper
new wrapper for a contents file the file has length
bytes and its MIME type is mime_type
4 5 6 7 |
# File 'lib/drm/wrapper.rb', line 4 def initialize(length, mime_type) @filekey = DRM::Metadata.new_filekey @metadata = DRM::Metadata.new_for_file length, mime_type end |
Instance Attribute Details
#filekey ⇒ Object (readonly)
the key used to wrap the file
10 11 12 |
# File 'lib/drm/wrapper.rb', line 10 def filekey @filekey end |
#metadata ⇒ Object (readonly)
the DRM metadata for the wrapped file
12 13 14 |
# File 'lib/drm/wrapper.rb', line 12 def @metadata end |
Instance Method Details
#wrap(read_proc, write_proc) ⇒ Object
wraps a content file with DRM protection read_proc(length, offset) reads length bytes at offset from the file to be wrapped write_proc(data) appends the given data to the file containing the wrapped content
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/drm/wrapper.rb', line 17 def wrap(read_proc, write_proc) # push the metadata = @metadata.to_yaml_str = [.length].pack('N') write_proc.call write_proc.call # push the data block_size = @metadata.block_size length = @metadata.length in_offset = 0 0.upto(@metadata.blocks - 1) do |block_index| # read read_size = (block_size <= length - in_offset) ? block_size : length - in_offset block_data = read_proc.call read_size, in_offset in_offset += read_size # encrypt subkey = @metadata.subkey_from_filekey(.subkey_index(block_index), @filekey) crypted_data = @metadata.encrypt_block block_data, block_index, subkey # write write_proc.call crypted_data end end |