Commits

William Pitcock committed 2047b18

Allow specifying lesser iterations.

Comments (0)

Files changed (4)

ediarpc/message.py

     >>> 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()
         'b617fdca6b994fa3cb45ea744e3b66f6daee37e24cd1e6df50a6ff38094549a412498c5830144000d3ed451efd39934a6cdc495cb0c86c892f66162a944e6025'
         """
-        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):
     pass
 
-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)
     True
     """
-    return Message(obj, secret).dumps(pretty_print)
+    return Message(obj, secret, iterations=iterations).dumps(pretty_print)
 
 if __name__ == '__main__':
     import doctest

ediarpc/rpc_client.py

 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)
         sock.close()
 
-        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):
         """

ediarpc/rpc_message.py

 
 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)

ediarpc/rpc_server.py

             self.wfile.write(response)
 
 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):
         try:
-            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")
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.