Core¶
Classes and functions providing the core functionality of Dynabyte
Built-in Operations¶
The functions can be used directly, or as methods from Array and File objects (with the exception of RotateLeft and RotateRight).
- dynabyte.operations.XOR(data, value=0)¶
XOR ‘data’ against ‘value’, ‘count’ times
If value is anything other than int, data will be XOR’d against the value sequentially (like a key).
- Parameters:
data – Data to perform operation on (str, list, bytes, bytearray, int)
value – Value to XOR array against (str, list, bytes, bytearray, int)
- Return type:
bytes
- dynabyte.operations.SUB(data, value=0)¶
Subtract ‘value’ from each byte in ‘data’, ‘count’ times
- Parameters:
data – Data to perform operation on (str, list, bytes, bytearray, int)
value (int) – Value to subtract
- Return type:
bytes
- dynabyte.operations.ADD(data, value=0)¶
Add ‘value’ to each byte in ‘data’, ‘count’ times
- Parameters:
data – Data to perform operation on (str, list, bytes, bytearray, int)
value (int) – Value to add
- Return type:
bytes
- dynabyte.operations.ROL(data, value=0)¶
Circular rotate shift ‘data’ left by ‘value’ bits
- Parameters:
data – Data to perform operation on (str, list, bytes, bytearray, int)
value (int) – Number of bits to rotate
- Return type:
bytes
- dynabyte.operations.ROR(data, value=0)¶
Circular rotate shift ‘data’ right by ‘value’ bits
- Parameters:
data – Data to perform operation on (str, list, bytes, bytearray, int)
value (int) – Number of bits to rotate
- Return type:
bytes
- dynabyte.operations.RC4(data, key)¶
Encrypt/decrypt data with key using RC4
- Parameters:
data – Data to encrypt (str, list, bytes, bytearray, int)
key – Key to encrypt data with (str, list, bytes, bytearray, int)
- Return type:
bytes
- dynabyte.operations.AESEncrypt(data, key, mode=9)¶
Encrypt data using AES
- Parameters:
data – Data to encrypt (str, list, bytes, bytearray, int)
key – Key to encrypt data with (str, list, bytes, bytearray, int) ! Must be 16 bytes !
- Return type:
bytes
- dynabyte.operations.AESDecrypt(data, key, *, nonce=None, tag=None, mode=9)¶
Encrypt data using AES
- Parameters:
data – Data to encrypt (str, list, bytes, bytearray, int)
key – Key to encrypt data with (str, list, bytes, bytearray, int) ! Must be 16 bytes !
- Return type:
bytes
- dynabyte.operations.reverse(data)¶
Reverse the order of data
- Parameters:
data – Data to reverse (str, list, bytes, bytearray, int)
- Return type:
bytes
- dynabyte.operations.pad(data, padding, target_size=-1, *, front=False, even=False)¶
Add padding bytes to given data
If padding both sides, the padding on the end will recieve more if the pad size is not even.
- Parameters:
data – Data to perform operation on (str, list, bytes, bytearray, int)
padding – Data to use for padding (str, list, bytes, bytearray, int)
target_size (int) – Target size of returned, padded data. If -1, given padding is simply added and returned
front (bool) – Place padding at the beginning of the data, instead of the end
even (bool) – Divide padding between the beginning and end of data, as evenly as possible
- Return type:
bytes
- dynabyte.operations.RotateLeft(x, n)¶
Circular rotate shift byte ‘x’ left by ‘n’ bits
Performs ROL on single byte. Included for convenience, not inherited by anything.
- Parameters:
x (int) – Byte to rotate
n (int) – Number of bits to shift by
- Return type:
int
- dynabyte.operations.RotateRight(x, n)¶
Circular rotate shift byte ‘x’ right by ‘n’ bits
Performs ROR on single byte. Included for convenience, not inherited by anything.
- Parameters:
x (int) – Byte to rotate
n (int) – Number of bits to shift by
- Return type:
int
Classes¶
DynabyteBase¶
Base class for dynabyte.core.Array and dynabyte.core.File, providing methods for using the aforementioned built-in operations. The XOR, ADD, SUB, ROL, and ROR methods can also be called with their respective binary operators (^, +, -, <<, >>).
- dynabyte.core.DynabyteBase.XOR(self, value=0)¶
XOR each byte of the current instance against ‘value’
If value is anything other than int, data will be XOR’d against the value sequentially (like a key).
- Parameters:
value – Value to XOR array against (int, str, list, bytes, or bytearray)
- dynabyte.core.DynabyteBase.SUB(self, value=0)¶
Subtract ‘value’ from each byte of the current instance
- Parameters:
value (int) – Value to subtract from each byte of array
- dynabyte.core.DynabyteBase.ADD(self, value=0)¶
“Add ‘value’ to each byte of the current instance
- Parameters:
value (int) – Value to add to each byte of array
- dynabyte.core.DynabyteBase.ROL(self, value=0)¶
Circular rotate shift left each byte of the current instance by ‘value’ bits
- Parameters:
value (int) – Number of places to shift array
- dynabyte.core.DynabyteBase.ROR(self, value=0)¶
Circular rotate shift right each byte of the current instance by ‘value’ bits
- Parameters:
value (int) – Number of places to shift array
- dynabyte.core.DynabyteBase.RC4(self, key)¶
Encrypt/decrypt data with key using RC4
- Parameters:
key – Key to encrypt data with (str, list, bytes, bytearray, int)
count (int) – Number of times to run RC4
- dynabyte.core.DynabyteBase.AESEncrypt(self, key)¶
Encrypt/decrypt data using AES
- Parameters:
key – Key to encrypt data with (str, list, bytes, bytearray, int) ! Must be 16 bytes !
- dynabyte.core.DynabyteBase.AESDecrypt(self, key, *, nonce=None, tag=None)¶
Encrypt/decrypt data using AES
- Parameters:
key – Key to encrypt data with (str, list, bytes, bytearray, int) ! Must be 16 bytes !
- dynabyte.core.DynabyteBase.reverse(self)¶
Reverse the order of data
If using on a file or particularly large string, be aware of the buffersize
- dynabyte.core.DynabyteBase.b64encode(self)¶
Encode data using base64
- dynabyte.core.DynabyteBase.b64decode(self)¶
Decode data using base64
- dynabyte.core.DynabyteBase.pad(self, padding, target_size=-1, *, front=False, even=False)¶
Add padding bytes to given data
If padding both sides, the padding on the end will recieve more if the pad size is not even. Be aware of the buffersize when using with File objects.
- Parameters:
padding – Data to use for padding (str, list, bytes, bytearray, int)
target_size (int) – Target size of returned, padded data. If -1, given padding is simply added and returned
front (bool) – Place padding at the beginning of the data, instead of the end
even (bool) – Divide padding between the beginning and end of data, as evenly as possible
- Return type:
bytes
- dynabyte.core.DynabyteBase.strip(self, *chars)¶
Remove leading and trailing characters from data
Wrapper around the builtin strip method
- Chars:
Characters to remove from data (str, list, bytes, bytearray, int)
Array¶
Array objects are iterable, and accept string, integer, byte, bytearray, list and other dynabyte.core.Array objects as input. List-type objects can contain a combination of any other valid input type. See Basic Usage section for details on printing/formating.
- dynabyte.core.Array(data, *, encoding='utf-8')¶
Dynabyte class for interacting with arrays
For use with string/list/byte/bytearray objects
- dynabyte.core.Array.fromfile(path, buffersize=-1, *, start=0, encoding='utf-8')¶
Return Array instance, initialized with the data retrieved from the file at the given path
Beware large files.
- Param:
Path of file to read from
- Parameters:
buffersize (int) – Number of bytes to read from file
start (int) – File offset to start reading from
encoding (str) – Encoding scheme for returned Array instance
- Returns Array:
Array object
- Return type:
dynabyte.core.Array
- dynabyte.core.Array.writefile(self, path, mode='wb')¶
Write instance data to given file
By default, creates/overwrites file. Returns the path of written file upon success, None on failure.
- Parameters:
path (str) – Path of file to write to
mode (str) – Mode to open file with
- Returns path:
Path of written file
- Return type:
str
- dynabyte.core.Array.run(self, callback, *, cb_type='full', output=None, count=1)¶
Execute operations defined in a callback function upon data.
Callback type ‘full’ gives the callback function full control over the data. Callback type ‘offset’ passes the data 1 byte at time to the callback function, along with the global offset.
- Parameters:
callback – Callback function: offset_func(byte, offset) -> byte OR full_func(data) -> bytes
cb_type (str) – ‘full’ (recieves all data) or ‘offset’ (recieves byte and its offset)
output (str) – Output file path (optional)
count (int) – Number of times to run array though callback function
- dynabyte.core.Array.gethash(self, hash='sha256')¶
Return hash of current instance data
- Parameters:
hash (str) – Hash type (Default: sha256)
- Return type:
str
- dynabyte.core.Array.insert(self, pos, value)¶
Inserts value into data at specified index
- Parameters:
pos (int) – Index position
value – int, unicode character, or bytes object (big endian)
- dynabyte.core.Array.append(self, value)¶
Adds value to the end of data
- Parameters:
value – int, unicode character, or bytes object (big endian)
- dynabyte.core.Array.extend(self, iterable)¶
Add all elements of iterable value to end of data
- Parameters:
value – List, tuple, string, dynabyte.core.Array objects
File¶
- dynabyte.core.File(path, buffersize=8192, *, start=0)¶
Dynabyte class for interacting with files
- dynabyte.core.File.run(self, callback, *, cb_type='full', output=None, count=1)¶
Execute operations defined in a callback function upon data within given file.
Callback type ‘full’ gives the callback function full control over the data. Callback type ‘offset’ passes the data 1 byte at time to the callback function, along with the global offset. Returns self, or instance created from output file.
- Parameters:
callback – Callback function: func(byte, offset) -> byte
output (str) – Output file path (optional)
count (int) – Number of times to run file though callback function
- dynabyte.core.File.getbytes(self, buffer=-1, encoding='utf-8')¶
Retrieve all bytes from file, return in a dynabyte Array
Beware hella large files
- Parameters:
buffer (int) – Number of bytes to read from file (Default: all)
- Returns Array:
Array object initialized with file bytes
- Return type:
dynabyte.core.Array
- dynabyte.core.File.getsize(self)¶
Return size of current instance file in bytes
- Return type:
int
- dynabyte.core.File.gethash(self, hash='sha256')¶
Return hash of current instance file
- Parameters:
hash (str) – Hash type (Default: sha256)
- Return type:
str
- dynabyte.core.File.delete(self)¶
Delete input file
Utility Functions¶
Dynabyte’s helper/utility functions
- dynabyte.utils.getbytearray(data, *, encoding='utf-8')¶
Convert string, list, bytes, or int objects to bytearray
List-type input data can be a combination of any valid input type (int, str, list, byte, bytearray). Strings will be encoded using the given codec.
- Parameters:
data – string, list, bytes, or int objects
encoding (str) – Codec to use for encoding if string given
- Return type:
bytearray
- dynabyte.utils.bprint(data, style=None, *, encoding='utf-8', delim=', ')¶
Print data as Python list, C-style array, string, or delimited hex
Default: Comma-deliminated hex representation
- Parameters:
data – string, list, bytes, bytearray, or int objects
style (str) – C, list, str, or None (hex bytes) array format
encoding (str) – Codec to decode string with
delim (str) – Delimiter between hex values
- Return type:
None
- dynabyte.utils.random_key(length=10, lower=33, upper=126, string=True, encoding='utf-8')¶
Return random assortment of characters in given length.
By default, the key will be generated from readable ascii characters.
- Parameters:
length (int) – Length, in characters, of key to return
lower (int) – Lower limit of values to generate (Default: 33/’!’)
lower – Upper limit of values to generate (Default: 126/’~’)
string (bool) – Return string representation if True, otherwise return bytes object
encoding (str) – Encoding scheme for returned string
- Return type:
str
- dynabyte.utils.comparefilebytes(path1, path2, verbose=True)¶
Compare the bytes of the two given files.
- Parameters:
path1 (str) – Path to file
path2 (str) – Path to second file
verbose (bool) – Print filesize message
- Return type:
bool