Package: ksi-python
This is a thin wrapper on top of KSI C SDK. Experimental, non-supported code.SynopsisExample of synchronous singing and verification.importksiimporthashlib# Instantiate service parameters from the environmentKSI=ksi.KSI(**ksi.ksi_env())# Sign a text stringsig=KSI.sign_hash(hashlib.sha256(b"Tere!"))# Print some signature propertiesprint(sig.get_signing_time(),sig.get_signer_id())# Now verify this text string, first obtaining a data hasherh=sig.get_hasher()h.update(b"Tere!")print(KSI.verify_hash(sig,h))# Obtain a binary blob which can be stored for long termserialized_signature=sig.get_binary()# Some time have passed, fetch the signature and verify againsig2=KSI.new_signature_object(serialized_signature)print(KSI.verify_hash(sig2,h))Note that asynchronous signing can provide a significant speedup when multiple signatures are requested.
Example of asynchronous singing with gevent.importksiimporthashlibfromgevent.poolimportPool# Instantiate service parameters from the environmentKSI=ksi.KSI(**ksi.ksi_env())# Multiple strings to be signedstring_list=["This","is","a","list","of","strings"]# Define a signer function.defsign_hash(h):sig=KSI.sign_hash(h)# Verification and/or storing could be done hereprint(KSI.verify_hash(sig,h))# Create a gevent pool. Note that for optimal efficiency# pool size should not be smaller than# ``KSI.get_async_config()['max_pending_count']``pool=Pool(100)# Sign all strings asynchronouslyforstringinstring_list:pool.spawn(sign_hash,hashlib.sha256(string.encode()))pool.join()Client-side aggregation (signing in blocks) is also possible if the KSI Gateway allows it. This means
that multiple hashes can be individually signed by a single request to the Gateway server. In addition, if
block signing is allowed, the asynchronous signing service will by default dynamically sign in blocks depending
on if the signing requests demand becomes too large to efficiently sign them one-by-one.
Example of signing a block of hashes synchronously (asynchronous block signing is supported as well).importksiimporthashlib# Instantiate service parameters from the environmentKSI=ksi.KSI(**ksi.ksi_env())# Multiple strings to be signedstring_list=["This","is","a","list","of","strings"]# Hashes of stringshash_list=[hashlib.sha256(string.encode())forstringinstring_list]# Sign hashes in a blocksigs=KSI.sign_hash_list(hash_list)# Verify hashesforiinrange(len(sigs)):print(KSI.verify_hash(sigs[i],hash_list[i]))InstallRequirements: Python 2.7+ or Python 3.1+. Jython, IronPython are not supported.Install fresh libksi aka KSI C SDK; seehttps://github.com/guardtime/libksi/Install python-devel packageRun:> pip install ksi-pythonor> easy_install ksi-pythonTestsSpecify KSI Gateway access parameters and run> python setup.py testTo test if KSI Python SDK signs asynchronously with gevent, make sure gevent is installed.Documentationhttp://guardtime.github.io/ksi-python/Type:> pydoc ksito read the documentation after installation. Generating html or pdf documentation:
make sure that dependencies like sphinx (pip install sphinx) are installed, extension
is built (python setup.py build) and run:> cd docs
> make html
or
> make latexpdfLicenseApache 2.0. Please contact Guardtime for supported options.