2014-04-07 16:43:39 -04:00
|
|
|
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
|
|
|
|
|
|
# Copyright 2014 IBM Corporation
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2013-08-09 16:59:08 -04:00
|
|
|
|
|
|
|
# Various utility functions that do not neatly fit into one category or another
|
|
|
|
import base64
|
|
|
|
import os
|
2013-09-06 15:21:50 -04:00
|
|
|
import struct
|
|
|
|
|
2013-08-09 16:59:08 -04:00
|
|
|
|
|
|
|
def randomstring(length=20):
|
|
|
|
"""Generate a random string of requested length
|
|
|
|
|
|
|
|
:param length: The number of characters to produce, defaults to 20
|
|
|
|
"""
|
|
|
|
chunksize = length / 4
|
|
|
|
if (length % 4 > 0):
|
|
|
|
chunksize += 1
|
|
|
|
strval = base64.urlsafe_b64encode(os.urandom(chunksize * 3))
|
|
|
|
return strval[0:length-1]
|
2013-09-06 15:21:50 -04:00
|
|
|
|
|
|
|
|
|
|
|
def securerandomnumber(min=0, max=4294967295):
|
|
|
|
"""Return a random number within requested range
|
|
|
|
|
|
|
|
Note that this function will not return smaller than 0 nor larger
|
|
|
|
than 2^32-1 no matter what is requested.
|
|
|
|
The python random number facility does not provide charateristics
|
|
|
|
appropriate for secure rng, go to os.urandom
|
|
|
|
|
|
|
|
:param min: Smallest number to return (defaults to 0)
|
|
|
|
:param max: largest number to return (defaults to 2^32-1)
|
|
|
|
"""
|
|
|
|
number = -1
|
|
|
|
while number < min or number > max:
|
2014-02-06 13:13:16 -05:00
|
|
|
number = struct.unpack("I", os.urandom(4))[0]
|
2013-09-06 15:21:50 -04:00
|
|
|
return number
|
|
|
|
|
2014-02-06 13:13:16 -05:00
|
|
|
|
2013-09-13 14:31:25 -04:00
|
|
|
def monotonic_time():
|
|
|
|
"""Return a monotoc time value
|
|
|
|
|
|
|
|
In scenarios like timeouts and such, monotonic timing is preferred.
|
|
|
|
"""
|
2014-02-06 13:13:16 -05:00
|
|
|
# for now, just support POSIX systems
|
|
|
|
return os.times()[4]
|