Skip to content

Commit 99e9664

Browse files
authored
Merge pull request #1556 from opacam/python-core-encryption-app
[WIP][CORE UPDATE - PART XV] Add encryption test app
2 parents 6295c36 + e031b2a commit 99e9664

File tree

3 files changed

+374
-0
lines changed

3 files changed

+374
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
from distutils.core import setup
3+
from setuptools import find_packages
4+
5+
options = {'apk': {'requirements': 'libffi,openssl,sdl2,pyjnius,kivy,python2,'
6+
'cryptography,pycrypto,scrypt,m2crypto,'
7+
'pysha3,pycryptodome,libtorrent',
8+
'android-api': 27,
9+
'ndk-api': 21,
10+
'dist-name': 'bdisttest_encryption',
11+
'ndk-version': '10.3.2',
12+
'arch': 'armeabi-v7a',
13+
'permissions': ['INTERNET', 'VIBRATE'],
14+
}}
15+
16+
package_data = {'': ['*.py',
17+
'*.png']
18+
}
19+
20+
setup(
21+
name='testapp_encryption',
22+
version='1.0',
23+
description='p4a setup.py test',
24+
author='Pol Canelles',
25+
author_email='[email protected]',
26+
packages=find_packages(),
27+
options=options,
28+
package_data={'testapp_encryption': ['*.py', '*.png']}
29+
)
187 KB
Loading

testapps/testapp_encryption/main.py

