From 2ad3493fb027df838c4af792817ed6caa2cac7ce Mon Sep 17 00:00:00 2001 From: shortcutme Date: Tue, 19 Nov 2019 02:05:02 +0100 Subject: [PATCH] Test and benchmark of crypto function in CryptMessage plugin --- plugins/CryptMessage/CryptMessagePlugin.py | 58 ++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/plugins/CryptMessage/CryptMessagePlugin.py b/plugins/CryptMessage/CryptMessagePlugin.py index 64c8ac81..c3907669 100644 --- a/plugins/CryptMessage/CryptMessagePlugin.py +++ b/plugins/CryptMessage/CryptMessagePlugin.py @@ -170,3 +170,61 @@ class UserPlugin(object): publickey = btctools.encode_pubkey(btctools.privtopub(privatekey), "bin_compressed") site_data["encrypt_publickey_%s" % index] = base64.b64encode(publickey).decode("utf8") return site_data["encrypt_publickey_%s" % index] + + +@PluginManager.registerTo("Actions") +class ActionsPlugin: + publickey = "A3HatibU4S6eZfIQhVs2u7GLN5G9wXa9WwlkyYIfwYaj" + privatekey = "5JBiKFYBm94EUdbxtnuLi6cvNcPzcKymCUHBDf2B6aq19vvG3rL" + utf8_text = '\xc1rv\xedzt\xfbr\xf5t\xfck\xf6rf\xfar\xf3g\xe9p' + + def getBenchmarkTests(self, online=False): + if hasattr(super(), "getBenchmarkTests"): + tests = super().getBenchmarkTests(online) + else: + tests = [] + + aes_key, encrypted = CryptMessage.eciesEncrypt(self.utf8_text.encode("utf8"), self.publickey) # Warm-up + tests.extend([ + {"func": self.testCryptEciesEncrypt, "kwargs": {}, "num": 100, "time_standard": 1.2}, + {"func": self.testCryptEciesDecrypt, "kwargs": {}, "num": 500, "time_standard": 1.3}, + {"func": self.testCryptAesEncrypt, "kwargs": {}, "num": 10000, "time_standard": 0.27}, + {"func": self.testCryptAesDecrypt, "kwargs": {}, "num": 10000, "time_standard": 0.25} + ]) + return tests + + def testCryptEciesEncrypt(self, num_run=1): + for i in range(num_run): + aes_key, encrypted = CryptMessage.eciesEncrypt(self.utf8_text.encode("utf8"), self.publickey) + assert len(aes_key) == 32 + yield "." + + def testCryptEciesDecrypt(self, num_run=1): + aes_key, encrypted = CryptMessage.eciesEncrypt(self.utf8_text.encode("utf8"), self.publickey) + for i in range(num_run): + assert len(aes_key) == 32 + ecc = CryptMessage.getEcc(self.privatekey) + assert ecc.decrypt(encrypted) == self.utf8_text.encode("utf8"), "%s != %s" % (ecc.decrypt(encrypted), self.utf8_text.encode("utf8")) + yield "." + + def testCryptAesEncrypt(self, num_run=1): + from lib import pyelliptic + + for i in range(num_run): + key = os.urandom(32) + iv = pyelliptic.Cipher.gen_IV('aes-256-cbc') + encrypted = pyelliptic.Cipher(key, iv, 1, ciphername='aes-256-cbc').ciphering(self.utf8_text.encode("utf8")) + yield "." + + def testCryptAesDecrypt(self, num_run=1): + from lib import pyelliptic + + key = os.urandom(32) + iv = pyelliptic.Cipher.gen_IV('aes-256-cbc') + encrypted_text = pyelliptic.Cipher(key, iv, 1, ciphername='aes-256-cbc').ciphering(self.utf8_text.encode("utf8")) + + for i in range(num_run): + ctx = pyelliptic.Cipher(key, iv, 0, ciphername='aes-256-cbc') + decrypted = ctx.ciphering(encrypted_text).decode("utf8") + assert decrypted == self.utf8_text + yield "."