-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperators.py
More file actions
267 lines (217 loc) · 7.91 KB
/
Copy pathOperators.py
File metadata and controls
267 lines (217 loc) · 7.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import os
import struct
def stringtobin(message):
"""
:param message: The string message to be turned into binary
:return: returns the message converted to a string of bits
"""
# creates a variable called bits total where the bits will be stored
bitstotal = ""
# this loop runs the for the length of the message that needs to be converted
for i in range(len(message)):
# when using the bin function, a '0b' is added to the start of it,the [2:0] removes it
char = bin(ord(message[i]))[2:]
# adds enough leading zeros to create a byte
while len(char) < 8:
char = "0" + char
# adds the byte value of the character to the variable bitstotal
bitstotal += char
return (bitstotal)
def bintostring(binarymessage):
"""
:param binarymessage: Converts a sequence of bits into a string. Binarymessage
must be a string of multiple 8
:return: returns a string.
"""
# total is where the string will be stored
total = ""
# for each byte, it is converted to base 10 and then converted to an ascii value
for i in range(len(binarymessage) // 8):
byte = binarymessage[8 * i: 8 * (i + 1)]
byte = int(byte, 2)
char = chr(byte)
# after its converted into the character, it is stored in total
total += char
return total
def inttobin(integertoconvert):
"""
:param integertoconvert: the base-10 integer to convert to binary
:return: returns the binary equivalent of the integer
"""
# converts the number into binary and removes the '0b'
binarynumber = bin(integertoconvert)[2:]
# if the length of the integer is not a multiple of 8 (a byte), it adds leading 0's
while len(binarynumber) % 8 != 0:
binarynumber = "0" + binarynumber
return binarynumber
def bintoint(string):
"""
:param string: string is a string of bits that needs to be converted to an integer
:return: returns a base 10 integer
"""
# takes the input in bits, and converts it to base 2
decimal = int(string, 2)
return decimal
def hextobin(hexval):
"""
:param hexval: The hex value to be converted to an integer
:return: returns a base 10 integer
"""
# takes the hexvalue converts it into base 10
decimal = int(hexval, 16)
# ueses intobin function to convert to binary
result = inttobin(decimal)
return result
def bintohex(binval):
"""
:param binval: The string of bits to be converted
:return: returns the hex value
"""
# takes the binary number and converts it into base 10
decimal = int(binval, 2)
# takes converts the base 10 number to a hex
result = hex(decimal)[2:] # The 2: is because all hex numbers in python start with 0x which we don't want
# if the hex value is less than 8, it adds leading zeros
while len(result) < 8:
result = "0" + result
return result
# ---------Bitwise Operators---------
def bitxor(*bits):
"""
:param bits: All the bytes to be XOR'd
:return: The XOR'd value
"""
# Convert to base 10
allnums = []
for icounter in range(len(bits)):
allnums.append(bintoint(bits[icounter]))
# XOR The numbers
result = allnums[0]
for icounter in range(1, len(allnums)):
result = result ^ allnums[icounter]
# Convert back to binary
result = inttobin(result)
return result
def bitadd(*bits):
"""
:param bits: All the bytes to be added
:return: The total value
"""
# Convert to base 10
allnums = []
for icounter in range(len(bits)):
allnums.append(bintoint(bits[icounter]))
# Add The numbers
result = allnums[0]
for icounter in range(1, len(allnums)):
result = result + allnums[icounter]
#Convert to base 2
result = inttobin(result)
return result
def bitnot(bitinput):
"""
:param bitinput: the string of bits to be "notted".
:return: Returns a sequence of bits, the complement of bitinput
"""
result = ""
# checks each bit and gets the compliment of it
for i in range(len(bitinput)):
# if the bitinput is a 0, the result will be a 1
if bitinput[i] == "1":
result += "0"
else:
# if the bitinput is a 1, the result will be 0
result += "1"
return result
def bitand(*bits):
"""
:param bits: All the bytes to be "anded"
:return: The resulting value
"""
# Convert to base 10
allnums = []
for icounter in range(len(bits)):
allnums.append(bintoint(bits[icounter]))
# And The numbers using the built in function
result = allnums[0]
for icounter in range(1, len(allnums)):
result = result & allnums[icounter]
# Convert to base 2
result = inttobin(result)
return result
# -----Bit Manipulation Functions---------
def rightrotate(strinput, amount):
"""
:param strinput: The string of bits to be right rotated
:param amount: The amount of times to right rotate strinput.
:return: returns strinput rotated right 'amount' number of times
"""
# Get the full string
stringfull = strinput
string = strinput
for i in range(amount):
# Set the first value to the last character
string = stringfull[len(stringfull) - 1]
# The for loop adds in the other characters to the right
for g in range(0, len(strinput) - 1):
string += stringfull[g]
stringfull = string
return string
def leftrotate(strinput, amount):
"""
:param strinput: is the string input of bits
:param amount: the amount of times to rotate by
:return: returns strinput shifted left 'amount' number of times
"""
stringfull = strinput
string = strinput
for i in range(amount):
# Set the last character of string to the first value of stringfull
string = stringfull[0]
for g in range(1, len(strinput)):
# Add in the other characters to the left
string = stringfull[len(strinput) - g] + string
stringfull = string
return string
def rightshift(strinput, amount):
"""
:param strinput: This is the string input in bits
:param amount: Amount to shift by
:return: Returns strinput shifted right 'amount' number of times
"""
# Convert to base 10
bit = bintoint(strinput)
# Perform the Operation using the built in function
result = bit >> amount
# Convert to base 2
result = inttobin(result)
return result
#========Miscallenous Functions dealing with bytes=========
def getbytearray(file):
"""
This method takes a file and converts it to an array of bytes
:param file: The file to be converted
:return: An array of bytes
"""
# Open the fila as binary
with open(file, "rb") as image:
# Read the file and use the built-in bytearray function
contents = image.read()
raw_byte_array = bytearray(contents)
# Convert the integers into bytes and return the new byte array
byte_array = []
for i in range(len(raw_byte_array)):
byte_array.append(inttobin(raw_byte_array[i]))
return byte_array
def write_to_desktop(byte_array, name):
"""
This function writes a byte array to a file on the desktop.
:param byte_array: The byte array input to be written to a file on the desktop
:param name: The name of the new file. Must include file extension
:return: Nothing is returned, but a file is written to the dektop
"""
with open(os.path.join(os.path.expanduser('~'), "Desktop", name), "wb") as hack:
for byte in byte_array:
newbyte = int(byte, 2)
newbyte = struct.pack("B", newbyte)
hack.write(newbyte)