how to make a PKCS8 RSA signature in python

>>> from Crypto.Signature import PKCS1_v1_5
>>> from Crypto.Hash import SHA
>>> from Crypto.PublicKey import RSA
>>>
>>> message = 'To be signed'
>>> key = RSA.importKey(open('privkey.der').read())
>>> h = SHA.new(message)
>>> signer = PKCS1_v1_5.new(key)
>>> signature = signer.sign(h)

Here is what the above code is Doing:
1. Importing the PKCS1_v1_5 module from the Crypto.Signature package.
2. Importing the SHA module from the Crypto.Hash package.
3. Importing the RSA module from the Crypto.PublicKey package.
4. Creating a message to be signed.
5. Importing the private key from a file.
6. Creating a SHA object by passing the message to the SHA.new() method.
7. Creating a PKCS1_v1_5 object by passing the private key to the PKCS1_v1_5.new() method.
8. Signing the message by passing the SHA object to the sign() method.

Verifying the Signature

To verify the signature, we need the public key and the signature.

>>> from Crypto.Signature import PKCS1_v1_5
>>> from Crypto.Hash import SHA
>>> from Crypto.PublicKey import RSA
>>>
>>> message = ‘To be signed’
>>> key = RSA.importKey(open(‘pubkey.der’).read())
>>> h = SHA.new(message)
>>> verifier = PKCS1_v1_5.new(key)
>>> if verifier.verify(h, signature):
… print “The signature is authentic.”
… else:
… print “The signature is not authentic.”