libcloud-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject [2/7] git commit: Add get_secure_random_string utility function.
Date Tue, 05 Nov 2013 19:05:20 GMT
Add get_secure_random_string utility function.


Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/53a67432
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/53a67432
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/53a67432

Branch: refs/heads/trunk
Commit: 53a67432c963a5dd95c4f796a1888e287018764b
Parents: ca1104f
Author: Tomaz Muraus <tomaz@apache.org>
Authored: Tue Nov 5 15:20:41 2013 +0000
Committer: Tomaz Muraus <tomaz@apache.org>
Committed: Tue Nov 5 18:52:12 2013 +0000

----------------------------------------------------------------------
 libcloud/test/test_utils.py |  6 ++++++
 libcloud/utils/misc.py      | 27 ++++++++++++++++++++++++---
 2 files changed, 30 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/53a67432/libcloud/test/test_utils.py
----------------------------------------------------------------------
diff --git a/libcloud/test/test_utils.py b/libcloud/test/test_utils.py
index a4f8531..0233036 100644
--- a/libcloud/test/test_utils.py
+++ b/libcloud/test/test_utils.py
@@ -33,6 +33,7 @@ from libcloud.utils.py3 import b
 from libcloud.utils.py3 import urlquote
 from libcloud.compute.types import Provider
 from libcloud.compute.providers import DRIVERS
+from libcloud.utils.misc import get_secure_random_string
 
 
 WARNINGS_BUFFER = []
@@ -226,6 +227,11 @@ class TestUtils(unittest.TestCase):
         uri = urlquote(b('~abc'))
         self.assertEqual(b(uri), b('%7Eabc'))
 
+    def test_get_secure_random_string(self):
+        for i in range(1, 500):
+            value = get_secure_random_string(size=i)
+            self.assertEqual(len(value), i)
+
 
 if __name__ == '__main__':
     sys.exit(unittest.main())

http://git-wip-us.apache.org/repos/asf/libcloud/blob/53a67432/libcloud/utils/misc.py
----------------------------------------------------------------------
diff --git a/libcloud/utils/misc.py b/libcloud/utils/misc.py
index 93570ad..21978e1 100644
--- a/libcloud/utils/misc.py
+++ b/libcloud/utils/misc.py
@@ -13,6 +13,11 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+import os
+import sys
+import binascii
+
+
 __all__ = [
     'get_driver',
     'set_driver',
@@ -21,11 +26,10 @@ __all__ = [
     'str2dicts',
     'dict2str',
     'reverse_dict',
-    'lowercase_keys'
+    'lowercase_keys',
+    'get_secure_random_string'
 ]
 
-import sys
-
 
 def get_driver(drivers, provider):
     """
@@ -231,3 +235,20 @@ def reverse_dict(dictionary):
 
 def lowercase_keys(dictionary):
     return dict(((k.lower(), v) for k, v in dictionary.items()))
+
+
+def get_secure_random_string(size):
+    """
+    Return a string of ``size`` random bytes. Returned string is suitable for
+    cryptographic use.
+
+    :param size: Size of the generated string.
+    :type size: ``int``
+
+    :return: Random string.
+    :rtype: ``str``
+    """
+    value = os.urandom(size)
+    value = binascii.hexlify(value)
+    value = value.decode('utf-8')[:size]
+    return value


Mime
View raw message