This repository was archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 124
Add Tutorials 6. Store Encrypted Data
and 7. Fetch Encrypted Data
#294
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,65 @@ | ||
""" | ||
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 iota.crypto.addresses import AddressGenerator | ||
from simplecrypt import encrypt | ||
from base64 import b64encode | ||
from getpass import getpass | ||
|
||
import json | ||
|
||
# Declare an API object | ||
api = Iota('https://nodes.devnet.iota.org:443', 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 = AddressGenerator(b'YOURSEEDFROMTHEPREVIOUSTUTORIAL').get_addresses( | ||
start=42, | ||
)[0] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❔ Could we provide the seed to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Absolutely! It's less confusing as well, there is no need for user to know about the existence of |
||
|
||
# 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 hidden or 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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.