Example #1
0
    def load_public_key(s=None, fileName=None):
        assert s or fileName, "load_public_key must be passed either a string or file"
        if fileName:
            key = M2Crypto.RSA.load_pub_key(fileName)
            publicKey = PublicKey(key)
            eStr, nStr = key.pub()
            publicKey.e = Basic.bytes_to_long(eStr[4:])
            publicKey.n = Basic.bytes_to_long(nStr[4:])
            return publicKey
        else:
            start = s.find("-----BEGIN RSA PUBLIC KEY-----")
            end = s.find("-----END RSA PUBLIC KEY-----")
            if start == -1:
                raise Exception("Missing PEM prefix")
            if end == -1:
                raise Exception("Missing PEM postfix")
            remainder = s[end + len("-----END RSA PUBLIC KEY-----\n\r"):]
            s = s[start + len("-----BEGIN RSA PUBLIC KEY-----"):end]

            parser = decoder.decode(s.decode("base64"))[0]
            n = long(parser.getComponentByPosition(0))
            e = long(parser.getComponentByPosition(1))

            publicKey = PublicKey(n, e)
            return publicKey, remainder
Example #2
0
  def load_public_key(s=None, fileName=None):
    assert s or fileName, "load_public_key must be passed either a string or file"
    if fileName:
      key = M2Crypto.RSA.load_pub_key(fileName)
      publicKey = PublicKey(key)
      eStr, nStr = key.pub()
      publicKey.e = Basic.bytes_to_long(eStr[4:])
      publicKey.n = Basic.bytes_to_long(nStr[4:])
      return publicKey
    else:
      start = s.find("-----BEGIN RSA PUBLIC KEY-----")
      end = s.find("-----END RSA PUBLIC KEY-----")
      if start == -1:
        raise Exception("Missing PEM prefix")
      if end == -1:
        raise Exception("Missing PEM postfix")
      remainder = s[end+len("-----END RSA PUBLIC KEY-----\n\r"):]
      s = s[start+len("-----BEGIN RSA PUBLIC KEY-----") : end]

      parser = decoder.decode(s.decode("base64"))[0]
      n = long(parser.getComponentByPosition(0))
      e = long(parser.getComponentByPosition(1))
      
      publicKey = PublicKey(n, e)
      return publicKey, remainder
Example #3
0
 def __init__(self, constructor):
   """Pass either file location of public key or bit length for new key."""
   if isinstance(constructor, basestring):
     #load the key found at location: constructor
     self.key = M2Crypto.RSA.load_key(constructor)
   elif type(constructor) == int:
     #generate a key of length constructor
     def silence(*args):
       pass
     self.key = M2Crypto.RSA.gen_key(constructor, 65537, silence)
   else:
     raise TypeError('invalid argument: %s'%constructor)
   eStr, nStr = self.key.pub()
   self.e = Basic.bytes_to_long(eStr[4:])
   self.n = Basic.bytes_to_long(nStr[4:])
   #: length of the key in bytes (used in blinding/unblinding)
   self.keyLen = len(self.key)
 def __hash__(self):
     """Basically XOR everything together"""
     result = 0L
     for attr in self.COMPARISON_ORDER:
         val = Basic.bytes_to_long(str(getattr(self, attr)))
         result = result.__xor__(val)
     #TODO:  this is probably not exactly the right way to convert to an integer...
     INT_MAX = sys.maxint
     result = (result % (2 * INT_MAX)) - INT_MAX
     return result
 def __hash__(self):
   """Basically XOR everything together"""
   result = 0L
   for attr in self.COMPARISON_ORDER:
     val = Basic.bytes_to_long(str(getattr(self, attr)))
     result = result.__xor__(val)
   #TODO:  this is probably not exactly the right way to convert to an integer...
   INT_MAX = sys.maxint
   result = (result % (2*INT_MAX)) - INT_MAX
   return result
   
Example #6
0
 def unpack(self, blob):
   """expects to get a struct of the form
   20s hexId
   50s username
   50s password
   128s publickey
   """
   msg = struct.unpack('!128s50s50s128s', blob)
   hexIdSig = msg[0]
   #strip out the null padding characters
   username = msg[1].replace('\x00', '')
   password = msg[2].replace('\x00', '')
   n = long(Basic.bytes_to_long(msg[3]))
   log_msg('attempting to login user: %s @ time: %s!' % (username, time.ctime()), 2)
   return (hexIdSig, username, password, n)
Example #7
0
 def blind(self, message, r, length=None):
     """Blind a message using random number r (assuming length of the key by default)
 @param message: string to be blinded
 @type message: string
 @param r: blinding factor
 @type r: long
 @param length: length of the message after blinding (needed to convert from a long to a string).
 @type long: None or int
 @return: string of message blinded with r assuming length of n
 """
     Basic.validate_type(message, types.StringType)
     Basic.validate_type(r, types.LongType)
     message = Basic.bytes_to_long(message)
     tmp = pow(r, self.e, self.n)
     tmp = (message * tmp) % self.n
     return Basic.long_to_bytes(tmp, length or self.keyLen)
Example #8
0
 def blind(self, message, r, length=None):
   """Blind a message using random number r (assuming length of the key by default)
   @param message: string to be blinded
   @type message: string
   @param r: blinding factor
   @type r: long
   @param length: length of the message after blinding (needed to convert from a long to a string).
   @type long: None or int
   @return: string of message blinded with r assuming length of n
   """
   Basic.validate_type(message, types.StringType)
   Basic.validate_type(r, types.LongType)
   message = Basic.bytes_to_long(message)
   tmp = pow(r, self.e, self.n)
   tmp = (message * tmp) % self.n
   return Basic.long_to_bytes(tmp, length or self.keyLen)