1. TortoiseLabs LLC
  2. Untitled project
  3. ediarpc


William Pitcock  committed 2047b18

Allow specifying lesser iterations.

  • Participants
  • Parent commits 49f8472
  • Branches master

Comments (0)

Files changed (4)

File ediarpc/message.py

View file
     >>> m.payload_json()
     '{"moo": "cow"}'
-    def __init__(self, obj, secret, expected_hash=None):
+    def __init__(self, obj, secret, expected_hash=None, iterations=1000):
         The constructor for the Message object.
         self.obj = obj.copy()
         self.secret = secret
         self.expected_hash = expected_hash
+        self.iterations = iterations
     def payload_json(self):
         >>> m.signature()
-        signature = pbkdf2_hex(self.payload_json(), self.secret, iterations=1000, keylen=64, hashfunc=hashlib.sha512)
+        signature = pbkdf2_hex(self.payload_json(), self.secret, iterations=self.iterations, keylen=64, hashfunc=hashlib.sha512)
         return signature
     def dumps(self, pretty_print=False):
 class InvalidSignatureException(Exception):
-def loads(json_data, secret, allow_unsigned=False):
+def loads(json_data, secret, allow_unsigned=False, iterations=1000):
     Unpack a JSON envelope containing an Edia RPC message.
     if signature is None and allow_unsigned is not True:
         raise InvalidSignatureException()
-    message = Message(envelope, secret, signature)
+    message = Message(envelope, secret, signature, iterations)
     if message.validate() is not True and allow_unsigned is not True:
         raise InvalidSignatureException()
     return message.payload()
-def dumps(obj, secret, pretty_print=False):
+def dumps(obj, secret, pretty_print=False, iterations=1000):
     Pack a dictionary into an Edia RPC message.
     >>> signed_message == dumps(unpack_message, secret)
-    return Message(obj, secret).dumps(pretty_print)
+    return Message(obj, secret, iterations=iterations).dumps(pretty_print)
 if __name__ == '__main__':
     import doctest

File ediarpc/rpc_client.py

View file
 import rpc_message
 class ServerProxy(object):
-    def __init__(self, host, port, secret, allow_untrusted=False):
+    def __init__(self, host, port, secret, allow_untrusted=False, iterations=1000):
         self._host = host
         self._port = port
         self._secret = secret
         self._allow_untrusted = allow_untrusted
+        self._iterations = iterations
     def _read_loop(self, sock):
         data = []
         response = self._read_loop(sock)
-        return rpc_message.decode(self._secret, response, self._allow_untrusted)[1]
+        return rpc_message.decode(self._secret, response, self._allow_untrusted, iterations=self._iterations)[1]
     def __getattr__(self, name):

File ediarpc/rpc_message.py

View file
 import message
-def encode(secret, method, *args, **kwargs):
+def encode(secret, method, iterations=1000, *args, **kwargs):
     Encode a message.
     >>> encode(secret, 'moo', meow='cats')
     '{"params": {"meow": "cats"}, "method": "moo", "signature": "545ce308d8f3b81fc18638955571aa43f81b71007dc477d78868f7c01b754123556bd958c144db8ef3412a39cd98882207864ce83ba500c3279c40e22852da0d"}'
-    envelope = {'method': method, 'params': kwargs}
-    return message.dumps(envelope, secret)
+    inner_envelope = {'method': method, 'params': kwargs}
+    return message.dumps(inner_envelope, secret, iterations=iterations)
-def decode(secret, signed_message, allow_unsigned=False):
+def decode(secret, signed_message, allow_unsigned=False, iterations=1000):
     Return a tuple of the method and kwargs.
     >>> decode(secret, message)
     (u'moo', {u'meow': u'cats'})
-    unpack_message = message.loads(signed_message, secret, allow_unsigned)
+    unpack_message = message.loads(signed_message, secret, allow_unsigned, iterations)
     method = unpack_message.pop('method', None)
     params = unpack_message.pop('params', None)
     return (method, params)

File ediarpc/rpc_server.py

View file
 class RPCServer(SocketServer.TCPServer, RPCRequestHandler):
-    def __init__(self, tuple, secret):
+    def __init__(self, tuple, secret, iterations=1000):
         SocketServer.TCPServer.__init__(self, tuple, RPCRequestHandler)
         self.funcs = {}
         self.secret = secret
+        self.iterations = iterations
     def _reply(self, method, **kwargs):
-        return rpc_message.encode(self.secret, method, **kwargs)
+        return rpc_message.encode(self.secret, method, iterations=self.iterations, **kwargs)
     def _error_message(self, code, message):
         return self._reply('error_response', error_code=code, error_message=message)
     def dispatch(self, envelope):
-            unpack_message = rpc_message.decode(self.secret, envelope)
+            unpack_message = rpc_message.decode(self.secret, envelope, iterations=self.iterations)
         except InvalidSignatureException:
             return self._error_message(403, "Unauthorized, invalid passphrase")