Lines changed: 345 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,345 @@
1+
print('main.py was successfully called')
2+
3+
import os
4+
5+
print('imported os')
6+
7+
print('this dir is', os.path.abspath(os.curdir))
8+
9+
print('contents of this dir', os.listdir('./'))
10+
11+
import sys
12+
13+
print('pythonpath is', sys.path)
14+
15+
import kivy
16+
17+
print('imported kivy')
18+
print('file is', kivy.__file__)
19+
20+
from kivy.app import App
21+
22+
from kivy.lang import Builder
23+
from kivy.properties import StringProperty
24+
25+
from kivy.uix.popup import Popup
26+
from kivy.clock import Clock
27+
28+
print('Imported kivy')
29+
from kivy.utils import platform
30+
31+
print('platform is', platform)
32+
33+
# Test cryptography
34+
try:
35+
from cryptography.fernet import Fernet
36+
37+
key = Fernet.generate_key()
38+
f = Fernet(key)
39+
cryptography_encrypted = f.encrypt(
40+
b'A really secret message. Not for prying eyes.')
41+
cryptography_decrypted = f.decrypt(cryptography_encrypted)
42+
except Exception as e1:
43+
print('**************************')
44+
print('Error on cryptography operations:\n{}'.format(e1))
45+
print('**************************')
46+
cryptography_encrypted = 'Error'
47+
cryptography_decrypted = 'Error'
48+
49+
# Test pycrypto
50+
crypto_hash_message = 'A secret message'
51+
try:
52+
from Crypto.Hash import SHA256
53+
54+
hash = SHA256.new()
55+
hash.update(crypto_hash_message)
56+
crypto_hash_hexdigest = hash.hexdigest()
57+
except Exception as e2:
58+
print('**************************')
59+
print('Error on Crypto operations:\n{}'.format(e2))
60+
print('**************************')
61+
crypto_hash_hexdigest = 'Error'
62+
63+
# Test scrypt
64+
try:
65+
from scrypt import *
66+
67+
status_import_scrypt = 'Success'
68+
except ImportError as e3:
69+
print('**************************')
70+
print('Unable to import scrypt:\n{}'.format(e3))
71+
print('**************************')
72+
status_import_scrypt = 'Error'
73+
74+
# Test M2Crypto
75+
try:
76+
from M2Crypto import *
77+
78+
status_import_m2crypto = 'Success'
79+
except ImportError as e5:
80+
print('**************************')
81+
print('Unable to import M2Crypto:\n{}'.format(e5))
82+
print('**************************\n')
83+
status_import_m2crypto = 'Error'
84+
85+
# Test pysha3
86+
try:
87+
import sha3
88+
89+
print('Ok imported pysha3, testing some basic operations...')
90+
k = sha3.keccak_512()
91+
k.update(b"data")
92+
print('Test pysha3 operation (keccak_512): {}'.format(k.hexdigest()))
93+
status_import_pysha3 = 'Success'
94+
except ImportError as e6:
95+
print('**************************')
96+
print('Unable to import/operate with pysha3:\n{}'.format(e6))
97+
print('**************************')
98+
status_import_pysha3 = 'Error'
99+
100+
# Test pycryptodome
101+
try:
102+
from Crypto.PublicKey import RSA
103+
104+
print('Ok imported pycryptodome, testing some basic operations...')
105+
secret_code = "Unguessable"
106+
key = RSA.generate(2048)
107+
encrypted_key = key.export_key(passphrase=secret_code, pkcs=8,
108+
protection="scryptAndAES128-CBC")
109+
print('\t -> Testing key for secret code "Unguessable": {}'.format(
110+
encrypted_key))
111+
112+
file_out = open("rsa_key.bin", "wb")
113+
file_out.write(encrypted_key)
114+
print('\t -> Testing key write: {}'.format(
115+
'ok' if os.path.exists(file_out) else 'fail'))
116+
117+
print('\t -> Testing Public key:'.format(key.publickey().export_key()))
118+
status_import_pycryptodome = 'Success (import and doing simple operations)'
119+
except ImportError as e6:
120+
print('**************************')
121+
print('Unable to import/operate with pycryptodome:\n{}'.format(e6))
122+
print('**************************')
123+
status_import_pycryptodome = 'Error'
124+
125+
# Test libtorrent
126+
try:
127+
import libtorrent as lt
128+
129+
print('Imported libtorrent version {}'.format(lt.version))
130+
status_import_libtorrent = 'Success (version is: {})'.format(lt.version)
131+
except Exception as e4:
132+
print('**************************')
133+
print('Unable to import libtorrent:\n{}'.format(e4))
134+
print('**************************')
135+
status_import_libtorrent = 'Error'
136+
137+
kv = '''
138+
#:import Metrics kivy.metrics.Metrics
139+
#:import sys sys
140+
141+
<FixedSizeButton@Button>:
142+
size_hint_y: None
143+
height: dp(60)
144+
145+
<TestImport@BoxLayout>:
146+
orientation: 'vertical'
147+
size_hint_y: None
148+
height: self.minimum_height
149+
test_module: ''
150+
test_result: ''
151+
Label:
152+
height: self.texture_size[1]
153+
size_hint_y: None
154+
text_size: self.size[0], None
155+
markup: True
156+
text: '[b]*** TEST {} MODULE ***[/b]'.format(self.parent.test_module)
157+
halign: 'center'
158+
Label:
159+
height: self.texture_size[1]
160+
size_hint_y: None
161+
text_size: self.size[0], None
162+
markup: True
163+
text:
164+
'Import {}: [color=a0a0a0]{}[/color]'.format(
165+
self.parent.test_module, self.parent.test_result)
166+
halign: 'left'
167+
Widget:
168+
size_hint_y: None
169+
height: 20
170+
171+
172+
ScrollView:
173+
GridLayout:
174+
cols: 1
175+
size_hint_y: None
176+
height: self.minimum_height
177+
FixedSizeButton:
178+
text: 'test pyjnius'
179+
on_press: app.test_pyjnius()
180+
Label:
181+
height: self.texture_size[1]
182+
size_hint_y: None
183+
text_size: self.size[0], None
184+
markup: True
185+
text: '[b]*** TEST CRYPTOGRAPHY MODULE ***[/b]'
186+
halign: 'center'
187+
Label:
188+
height: self.texture_size[1]
189+
size_hint_y: None
190+
text_size: self.size[0], None
191+
markup: True
192+
text:
193+
'Cryptography decrypted:\\n[color=a0a0a0]%s[/color]\\n' \\
194+
'Cryptography encrypted:\\n[color=a0a0a0]%s[/color]' % (
195+
app.cryptography_decrypted, app.cryptography_encrypted)
196+
halign: 'left'
197+
Widget:
198+
size_hint_y: None
199+
height: 20
200+
Label:
201+
height: self.texture_size[1]
202+
size_hint_y: None
203+
text_size: self.size[0], None
204+
markup: True
205+
text: '[b]*** TEST CRYPTO MODULE ***[/b]'
206+
halign: 'center'
207+
Label:
208+
height: self.texture_size[1]
209+
size_hint_y: None
210+
text_size: self.size[0], None
211+
markup: True
212+
text:
213+
'Crypto message: \\n[color=a0a0a0]%s[/color]\\n'\\
214+
'Crypto hex: \\n[color=a0a0a0]%s[/color]' % (
215+
app.crypto_hash_message, app.crypto_hash_hexdigest)
216+
halign: 'left'
217+
Widget:
218+
size_hint_y: None
219+
height: 20
220+
TestImport:
221+
test_module: 'scrypt'
222+
test_result: app.status_import_scrypt
223+
TestImport:
224+
test_module: 'm2crypto'
225+
test_result: app.status_import_m2crypto
226+
TestImport:
227+
test_module: 'pysha3'
228+
test_result: app.status_import_pysha3
229+
TestImport:
230+
test_module: 'pycryptodome'
231+
test_result: app.status_import_pycryptodome
232+
TestImport:
233+
test_module: 'libtorrent'
234+
test_result: app.status_import_libtorrent
235+
Image:
236+
keep_ratio: False
237+
allow_stretch: True
238+
source: 'colours.png'
239+
size_hint_y: None
240+
height: dp(100)
241+
Label:
242+
height: self.texture_size[1]
243+
size_hint_y: None
244+
font_size: 100
245+
text_size: self.size[0], None
246+
markup: True
247+
text: '[b]Kivy[/b] on [b]SDL2[/b] on [b]Android[/b]!'
248+
halign: 'center'
249+
Label:
250+
height: self.texture_size[1]
251+
size_hint_y: None
252+
text_size: self.size[0], None
253+
markup: True
254+
text: sys.version
255+
halign: 'center'
256+
padding_y: dp(10)
257+
Widget:
258+
size_hint_y: None
259+
height: 20
260+
Label:
261+
height: self.texture_size[1]
262+
size_hint_y: None
263+
font_size: 50
264+
text_size: self.size[0], None
265+
markup: True
266+
text:
267+
'dpi: [color=a0a0a0]%s[/color]\\n'\\
268+
'density: [color=a0a0a0]%s[/color]\\n'\\
269+
'fontscale: [color=a0a0a0]%s[/color]' % (
270+
Metrics.dpi, Metrics.density, Metrics.fontscale)
271+
halign: 'center'
272+
FixedSizeButton:
273+
text: 'test ctypes'
274+
on_press: app.test_ctypes()
275+
Widget:
276+
size_hint_y: None
277+
height: 1000
278+
on_touch_down: print('touched at', args[-1].pos)
279+
280+
<ErrorPopup>:
281+
title: 'Error'
282+
size_hint: 0.75, 0.75
283+
Label:
284+
text: root.error_text
285+
'''
286+
287+
288+
class ErrorPopup(Popup):
289+
error_text = StringProperty('')
290+
291+
292+
def raise_error(error):
293+
print('ERROR:', error)
294+
ErrorPopup(error_text=error).open()
295+
296+
297+
class TestApp(App):
298+
cryptography_encrypted = cryptography_encrypted
299+
cryptography_decrypted = cryptography_decrypted
300+
crypto_hash_message = crypto_hash_message
301+
crypto_hash_hexdigest = crypto_hash_hexdigest
302+
status_import_scrypt = status_import_scrypt
303+
status_import_m2crypto = status_import_m2crypto
304+
status_import_pysha3 = status_import_pysha3
305+
status_import_pycryptodome = status_import_pycryptodome
306+
status_import_libtorrent = status_import_libtorrent
307+
308+
def build(self):
309+
root = Builder.load_string(kv)
310+
Clock.schedule_interval(self.print_something, 2)
311+
# Clock.schedule_interval(self.test_pyjnius, 5)
312+
print('testing metrics')
313+
from kivy.metrics import Metrics
314+
print('dpi is', Metrics.dpi)
315+
print('density is', Metrics.density)
316+
print('fontscale is', Metrics.fontscale)
317+
return root
318+
319+
def print_something(self, *args):
320+
print('App print tick', Clock.get_boottime())
321+
322+
def on_pause(self):
323+
return True
324+
325+
def test_pyjnius(self, *args):
326+
try:
327+
from jnius import autoclass
328+
except ImportError:
329+
raise_error('Could not import pyjnius')
330+
return
331+
332+
print('Attempting to vibrate with pyjnius')
333+
python_activity = autoclass('org.kivy.android.PythonActivity')
334+
activity = python_activity.mActivity
335+
intent = autoclass('android.content.Intent')
336+
context = autoclass('android.content.Context')
337+
vibrator = activity.getSystemService(context.VIBRATOR_SERVICE)
338+
339+
vibrator.vibrate(1000)
340+
341+
def test_ctypes(self, *args):
342+
import ctypes
343+
344+
345+
TestApp().run()

0 commit comments

Comments
 (0)