how to convert ed25519 keys in pgsql database and retrieve them back - python

I want to store the Public and Private keys in pgsql database and then when required want to retrieve them back.
I tried converting it ,storing and retrieving it from database but this results in error:
TypeError: Odd-length string
python testenc.py "TEST"
"5b69ecf0fb808693e6fdf6514fd8f1eca20b96e407ec78681269b24463d2cc0"
"3d4b091ff086660c8adfd3318d43563a2befb85619bb4422161a4e9ae0837131"
Input Message is : TEST
Traceback (most recent call last):
File "testenc.py", line 31, in <module>
skbob = pickle.loads(binascii.unhexlify(sys.argv[2]))
TypeError: Odd-length string

This can be done as follows:
1) use library pickle for this.
2) Use a socket communication and after receiving data on socket use pickle.load(received string) to maintain the key intact.
3) While sending data on socket "use pickle.dump(string)" so that data/Key remains intact.

Related

Tapo plug using python with PyP100 library login error

I am using the basic script from the repository to try and connect to a Tapo P110 plug. Clearly I am using my ip/email/pword.
from PyP100 import PyP110
p110 = PyP110.P110("192.168.X.X", "email#gmail.com", "Password123") #Creating a P110 plug object
p110.handshake() #Creates the cookies required for further methods
p110.login() #Sends credentials to the plug and creates AES Key and IV for further methods
#PyP110 has all PyP100 functions and additionally allows to query energy usage infos
p110.getEnergyUsage() #Returns dict with all the energy usage
But I am getting this error.
Traceback (most recent call last):
File "c:\Users\xxxx\xxxx\Add-hoc analysis projects\tapo_plug_energy_monitor\Python\Basic_Tapo_script.py", line 9, in <module>
p110.getDeviceInfo()
File "C:\Users\xxxx\AppData\Local\Programs\Python\Python39\lib\site-packages\PyP100\PyP100.py", line 236, in getDeviceInfo
URL = f"http://{self.ipAddress}/app?token={self.token}"
AttributeError: 'P110' object has no attribute 'token'
I should note I tried to ping the ip and all packets transmitted and received correctly. It is login() that causes the error but I have checked login details and they seem to be correct. Any help appreciated.
So the problem was because of an update to the Tapo P110 firmware version 1.0.16
The current solution is to make a small change to PyP100.py see below link.
https://github.com/fishbigger/TapoP100/issues/76

Exception: TypeError(string indices must be integers)

I have written the below python function (a snippet of the full code) to work in AWS Lambda. The purpose of it is to take a GeoJSON from an S3 bucket and parse it accordingly.
Once parsed, it is placed back into JSON format (data) and then should be inserted into the specified database using
bulk_item['uuid'] = str(uuid.uuid4())
bulk_item['name'] = feature_name
bulk_item['type'] = feature_type
bulk_item['info'] = obj
bulk_item['created'] = epoch_time
bulk_item['scope'] = 2
data = json.dumps(bulk_item)
print(data)
self.database.upsert_record(self.organisation, json_doc=data)
except Exception as e:
print(f'Exception: {e.__class__.__name__}({e})')
The db_access file in which the above is relating to is another python script. The function upsert_record is as below:
def upsert_record(self, organisation,
json_doc={}):
My code is working perfectly until I try to upsert it into the database. Once this line is gotten to, it throws the error
Traceback (most recent call last):
File "/var/task/s3_asset_handler.py", line 187, in process_incoming_file
self.database.upsert_record(self.organisation, json_doc=data)
File "/opt/python/database_access.py", line 1218, in upsert_record
new_uuid = json_doc['uuid']
TypeError: string indices must be integers
I can't seem to figure out the issue at all
You are trying to get an element from a JSON object, but passing a string.
The
data = json.dumps(bulk_item)
creates a string representing the object.
Try using bulk_item on it's own.

object supporting the buffer API required sha256 error

