Python for CryptoSys PKI¶
This is a Python interface to the CryptoSys PKI Pro library.
CryptoSys PKI Pro is a library to carry out public key cryptography using RSA and ECDSA. You can generate and manage X.509 certificates, PKCS#8/PKCS#12 private keys and CMS/PKCS#7 objects. It includes all required supporting cryptographic utilities.
CryptoSys PKI Pro v23.0 or above must be installed on your system. This is available from
To use in Python's REPL¶
>>> from cryptosyspki import *
>>> Gen.version() # "hello world!" for CryptoSys PKI
220100
>>> Hash.hex_from_data(b'abc') # compute SHA-1 hash in hex of 'abc' as bytes
'a9993e364706816aba3e25717850c26c9cd0d89d'
>>> Hash.hex_from_string('abc', Hash.Alg.SHA256) # same but over a string and using SHA-256
'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad'
>>> h = Hash.data(b'abc') # h is a byte array (bytes->bytes)
>>> print(Cnv.tohex(h)) # display the byte array in hex
A9993E364706816ABA3E25717850C26C9CD0D89D
If you don't like import *
and find cryptosyspki
a bit long to
type each time, try
>>> import cryptosyspki as pki
>>> pki.Gen.version() # Underlying core PKI dll
230000
>>> pki.__version__ # cryptosyspki.py package
'23.0.00.0000'
Examples¶
There is a series of tests in test_pki.py
(source).
You should find an example there of what you want to do.
These tests require a subdirectory work
in the same folder
which must contain all the required test files.
A spare set of these test files is available separately in pkiPythonTestFiles.zip
test
| test_pki.py
|
\---work
| <...all required test files>
|
\---tmp.XXXXXXXX
<..temporary files...>
The test function creates a new temporary subdirectory each time, which is deleted automatically.
If you want to keep this temp folder for debugging, then add the argument nodelete
on the command line.
This structure is already set up in the distribution file, so unzip the
file cryptosyspki-x.x.x.zip
and open a command-line prompt in the
test
subdirectory. You can do any of the following.
python test_pki.py
py.test -v
Open the file
test_pki.py
using IDLE and selectRun > Run Module (F5)
.
We've tested this using the Python 3.12.0 interpreter and IDLE, PyCharm 2023.2.3, and py.test
7.4.2.
Contact¶
For more information or to make suggestions, please contact us at https://www.cryptosys.net/contact/