I'm trying to make somebody Alice sign a message with a [privateKey], then letting somebody Bob verify that message with a [publicKey] (Bob doesn't know Alice`s private key).
class digitalSign:
def __init__(self):
self.privateKey = SigningKey.generate(curve = NIST384p)
self.publicKey = self.privateKey.verifying_key
def sign(self, privateKey, msg):
signature = self.privateKey.sign(msg.encode())
return signature
test = digitalSign()
rawPublicKey = test.publicKey.to_string() #suppose this is from database
publicKey = SigningKey.from_string(rawPublicKey, curve = NIST384p)
Getting this error:
Traceback (most recent call last):
File "bc.py", line 28, in <module>
publicKey = SigningKey.from_string(rawPublicKey, curve = NIST384p)
File "C:\...\site-packages\ecdsa-0.16.1-py3.7.egg\ecdsa\keys.py", line 885, in from_string
ecdsa.keys.MalformedPointError: Invalid length of private key, received 96, expected 48
Related
I am trying to pull a huge amount of data (in millions) and I am getting the following error when running my code. If I run the same code with a small range (to be exact a range of 2) it runs successfully. Please assist in helping me know if this is my issue or is coming from the API side
Thanks
The Error I am getting
DEBUG:google.api_core.bidi:Started helper thread Thread-ConsumeBidirectionalStream
DEBUG:google.api_core.bidi:Thread-ConsumeBidirectionalStream caught error 400 Request contains an invalid argument. and will exit. Generally this is due to the RPC itself being cancelled and the error will be surfaced to the calling code.
Traceback (most recent call last):
File "/home/coyugi/teltel_env/lib/python3.8/site-packages/google/api_core/grpc_helpers.py", line 147, in error_remapped_callable
return _StreamingResponseIterator(
File "/home/coyugi/teltel_env/lib/python3.8/site-packages/google/api_core/grpc_helpers.py", line 73, in __init__
self._stored_first_result = next(self._wrapped)
File "/home/coyugi/teltel_env/lib/python3.8/site-packages/grpc/_channel.py", line 426, in __next__
return self._next()
File "/home/coyugi/teltel_env/lib/python3.8/site-packages/grpc/_channel.py", line 826, in _next
raise self
grpc._channel._MultiThreadedRendezvous: <_MultiThreadedRendezvous of RPC that terminated with:
status = StatusCode.INVALID_ARGUMENT
details = "Request contains an invalid argument."
debug_error_string = "{"created":"#1652904360.179503883","description":"Error received from peer ipv4:173.194.76.95:443","file":"src/core/lib/surface/call.cc","file_line":952,"grpc_message":"Request contains an invalid argument.","grpc_status":3}"
>
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/coyugi/teltel_env/lib/python3.8/site-packages/google/api_core/bidi.py", line 636, in _thread_main
self._bidi_rpc.open()
File "/home/coyugi/teltel_env/lib/python3.8/site-packages/google/api_core/bidi.py", line 279, in open
call = self._start_rpc(iter(request_generator), metadata=self._rpc_metadata)
File "/home/coyugi/teltel_env/lib/python3.8/site-packages/google/cloud/bigquery_storage_v1/services/big_query_write/client.py", line 678, in append_rows
response = rpc(
File "/home/coyugi/teltel_env/lib/python3.8/site-packages/google/api_core/gapic_v1/method.py", line 154, in __call__
return wrapped_func(*args, **kwargs)
File "/home/coyugi/teltel_env/lib/python3.8/site-packages/google/api_core/retry.py", line 283, in retry_wrapped_func
return retry_target(
File "/home/coyugi/teltel_env/lib/python3.8/site-packages/google/api_core/retry.py", line 190, in retry_target
return target()
File "/home/coyugi/teltel_env/lib/python3.8/site-packages/google/api_core/grpc_helpers.py", line 151, in error_remapped_callable
raise exceptions.from_grpc_error(exc) from exc
google.api_core.exceptions.InvalidArgument: 400 Request contains an invalid argument.
INFO:google.api_core.bidi:Thread-ConsumeBidirectionalStream exiting
DEBUG:google.cloud.bigquery_storage_v1.writer:Finished stopping manager.
Traceback (most recent call last):
File "write_data_to_db2.py", line 207, in <module>
p.append_rows_pending(project_id='dwingestion', dataset_id='ke',
File "write_data_to_db2.py", line 188, in append_rows_pending
response_future_1 = append_rows_stream.send(request)
File "/home/coyugi/teltel_env/lib/python3.8/site-packages/google/cloud/bigquery_storage_v1/writer.py", line 234, in send
return self._open(request)
File "/home/coyugi/teltel_env/lib/python3.8/site-packages/google/cloud/bigquery_storage_v1/writer.py", line 207, in _open
raise request_exception
google.api_core.exceptions.Unknown: None There was a problem opening the stream. Try turning on DEBUG level logs to see the error.
Summary Of My Code
# PULLING DATA FROM THE API
def whole_teltel_raw_data():
# Creating a session to introduce network consistency
session = requests.Session()
retry = Retry(connect=3, backoff_factor=1.0)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
url = "https://my_api_url"
the_headers = {"X-API-KEY": 'my key'}
offset_limit = 1249500
teltel_data = []
# Loop through the results and if present extend the teltel_data list
#======================================================================================================================
# WRITE THE DATA TO THE DATA WAREHOUSE
# ======================================================================================================================
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'dwingestion-b033d9535e9d.json'
def create_row_data(tuple_data):
call_id, starttime, stoptime, direction, type, status, duration_sec, rate, cost, transfer, extra_prefix, audio_url, \
hangup_element, caller_number, caller_type, caller_cid, caller_dnid, caller_user_id, caller_user_short, \
callee_number, calle_type, callee, hangup_element_name, hangup_element_element, callee_user_id, callee_user_short, \
caller = tuple_data
row = teltel_call_data_pb2.TeltelCall()
row.call_id = call_id
row.starttime = starttime
row.stoptime = stoptime
row.direction = direction
row.type = type
row.status = status
row.duration_sec = duration_sec
row.rate = rate
row.cost = cost
row.transfer = transfer
row.extra_prefix = extra_prefix
row.audio_url = audio_url
row.hangup_element = hangup_element
row.caller_number = caller_number
row.caller_type = caller_type
row.caller_cid = caller_cid
row.caller_dnid = caller_dnid
row.caller_user_id = caller_user_id
row.caller_user_short = caller_user_short
row.callee_number = callee_number
row.calle_type = calle_type
row.callee = callee
row.hangup_element_name = hangup_element_name
row.hangup_element_title = hangup_element_element
row.callee_user_id = callee_user_id
row.callee_user_short = callee_user_short
row.caller = caller
return row.SerializeToString()
# Creating connection to the data warehouse
def create_bigquery_storage_client(google_credentials):
return bigquery_storage_v1.client.BigQueryWriteClient(
credentials=google_credentials
)
class GcpBigqueryStorageService(object):
def __init__(self, google_credentials=None, gcp_config=None):
self.client = create_bigquery_storage_client(google_credentials)
self.config = gcp_config
def append_rows_pending(self, project_id: str, dataset_id: str, table_id: str):
"""Create a write stream, write some sample data, and commit the stream."""
# write_client = self.client
parent = self.client.table_path(project_id, dataset_id, table_id)
write_stream = types.WriteStream()
# When creating the stream, choose the type. Use the PENDING type to wait
write_stream.type_ = types.WriteStream.Type.PENDING
write_stream = self.client.create_write_stream(
parent=parent, write_stream=write_stream
)
stream_name = write_stream.name
# Create a template with fields needed for the first request.
request_template = types.AppendRowsRequest()
# The initial request must contain the stream name.
request_template.write_stream = stream_name
# So that BigQuery knows how to parse the serialized_rows, generate a
# protocol buffer representation of your message descriptor.
proto_schema = types.ProtoSchema()
proto_descriptor = descriptor_pb2.DescriptorProto()
teltel_call_data_pb2.TeltelCall.DESCRIPTOR.CopyToProto(proto_descriptor)
proto_schema.proto_descriptor = proto_descriptor
proto_data = types.AppendRowsRequest.ProtoData()
proto_data.writer_schema = proto_schema
request_template.proto_rows = proto_data
# Some stream types support an unbounded number of requests. Construct an
# AppendRowsStream to send an arbitrary number of requests to a stream.
append_rows_stream = writer.AppendRowsStream(self.client, request_template)
# Create a batch of row data by appending proto2 serialized bytes to the
# serialized_rows repeated field.
proto_rows = types.ProtoRows()
row_number = 0
for row in whole_teltel_raw_data():
proto_rows.serialized_rows.append(create_row_data(row))
# checking the writing progress
row_number = row_number + 1
print("Writing to the database row number", row_number)
# The first request must always have an offset of 0.
request = types.AppendRowsRequest()
proto_data = types.AppendRowsRequest.ProtoData()
proto_data.rows = proto_rows
request.proto_rows = proto_data
append_rows_stream.close()
# A PENDING type stream must be "finalized" before being committed. No new
# records can be written to the stream after this method has been called.
self.client.finalize_write_stream(name=write_stream.name)
# Commit the stream you created earlier.
batch_commit_write_streams_request = types.BatchCommitWriteStreamsRequest()
batch_commit_write_streams_request.parent = parent
batch_commit_write_streams_request.write_streams = [write_stream.name]
self.client.batch_commit_write_streams(batch_commit_write_streams_request)
print(f"Writes to stream: '{write_stream.name}' have been committed.")
p = GcpBigqueryStorageService()
p.append_rows_pending(project_id='my_project', dataset_id='my_id', table_id='teltel_call_2')
So, here's my code:
import base64
import os
from cryptography.fernet import Fernet
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
def decrypt_data(key, data):
key = bytes(key.encode('utf-8'))
SALT = b'\xbf\xd4\xa9\xcei\xfa\xa7Db\x83\xbf\xa5.\xbe/\xd9'
kdf = PBKDF2HMAC(
hashes.SHA256(),
length=32,
salt=SALT,
iterations=32000
)
fernet = Fernet(base64.urlsafe_b64encode(kdf.derive(key)))
return fernet.decrypt(data)
def encrypt_data(key, data):
key = bytes(key.encode('utf-8'))
SALT = b'\xbf\xd4\xa9\xcei\xfa\xa7Db\x83\xbf\xa5.\xbe/\xd9'
kdf = PBKDF2HMAC(
hashes.SHA256(),
length=32,
salt=SALT,
iterations=32000
)
key = base64.urlsafe_b64encode(kdf.derive(key))
fernet = Fernet(key)
return fernet.encrypt(data.encode())
Here's the full code (get_history fetches the encrypted data, and decrypts it with a key)
from cryptography.fernet import InvalidToken
from framework.local_storage_encryption import encrypt_data, decrypt_data
# get_history() ->
# -1: unknown exception
# 0: success
# 1: invalid key
def get_history(key):
try:
value = []
string_plain = decrypt_data(key, open('local_storage/HISTORY').readlines())
for url in string_plain.split("\n"):
value.append(url)
return value
except InvalidToken as exc:
print(f"[!!!] Unable to load search history as the decryption key provided is invalid.")
return [], 1
return [], -1
def save_history(key, value: list):
string_plain = ""
for url in value:
string_plain += f"{url}\n"
with open('local_storage/HISTORY', 'wb') as file:
file.write(encrypt_data(key, string_plain))
history = get_history("test") # this function itself raises the error
for some reason, encryption works just fine, but when it comes to decryption, it throws this error.
Traceback (most recent call last):
File "/home/trayambak/Projects/Browser/main.py", line 20, in <module>
main()
File "/home/trayambak/Projects/Browser/main.py", line 15, in main
window.init()
File "/home/trayambak/Projects/Browser/framework/__init__.py", line 41, in init
self.live_history = get_history(encryption_key)
File "/home/trayambak/Projects/Browser/framework/history_reader.py", line 12, in get_history
string_plain = decrypt_data(key, open('local_storage/HISTORY').readlines())
File "/home/trayambak/Projects/Browser/framework/local_storage_encryption.py", line 18, in decrypt_data
return fernet.decrypt(data)
File "/usr/lib/python3.10/site-packages/cryptography/fernet.py", line 76, in decrypt
timestamp, data = Fernet._get_unverified_token_data(token)
File "/usr/lib/python3.10/site-packages/cryptography/fernet.py", line 101, in _get_unverified_token_data
utils._check_bytes("token", token)
File "/usr/lib/python3.10/site-packages/cryptography/utils.py", line 33, in _check_bytes
raise TypeError("{} must be bytes".format(name))
TypeError: token must be bytes
(no, the key isn't randomly generated every execution, I use the key 'testing', this isn't a salt problem either)
As you can see, the key is very much encoded into bytes yet cryptography throws this error. Any help would be appreciated, thanks. The only sketch thing that can cause an error here in my opinion is PBKDF2HMAC, but it worked earlier, but now it does not. The key is a str converted to bytes, hence I am very confused.
I am new to Marshmallow (3.10.0) and I am lost and need help trying to figure out what causes the following error:
AssertionError: ["Input Error - exten: ['Missing data for required field.']"]
The traceback of the error is the following:
Traceback (most recent call last):
File "/root/wazo_virtualenv_python37/lib/python3.7/site-packages/nose/case.py", line 198, in runTest
self.test(*self.arg)
File "/root/wazo-confd/integration_tests/suite/helpers/wrappers.py", line 81, in decorated
result = func(*new_args, **kwargs)
File "/root/wazo-confd/integration_tests/suite/helpers/wrappers.py", line 81, in decorated
result = func(*new_args, **kwargs)
File "/root/wazo-confd/integration_tests/suite/base/test_call_filter_surrogate_user.py", line 216, in test_get_surrogates_callfilter_exten_when_disabled
confd.extensions.features(feature['id']).put({'enabled': False}).assert_updated()
File "/root/wazo-confd/integration_tests/suite/helpers/client.py", line 272, in assert_updated
self.assert_status(204)
File "/root/wazo-confd/integration_tests/suite/helpers/client.py", line 242, in assert_status
assert_that(self.response.status_code, is_in(statuses), self.response.text)
So it seems that the test function test_get_surrogates_callfilter_exten_when_disabled is failing:
def test_get_surrogates_callfilter_exten_when_disabled(call_filter, user):
response = confd.extensions.features.get(search="bsfilter")
feature = response.items[0]
---> (line 216 in traceback): confd.extensions.features(feature['id']).put({'enabled': False}).assert_updated()
with a.call_filter_surrogate_user(call_filter, user):
response = confd.callfilters(call_filter['id']).get()
assert_that(
response.item,
has_entries(
surrogates=has_entries(
users=contains(has_entries(exten=None, uuid=user['uuid']))
)
),
)
confd.extensions.features(feature['id']).put(
{'enabled': feature['enabled']}
).assert_updated()
the feature_extension schema is defined as the following:
class ExtensionFeatureSchema(BaseSchema):
id = fields.Integer(dump_only=True)
exten = fields.String(validate=Regexp(EXTEN_REGEX), required=True)
context = fields.String(dump_only=True)
feature = fields.String(attribute='typeval', dump_only=True)
enabled = fields.Boolean()
links = ListLink(Link('extensions_features'))
and the put function:
def put(self):
form = self.schema().load(request.get_json())
variables = [self.model(**option) for option in form]
self.service.edit(self.section_name, variables)
return '', 204
I have tried many solutions that I found online; but they did not fix the issue for me:
1 + pass partial=True to the load function:
form = self.schema().load(request.get_json(), partial=True)
2 + remove required=True from the field definition; this made the above error go away but failed many other tests that I have.
I am currently out of ideas, so if anyone has an idea how to fix the issue.
UPDATE: I know the token error only and I don't understand what is happening as I encrypt and decrypt with the same key?
Error at hand:
Exception in Tkinter callback #Don't understand this callback as this is to do with the cryptography module I am using
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 1883, in __call__
return self.func(*args)
File "/Users/James/Documents/Programming/PasswordManager/main.py", line 190, in see
decrypt = f.decrypt(item[1].encode('utf-8'))
File "/Users/James/Documents/Programming/venv/lib/python3.8/site-packages/cryptography/fernet.py", line 75, in decrypt
timestamp, data = Fernet._get_unverified_token_data(token)
File "/Users/James/Documents/Programming/venv/lib/python3.8/site-packages/cryptography/fernet.py", line 107, in _get_unverified_token_data
raise InvalidToken
cryptography.fernet.InvalidToken
b'gAAAAABgY3KXoeeegS8RPHdvXTH6_GQ_EPfgdqZqIwP4XL-hIyEk3BcxV3y0o_quNPFyHeTdkv7Pk9MmnEIL1XeXlEfuUQNJ_e_dIsAr2ZvLGpDT1Y6I6Qvzat5aAf7Z0624O4BeAFNf
Original Question
I am making a password manager, I started by using files and some suggested sqlite3 so I have started to use it so I can add the function of rewriting the passwords but I keep getting two errors that have really confused me,
The table sqlite table:
c.execute("""CREATE TABLE passwords (passwordTitle text,generatedPassword text)""")
I encrypt the password using this function, this works and inserts into database:
def submited():
# Call the password generator
setPassword()
# Get user input from the entry
usrinput = userInput.get()
# Set a splitter for writing to the file
# If passwordenc.txt exists:
if os.path.isfile('data.db'):
# Open passwordenc.txt and set it as password_file
# Encrypt the combo
genPasswordEnc = f.encrypt(generatePassword.encode('utf-8'))
c.execute("INSERT INTO passwords VALUES(?,?)", (usrinput, genPasswordEnc))
conn.commit()
# If passwordenc.txt does not exist:
and I decrypt it using this function:
def see():
menu.destroy()
seeWin = Tk()
seeWin.geometry('1250x500')
# Same size will be defined in variable for center screen in Tk_Width and Tk_height
Tk_Width = 1250
Tk_Height = 500
# calculate coordination of screen and window form
x_Left = int(seeWin.winfo_screenwidth() / 2 - Tk_Width / 2)
y_Top = int(seeWin.winfo_screenheight() / 2 - Tk_Height / 2)
# Write following format for center screen
seeWin.geometry("+{}+{}".format(x_Left, y_Top))
seeWin.title('See Password')
seeFrame = tk.Frame()
listbox = tk.Listbox(seeFrame, height='30', width='135')
listbox.pack(side=LEFT, fill=BOTH)
scrollbar = tk.Scrollbar(seeFrame, )
scrollbar.pack(side=RIGHT, fill=Y)
listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview)
if os.path.isfile('data.db'):
c.execute('SELECT * FROM passwords')
password_list = c.fetchall()
for item in password_list:
item = str(item)
itemSplit = item.split(',')
leng = len(itemSplit[1])-2
itemCut = itemSplit[1][:leng].encode('utf-8')
print(itemCut)
decrypt = f.decrypt(itemCut)
print(decrypt)
listbox.insert(END, str(decrypt))
print("Decrypted pass: ", decrypt)
But I keep getting this error:
Exception in Tkinter callback
Traceback (most recent call last):
File "/Users/James/Documents/Programming/venv/lib/python3.8/site-packages/cryptography/fernet.py", line 102, in _get_unverified_token_data
data = base64.urlsafe_b64decode(token)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/base64.py", line 133, in urlsafe_b64decode
return b64decode(s)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/base64.py", line 87, in b64decode
return binascii.a2b_base64(s)
binascii.Error: Invalid base64-encoded string: number of data characters (141) cannot be 1 more than a multiple of 4
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 1883, in __call__
return self.func(*args)
File "/Users/James/Documents/Programming/PasswordManager/main.py", line 190, in see
decrypt = f.decrypt(itemCut)
File "/Users/James/Documents/Programming/venv/lib/python3.8/site-packages/cryptography/fernet.py", line 75, in decrypt
timestamp, data = Fernet._get_unverified_token_data(token)
File "/Users/James/Documents/Programming/venv/lib/python3.8/site-packages/cryptography/fernet.py", line 104, in _get_unverified_token_data
raise InvalidToken
cryptography.fernet.InvalidToken
And I don't understand what is throwing this error as when using a .txt file
I keep getting this error, I'm not sure why though.
Traceback (most recent call last):
File "/home/cambria/Main.py", line 1, in <module>
from RiotAPI import RiotAPI
File "/home/cambria/RiotAPI.py", line 6
def __init__(self, api_key, region=Consts.REGIONS['north_america'])
^
SyntaxError: invalid syntax
I have not used Python for that long, I am just using it because it facilitates what I'm trying to do well, but I have used various other languages and as far as I can tell you would want to close these ()'s in this statement def __init__(self, api_key, region=Consts.REGIONS['north_america']) however I keep getting a SyntaxError: invalid syntax?
the rest of that definition is as follows, if it helps.
class RiotAPI(object):
def __init__(self, api_key, region=Consts.REGIONS['north_america'])
self.api_key = api_key
self.region = region
EDIT 1: if i add a : at the end of def __init__(self, api_key, region=Consts.REGIONS['north_america']): like so, why? and after doing this i get a new syntax error that i will address after some wisedom
EDIT 2: new syntax error after fixing the first is,
Traceback (most recent call last):
File "/home/cambria/Main.py", line 1, in <module>
from RiotAPI import RiotAPI
File "/home/cambria/RiotAPI.py", line 11
args = ('api_key': self.api_key)
^
SyntaxError: invalid syntax
which is
def _request(self, api_url, params=()):
args = ('api_key': self.api_key)
for key, value in params.items():
if key not in args:
args[key] = value
EDIT 3: This should be the last of it.. no more syntax, just a
Traceback (most recent call last):
File "/home/cambria/Main.py", line 10, in <module>
main()
File "/home/cambria/Main.py", line 5, in main
respons3 = api.get_summoner_by_name('hi im gosan')
File "/home/cambria/RiotAPI.py", line 31, in get_summoner_by_name
return self._request(api_url)
File "/home/cambria/RiotAPI.py", line 12, in _request
for key, value in params.items():
AttributeError: 'tuple' object has no attribute 'items'
in
def _request(self, api_url, params=()):
args = {'api_key': self.api_key}
for key, value in params.items():
if key not in args:
args[key] = value
response = requests.get(
Consts.URL['base'].format(
proxy=self.region,
region=self.region,
url=api_url
),
params=args
)
print response.url
return response.json()
this is the only error i have received that i really don't know much on. Is this a result of there being no .items on my params? or i left it initialized as an empty dictionary?
The problem is just that you're missing a : at the end of the line.
def __init__(self, api_key, region=Consts.REGIONS['north_america']):
self.api_key = api_key
self.region = region
You forgot a ::
class RiotAPI(object):
def __init__(self, api_key, region=Consts.REGIONS['north_america']): # <HERE
self.api_key = api_key
self.region = region