-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathipfs-add-from-encrypted.py
executable file
·75 lines (64 loc) · 2.3 KB
/
ipfs-add-from-encrypted.py
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
#!/usr/bin/env python3
# Takes file in does symmetric encryption with the password you provide
# then adds it to a running IPFS(ipfs.io) instance.
#
import os
import argparse
import ipfsapi
import subprocess
# Parse command arguments
parser = argparse.ArgumentParser(description='Encrypt file/directory and add it to IPFS')
parser.add_argument('-i','--input', help='File.txt or Directory', required=True)
parser.add_argument('-n','--name', help='Set encrypted output filename', required=True)
args = parser.parse_args()
# Get dataToEncrypt full path
dataToEncrypt = (os.path.abspath(args.input))
# File
fileReady = (args.name + ".gpg")
# Tar
tarReady = (args.name + ".tgz.gpg")
def ipfsConnect():
# Tell module where IPFS instance is located
try:
api = ipfsapi.connect('127.0.0.1', 5001)
return(api)
except:
print("IPFS Daemon not running!")
quit()
def packageData():
if os.path.isfile(dataToEncrypt):
subprocess.run(["gpg", "-o", args.name + ".gpg", "-c", dataToEncrypt])
else:
ps = subprocess.Popen(("tar", "-cz", dataToEncrypt),stdout=subprocess.PIPE)
output = subprocess.check_output(("gpg", "-c", "-o", args.name + ".tgz.gpg"), stdin=ps.stdout)
ps.wait()
def ipfsFile():
try:
api = ipfsConnect()
# Add encrypted file to IPFS
ipfsLoadedFile = api.add(fileReady, wrap_with_directory=True)
# Return Hash of new IPFS File
fullHash = (ipfsLoadedFile[1])
ipfsFile.ipfsHash = fullHash['Hash']
return(ipfsFile.ipfsHash)
except:
# Add encrypted directory to IPFS
ipfsLoadedFile = api.add(tarReady, wrap_with_directory=True)
# Return Hash of new IPFS File
fullHash = (ipfsLoadedFile[1])
ipfsFile.ipfsHash = fullHash['Hash']
return(ipfsFile.ipfsHash)
def delEncryptedFile():
if os.path.isfile(fileReady):
os.remove(fileReady)
elif os.path.isfile(tarReady):
os.remove(tarReady)
def main():
ipfsConnect()
packageData()
ipfsFile()
print ("File encrypted and added to IPFS with this hash " + ipfsFile.ipfsHash)
print ("Be sure to use secure messaging like Signal.org or Keybase.io to send the password to the file recipient.")
delEncryptedFile()
if __name__ == "__main__":
main()