def check_rsa_key(sample):
"""
Returns a 3-tuple (is_rsa_key, has_private_component, n_bit_length)
is_rsa_key - a bool indicating that the sample is, in fact, an RSA key
in a format readable by Crypto.PublicKey.RSA.importKey
has_private_component - a bool indicating whether or not d was in the
analyzed key, or false if the sample is not an RSA key
n_bit_length - an int representing the bit length of the modulus found
in the analyzed key, or False if the sample is not an RSA key
"""
is_rsa_key = has_private_component = n_bit_length = False
try:
rsakey = RSA.importKey(sample.strip())
is_rsa_key = True
if rsakey.has_private():
has_private_component = True
n_bit_length = gmpy.mpz(rsakey.n).bit_length()
# Don't really care why it fails, just want to see if it did
except:
is_rsa_key = False
return (is_rsa_key, has_private_component, n_bit_length)
评论列表
文章目录