This repository has been archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #294 from lzpap/tutorial6
Add Tutorials `6. Store Encrypted Data` and `7. Fetch Encrypted Data`
- Loading branch information
Showing
3 changed files
with
323 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
""" | ||
Encrypt data and store it on the Tangle. | ||
simplecrypt library is needed for this example (`pip install simple-crypt`)! | ||
""" | ||
from iota import Iota, TryteString, Tag, ProposedTransaction | ||
from simplecrypt import encrypt | ||
from base64 import b64encode | ||
from getpass import getpass | ||
|
||
import json | ||
|
||
# Declare an API object | ||
api = Iota( | ||
adapter='https://nodes.devnet.iota.org:443', | ||
seed=b'YOURSEEDFROMTHEPREVIOUSTUTORIAL', | ||
testnet=True, | ||
) | ||
|
||
# Some confidential information | ||
data = { | ||
'name' : 'James Bond', | ||
'age' : '32', | ||
'job' : 'agent', | ||
'address' : 'London', | ||
} | ||
|
||
# Convert to JSON format | ||
json_data = json.dumps(data) | ||
|
||
# Ask user for a password to use for encryption | ||
password = getpass('Please supply a password for encryption:') | ||
|
||
print('Encrypting data...') | ||
# Encrypt data | ||
# Note, that in Python 3, encrypt returns 'bytes' | ||
cipher = encrypt(password, json_data) | ||
|
||
# Encode to base64, output contains only ASCII chars | ||
b64_cipher = b64encode(cipher) | ||
|
||
print('Constructing transaction locally...') | ||
# Convert to trytes | ||
trytes_encrypted_data = TryteString.from_bytes(b64_cipher) | ||
|
||
# Generate an address from your seed to post the transfer to | ||
my_address = api.get_new_addresses(index=42)['addresses'][0] | ||
|
||
# Tag is optional here | ||
my_tag = Tag(b'CONFIDENTIALINFORMATION') | ||
|
||
# Prepare a transaction object | ||
tx = ProposedTransaction( | ||
address=my_address, | ||
value=0, | ||
tag=my_tag, | ||
message=trytes_encrypted_data, | ||
) | ||
|
||
print('Sending transfer...') | ||
# Send the transaction to the network | ||
response = api.send_transfer([tx]) | ||
|
||
print('Check your transaction on the Tangle!') | ||
print('https://utils.iota.org/transaction/%s/devnet' % response['bundle'][0].hash) | ||
print('Tail transaction hash of the bundle is: %s' % response['bundle'].tail_transaction.hash) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
""" | ||
Decrypt data fetched from the Tangle. | ||
simplecrypt library is needed for this example (`pip install simple-crypt`)! | ||
""" | ||
from iota import Iota | ||
from simplecrypt import decrypt | ||
from base64 import b64decode | ||
from getpass import getpass | ||
|
||
import json | ||
|
||
# Declare an API object | ||
api = Iota('https://nodes.devnet.iota.org:443', testnet=True) | ||
|
||
# Prompt user for tail tx hash of the bundle | ||
tail_hash = input('Tail transaction hash of the bundle: ') | ||
|
||
print('Looking for bundle on the Tangle...') | ||
# Fetch bundle | ||
bundle = api.get_bundles(tail_hash)['bundles'][0] | ||
|
||
print('Extracting data from bundle...') | ||
# Get all messages from the bundle and concatenate them | ||
b64_encrypted_data = "".join(bundle.get_messages()) | ||
|
||
# Decode from base64 | ||
encrypted_data = b64decode(b64_encrypted_data) | ||
|
||
# Prompt for passwword | ||
password = getpass('Password to be used for decryption:') | ||
|
||
print('Decrypting data...') | ||
# Decrypt data | ||
# decrypt returns 'bytes' in Python 3, decode it into string | ||
json_data = decrypt(password, encrypted_data).decode('utf-8') | ||
|
||
# Convert JSON string to python dict object | ||
data = json.loads(json_data) | ||
|
||
print('Succesfully decrypted the following data:') | ||
print(data) |