views:

337

answers:

1

I've got the following Java code that I'm trying to convert to python, and I'm not really sure how to do this:

import java.security.spec.X509EncodedKeySpec;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.Signature;

byte[] key = KeyReader.read(filestream) 
  //KeyReader.read(inputstream) just reads in the bytes 1 at a time from the filestream
X509EncodedKeySpec pubKey = new X509EncodedKeySpec(key);
KeyFactory keyFactory = KeyFactory.getInstance("DSA");

PublicKey pub = keyFactory.generatePublic(pubKey);

Signature signature = Signature.getInstance("SHA1withDSA");
signature.initVerify(pub)
signature.update(a_byte_string) //doesn't matter

I'm kinda lost as to how to do this in python. Specifically, the SHA1withDSA part. I just don't know enough about the python crypto libs (m2crypto to be exact) to map the functions (nor could I find any decent writeups on how to do this).

A: 

I don't exactly understand the Java code, but is this what you are trying to do?

from M2Crypto import X509

x509 = X509.load_cert(filename)
assert x509.verify() == 1
Heikki Toivonen