Browse Source

Update demo_dynamic.py

added error strings (with function returning a string type) and a decryption to ChCha.
Larry Bugbee 8 years ago
parent
commit
695c3b235d
1 changed files with 23 additions and 6 deletions
  1. 23 6
      demos/demo_dynamic.py

+ 23 - 6
demos/demo_dynamic.py

@@ -1,7 +1,7 @@
 
 
 
 
 """
 """
-    demo_dynamic.py                                     v2
+    demo_dynamic.py                                     v2b
 
 
     This program demonstrates Python's use of the dynamic
     This program demonstrates Python's use of the dynamic
     language support additions to LTC, namely access to LTC
     language support additions to LTC, namely access to LTC
@@ -33,11 +33,12 @@
               mathlib.  For example, public key crypto requires
               mathlib.  For example, public key crypto requires
               a mathlib; hashing and symmetric encryption do not.
               a mathlib; hashing and symmetric encryption do not.
 
 
-    This code was written for Python 2.7.
+    This code was written for Python 2.7 with the ctypes standard
+    library.
 
 
     Larry Bugbee
     Larry Bugbee
     March 2014      v1
     March 2014      v1
-    August 2017     v2
+    August 2017     v2b
 
 
 """
 """
 
 
@@ -195,6 +196,13 @@ def _get_constant(name):
         raise Exception('LTC.crypt_get_constant(%s) rc = %d' % (name, rc))
         raise Exception('LTC.crypt_get_constant(%s) rc = %d' % (name, rc))
     return constant.value
     return constant.value
 
 
+def _err2str(err):
+    # define return type
+    errstr = LTC.error_to_string
+    errstr.restype = c_char_p
+    # get and return err string
+    return errstr(err)
+
 CRYPT_OK = _get_constant('CRYPT_OK')
 CRYPT_OK = _get_constant('CRYPT_OK')
 
 
 class SHA256(object):
 class SHA256(object):
@@ -213,15 +221,17 @@ class ChaCha(object):
         self.state   = c_buffer(_get_size('chacha_state'))
         self.state   = c_buffer(_get_size('chacha_state'))
         self.counter = c_int(1)
         self.counter = c_int(1)
         err = LTC.chacha_setup(byref(self.state), key, len(key), rounds)
         err = LTC.chacha_setup(byref(self.state), key, len(key), rounds)
+        if err != CRYPT_OK:
+            raise Exception('LTC.chacha_setup(), err = %d, "%s"' % (err, _err2str(err)))
     def set_iv32(self, iv):
     def set_iv32(self, iv):
         err = LTC.chacha_ivctr32(byref(self.state), iv, len(iv), byref(self.counter))
         err = LTC.chacha_ivctr32(byref(self.state), iv, len(iv), byref(self.counter))
         if err != CRYPT_OK:
         if err != CRYPT_OK:
-            raise Exception('LTC.chacha_ivctr32() err = %d' % err)
+            raise Exception('LTC.chacha_ivctr32(), err = %d, "%s"' % (err, _err2str(err)))
     def crypt(self, datain):
     def crypt(self, datain):
         dataout = c_buffer(len(datain))
         dataout = c_buffer(len(datain))
         err = LTC.chacha_crypt(byref(self.state), datain, len(datain), byref(dataout))
         err = LTC.chacha_crypt(byref(self.state), datain, len(datain), byref(dataout))
         if err != CRYPT_OK:
         if err != CRYPT_OK:
-            raise Exception('LTC.chacha_crypt() err = %d' % err)
+            raise Exception('LTC.chacha_crypt(), err = %d, "%s"' % (err, _err2str(err)))
         return dataout.raw
         return dataout.raw
 
 
 # - - - - - - - - - - - - -
 # - - - - - - - - - - - - -
@@ -254,8 +264,15 @@ if SHOW_CHACHA_EXAMPLE:
     cha.set_iv32(iv)
     cha.set_iv32(iv)
     cipher = cha.crypt(plain)
     cipher = cha.crypt(plain)
 
 
-    template = '\n  ChaCha%d ciphertext for "%s" is "%s" \n'
+    template = '\n  ChaCha%d ciphertext   for "%s" is "%s"'
     print template % (rounds, plain, cipher.encode('hex'))
     print template % (rounds, plain, cipher.encode('hex'))
+    
+    # reset to decrypt
+    cha.set_iv32(iv)
+    decrypted = cha.crypt(cipher)
+
+    template = '  ChaCha%d decoded text for "%s" is "%s" \n'
+    print template % (rounds, plain, decrypted)