Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

""" 

@file 

@brief encrypt and decrypt command lines 

""" 

from __future__ import print_function 

import os 

import argparse 

import sys 

 

 

def get_parser(encrypt): # pylint: disable=W0621 

""" 

Defines the way to parse the magic command ``%encrypt`` and ``%decrypt``. 

 

@param encrypt encrypt or decrypt 

@return parser 

""" 

task = "encrypt" if encrypt else "decrypt" 

parser = argparse.ArgumentParser(prog=task, 

description='%s a folder.' % task + 

'\nFor a second run, the program looks into file status' + 

'\nto avoid crypting same file gain, it does only modified files' + 

'\nit does not work well in Python 2.7 with pycryptodome.') 

parser.add_argument( 

'source', 

help='folder to %s' % task) 

parser.add_argument( 

'dest', 

help='location of the %sed files' % task) 

parser.add_argument( 

'password', 

help='password, usually an ascii string with 16x characters') 

if encrypt: 

parser.add_argument( 

'-s', 

'--status', 

default="crypt_status.txt", 

help='to keep track of what was done') 

parser.add_argument( 

'-m', 

'--map', 

default="crypt_map.txt", 

help='mapping between raw files and crypted files') 

else: 

parser.add_argument( 

'-r', 

'--regex', 

default="", 

help='the script can retrieve only a subpart of the data defined by a regular expression') 

 

return parser 

 

 

def do_main(source, dest, password, encrypt, # pylint: disable=W0621 

crypt_file, crypt_map, regex=None, fLOG=None): 

""" 

Encrypts or decrypts a folder, see @see cl EncryptedBackup. 

The function relies on module :epkg:`pycrypto`, :epkg:`cryptography`, 

algoritm `AES <https://fr.wikipedia.org/wiki/Advanced_Encryption_Standard>`_, 

`Fernet <http://cryptography.readthedocs.org/en/latest/fernet/>`_. 

 

@param source source of files to encrypt or decrypt 

@param dest destination 

@param password password 

@param encrypt boolean, True to encrypt 

@param crypt_file encrypted file 

@param crypt_map @see cl EncryptedBackup 

@param regex regular expression to filter in files to retrieve 

@param fLOG logging function 

""" 

if not os.path.exists(source): 

raise FileNotFoundError(source) 

try: 

from pyquickhelper.filehelper import EncryptedBackup, TransferAPIFile, FileTreeNode 

except ImportError: 

folder = os.path.normpath(os.path.join( 

os.path.abspath(os.path.dirname(__file__)), "..", "..")) 

sys.path.append(folder) 

from pyquickhelper.filehelper import EncryptedBackup, TransferAPIFile, FileTreeNode 

 

if isinstance(password, str): 

password = bytes(password, encoding="ascii") 

 

root = source 

local = root 

api = TransferAPIFile(dest) 

 

if encrypt: 

if fLOG: 

fLOG("looking for file in", root) 

ft = FileTreeNode(root, repository=False, fLOG=fLOG, log1=True) 

enc = EncryptedBackup(key=password, file_tree_node=ft, 

transfer_api=api, root_local=local, file_status=crypt_file, 

file_map=crypt_map, fLOG=fLOG) 

 

if fLOG: 

fLOG("start backup") 

issue = enc.start_transfering()[1] 

 

for file, exc in issue: 

if fLOG: 

fLOG("{0} -- {1}".format(file, exc)) 

else: 

enc = EncryptedBackup(key=password, file_tree_node=None, 

transfer_api=api, root_local=None, file_status=None, 

file_map=None, fLOG=fLOG) 

if fLOG: 

fLOG("start restoration") 

enc.retrieve_all(source, regex=regex) 

 

 

def encrypt(fLOG=print, args=None): 

""" 

Encrypts using class @see cl EncryptedBackup. 

The function relies on module :epkg:`pycrypto`, :epkg:`cryptography`, 

algoritm `AES <https://fr.wikipedia.org/wiki/Advanced_Encryption_Standard>`_, 

`Fernet <http://cryptography.readthedocs.org/en/latest/fernet/>`_. 

 

@param fLOG logging function 

@param args to overwrite ``sys.args`` 

 

.. cmdref:: 

:title: Encrypt a string 

:cmd: pyquickhelper.cli.encryption_cli:encrypt 

 

Encrypts a string from the command line. 

""" 

parser = get_parser(True) 

if args is not None and args == ['--help']: 

fLOG(parser.format_help()) 

else: 

try: 

args = parser.parse_args(args=args) 

except SystemExit: 

if fLOG: 

fLOG(parser.format_usage()) 

args = None 

 

if args is not None: 

do_main(source=args.source, dest=args.dest, password=args.password, 

encrypt=True, crypt_file=args.status, crypt_map=args.map, fLOG=fLOG) 

 

 

def decrypt(fLOG=print, args=None): 

""" 

Decrypts using class @see cl EncryptedBackup. 

The function relies on module :epkg:`pycrypto`, :epkg:`cryptography`, 

algoritm `AES <https://fr.wikipedia.org/wiki/Advanced_Encryption_Standard>`_, 

`Fernet <http://cryptography.readthedocs.org/en/latest/fernet/>`_. 

 

@param fLOG logging function 

@param args to overwrite ``sys.args`` 

 

.. cmdref:: 

:title: Decrypt a string 

:cmd: pyquickhelper.cli.encryption_cli:decrypt 

 

Decrypts an encrypted string from the command line. 

""" 

parser = get_parser(False) 

if args is not None and args == ['--help']: 

fLOG(parser.format_help()) 

else: 

try: 

args = parser.parse_args(args=args) 

except SystemExit: 

if fLOG: 

fLOG(parser.format_usage()) 

args = None 

 

if args is not None: 

do_main(source=args.dest, dest=args.source, password=args.password, 

encrypt=False, crypt_file=None, crypt_map=None, 

regex=args.regex if args.regex else None, 

fLOG=fLOG) 

 

 

if __name__ == "__main__": 

decrypt()