I want to hash some 4 digit numbers
but it gives me (object supporting the buffer API required) error
here's my code
import hashlib
import itertools as it
number=[0,1,2,3,4,5,6,7,8,9]
code = hashlib.sha256()
passwords = list(it.permutations(number, 4))
#hpass is hash password
for hpass in passwords :
code.update(passwords)
print(hpass)
and the output is
Traceback (most recent call last):
File "c:\Users\Parsa\Desktop\project\Untitled-2.py", line 11, in <module>
code.update(passwords)
TypeError: object supporting the buffer API required
the update function of hashlib.sha256 instance require the bytes-like object
Update the hash object with the bytes-like object.
https://docs.python.org/3/library/hashlib.html
and also it seems input passwords list at update
It is hard to know the intention of your code.
but I guess you wanna get a set of hashes by 4 digit numbers
if it is right try this.
import hashlib
import itertools as it
number=[0,1,2,3,4,5,6,7,8,9]
passwords = list(it.permutations(number, 4))
# hpass is hash password
for hpass in passwords :
encoded_hpass = ''.join(map(str, hpass)).encode('ascii')
code = hashlib.sha256()
code.update(encoded_hpass)
print(encoded_hpass)
print(code.digest())
Output
b'0123'
b'\x1b\xe2\xe4R\xb4mz\r\x96V\xbb\xb1\xf7h\xe8$\x8e\xba\x1bu\xba\xede\xf5\xd9\x9e\xaf\xa9H\x89\x9aj'
b'0124'
b'\x91\xb1\xe2#\xed\x10?)\x1c\x16v\\\xb2\x01\xa9\xe4\xf2\xc2?\xf4\x05pP\xb9\xfdxW\x1f7:\xce='
b'0125'
b'\x17\x9f\x91-Q]\xdb\x97\xa0\x8f\xee\xb4\xe3v\x99aH\xda;\xb7\xcb]\x97K\x81<\x8a\xfb\xdcaf+'
b'0126'
b'\x9d\xa4\x84d|\xdd\xd7\x98]\x9e\xf5\x06\xf9\xbd\x15\x80\xf5\xa8\xdc\x06R8\xdbp\x1b\xc5~\x08\xa3<\\\xa9'
b'0127'
b'h|\xdf<\xae\xaa\x88\x8b\x00\x0e\xdfJ\x01\xd1(\xe3\xb3 &\xb0O\xe2H\xaa0\xab/\xab\xa6\xd3#3'
b'0128'
b'\xee\xb9\xff>\xa6X,\xe2\xbf\x03\xb9\xbb\xff\x95\x88"\x90\xb8\xa8\xe5(\xa3\x91\xbc5i\x17\x92\x8fr\x1c\x06'
b'0129'
b'-\x90|u\xaa\xb2$\x85\x0bkv\xd1^/\xd4q$\x8e\xdfq]\xe8\xf7\x9d\xc8L-A\x1ff?\x88'
b'0132'
b"\xa7H\x02\x8b\x05\x18\xda\x98\xd8\xd2F\xe6\x1a8\x96\xa6w\x05\x97^'\xc3\xa0B\xb1E\r\xa9\\\xe3\x9bU"
b'0134'
....

Amazon Neptune on submitting query: AttributeError: 'str' object has no attribute 'source_instructions'

I have the following code running on AWS lambda, but getting the following error.
Error
[ERROR] AttributeError: 'str' object has no attribute 'source_instructions'
Traceback (most recent call last):
File "/var/task/gremlin_python/driver/driver_remote_connection.py", line 56, in submit
    result_set = self._client.submit(bytecode, request_options=self._extract_request_options(bytecode))
  File "/var/task/gremlin_python/driver/driver_remote_connection.py", line 81, in _extract_request_options
    options_strategy = next((x for x in bytecode.source_instructionsEND RequestId: 4ee8073c-e941-43b3-8014-8717893b3188
Source code
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
def test_neptune(host):
remoteConn = DriverRemoteConnection('wss://{}:8182/gremlin','g'.format(host))
query = "g.V().groupCount().by(label).unfold().project('label','count').by(keys).by(values)"
response = remoteConn.submit(query)
print("response-> {}" .format(response))
# iterate repsonse
# go thru label
for set_item in response:
for item in set_item:
print("item-> item: {}".format(item))
remoteConn.close()
test_neptune()
Your DriverRemoteConnection call is wrong. You have:
remoteConn = DriverRemoteConnection('wss://{}:8182/gremlin','g'.format(host))
So you are sending {} as the hostname, and passing 'g' as a second parameter, which is probably where the error comes from. I don't know what you intended the 'g' for, but you probably want:
remoteConn = DriverRemoteConnection('wss://{}:8182/gremlin'.format(host))
If you send the query as a text string you need to create the Client object differently or write the query as in-line Python. There are two examples at (1) and (2) that show each option. The error you are seeing is because the server is trying to find Gremlin bytecode in the packet sent but only found a string (which does not have a source_instructions method).
Using a DriverRemoteConnection you can use a Python line of code such as:
result = (g.V().groupCount().
by(label).
unfold().
project('label','count').
by(keys).
by(values).
next())
If you actually want/need to send the query as a string instead of bytecode, please see my answer to this question
https://github.com/krlawrence/graph/blob/master/sample-code/basic-client.py
https://github.com/krlawrence/graph/blob/master/sample-code/glv-client.py

Access JSON file data using preset query - Python

I am reading a json file with dictionary and values, but I am battling to use a variable as a query item when searching the json file.
x = value_cloud = "%s%s%s" % (["L1_METADATA_FILE"],["IMAGE_ATTRIBUTES"],["CLOUD_COVER"])
for meta in filelist(dir):
with open (meta) as data_file:
data = json.load(data_file)
cloud = str(data[x])
The error I get is:
Traceback (most recent call last):
File "E:\SAMPLE\Sample_Script_AWS\L8_TOA_using_gdal_rasterio.py", line 96, in <module>
cloud = str(data[x])
KeyError: "['L1_METADATA_FILE']['IMAGE_ATTRIBUTES']['CLOUD_COVER']"
What I actually want is to search the json file for the key in the variable...
The keys do exist in the json file because when I run the following I get the correct output.
cloud = str(data["L1_METADATA_FILE"]["IMAGE_ATTRIBUTES"]["CLOUD_COVER"])
print cloud
My knowledge of python is sketchy, and I am passing the variable through as a string and not an expression or object and therefore it gives me that error. What is the correct way to create the variable and call the keys that I want.
Thanks in advance!
Your key ends up including the brackets in the string, which which where the error comes from. If you use each key in its own variable, like this:
x, y, z = "L1_METADATA_FILE", "IMAGE_ATTRIBUTES" , "CLOUD_COVER"
and then:
cloud = str(data[x][y][z])
it should avoid any errors.

Categories

Resources